public static int Update(int ID, bool Aktiv, string Name, string Beskrivelse, string Filename, string Ext, string Mime, int Type, int Category, int Owner, int MedArbGrpID, int Size, int Width, int Height, DateTime Dato, DateTime ExpireDate, string UserId, string IP, bool Hidden, string Clob, string RettetAF, string RettetIP)
        {
            Dokumenter d = new Dokumenter(ID);

            d.Aktiv       = Aktiv;
            d.Hidden      = Hidden;
            d.Dato        = Dato;
            d.ExpireDate  = ExpireDate;
            d.Name        = Name;
            d.Mime        = Mime;
            d.Beskrivelse = Beskrivelse;
            d.Type        = Type;
            d.Category    = Category;
            d.Owner       = Owner;
            d.MedArbGrpID = MedArbGrpID;
            d.Filename    = Filename;
            d.Ext         = Ext;
            d.Size        = Size;
            d.Width       = Width;
            d.Height      = Height;
            d.UserID      = UserId;
            d.IP          = IP;
            d.Clob        = Clob;
            //.Blob = Blob
            return(Update(d)); //db.ExecuteNonQuery("Co2Db_Dokumenter_Update")
        }
        public static int Insert(string Name, string Beskrivelse, string Filename, string Ext, string Mime, int Type, int Category, int Owner, int MedArbGrpID, int Size, int Width, int Height, DateTime Dato, DateTime ExpireDate, string UserId, string IP, bool Hidden, string Clob, object Blob, string RettetAF, string RettetIP)
        {
            Dokumenter d = new Dokumenter();

            d.Aktiv       = true;
            d.Hidden      = Hidden;
            d.Dato        = Dato;
            d.ExpireDate  = ExpireDate;
            d.Name        = Name;
            d.Mime        = Mime;
            d.Beskrivelse = Beskrivelse;
            d.Type        = Type;
            d.Category    = Category;
            d.Owner       = Owner;
            d.MedArbGrpID = MedArbGrpID;
            d.Filename    = Filename;
            d.Ext         = Ext;
            d.Size        = Size;
            d.Width       = Width;
            d.Height      = Height;
            d.UserID      = UserId;
            d.IP          = IP;
            d.Clob        = Clob;
            d.Blob        = Blob;
            return(Insert(d));
        }
        private static void Populate(SqlDataReader dr, Dokumenter d)
        {
            PopulateStandard(dr, d);
            var with_1 = d;

            with_1.Aktiv       = System.Convert.ToBoolean(dr.DBtoBool("Aktiv"));
            with_1.Dato        = System.Convert.ToDateTime(dr.DBtoDate("Dato"));
            with_1.ExpireDate  = System.Convert.ToDateTime(dr.DBtoDate("ExpireDate"));
            with_1.Name        = dr.DBtoString("Name");
            with_1.Mime        = dr.DBtoString("Mime");
            with_1.Beskrivelse = dr.DBtoString("Beskrivelse");
            with_1.Type        = System.Convert.ToInt32(dr.DBtoInt("Type"));
            with_1.Category    = System.Convert.ToInt32(dr.DBtoInt("Category"));
            with_1.Owner       = System.Convert.ToInt32(dr.DBtoInt("Owner"));
            with_1.MedArbGrpID = System.Convert.ToInt32(dr.DBtoInt("MedArbGrpID"));
            with_1.Filename    = dr.DBtoString("Filename");
            with_1.Ext         = dr.DBtoString("Ext");
            with_1.Size        = System.Convert.ToInt32(dr.DBtoInt("Size"));
            with_1.Width       = System.Convert.ToInt32(dr.DBtoInt("Width"));
            with_1.Height      = System.Convert.ToInt32(dr.DBtoInt("Height"));
            with_1.UserID      = dr.DBtoString("UserID");

            //.Blob = dbto
            with_1.Clob = dr.DBtoString("Clob");
        }
        private static void AddParms(ref DBAccess db, Dokumenter d)
        {
            var with_1 = d;

            db.AddBit("Aktiv", with_1.Aktiv);
            db.AddDateTime("Dato", with_1.Dato);
            db.AddDateTime("ExpireDate", with_1.ExpireDate);
            db.AddNVarChar("Name", with_1.Name, 250);
            db.AddNVarChar("Mime", with_1.Mime, 100);
            db.AddNVarChar("Beskrivelse", with_1.Beskrivelse, -1);
            db.AddInt("Type", with_1.Type);
            db.AddInt("Category", with_1.Category);
            db.AddInt("Owner", with_1.Owner);
            db.AddInt("MedArbGrpID", with_1.MedArbGrpID);
            db.AddNVarChar("Filename", with_1.Filename, 250);
            db.AddNVarChar("Ext", with_1.Ext, 50);
            db.AddInt("Size", with_1.Size);
            db.AddInt("Width", with_1.Width);
            db.AddInt("Height", with_1.Height);
            db.AddNVarChar("UserID", with_1.UserID, 50);

            db.AddBlob("Blob", d.Blob);
            db.AddNVarChar("Clob", with_1.Clob, -1);
            AddParmsStandard(db, d);
        }
        public static string UpdateClob(Dokumenter d)
        {
            DBAccess db = new DBAccess();

            db.AddInt("ID", d.ID);
            db.AddNVarChar("Clob", d.Clob, -1);
            return(System.Convert.ToString(db.ExecuteNonQuery(_SQLUpdateClob)));
        }
        public static int UpdateBlob(Dokumenter d)
        {
            DBAccess db = new DBAccess();

            db.AddInt("ID", d.ID);
            db.AddBlob("Blob", d.Blob);
            return(db.ExecuteNonQuery(_SQLUpdateBlob));
        }
        public static int Update(Dokumenter d)
        {
            DBAccess db = new DBAccess();

            db.AddInt("ID", d.ID);
            AddParms(ref db, d);

            int retval = db.ExecuteNonQuery(_SQLUpdate);

            return(retval);
        }
        public static int InsertFromDisk(int OwnerID, int TypeID, int CategoryID, string Beskrivelse, string DokumentNavn, string Filename, bool Hidden, bool DeleteFile = false)
        {
            if (!File.Exists(Filename))
            {
                throw (new FileNotFoundException("Cannot create attachment because the file was not found", Filename));
            }

            Dokumenter Dok = new Dokumenter();

            Dok.Owner       = OwnerID;
            Dok.Type        = TypeID;
            Dok.Category    = CategoryID;
            Dok.Beskrivelse = Beskrivelse;
            Dok.Name        = DokumentNavn;

            Dok.Aktiv      = true;
            Dok.Hidden     = Hidden;
            Dok.Dato       = DateTime.Today;
            Dok.ExpireDate = DateTime.Today.AddYears(99);

            Dok.Mime = FileIO.getMimeFromFile(Filename); // System.Net.Mime.MediaTypeNames.Application.Pdf ' Text.Plain

            // Load file from harddrive and load into database
            FileInfo fi = new FileInfo(Filename);

            Dok.Filename = fi.Name;
            Dok.Ext      = fi.Extension.Replace(".", "");
            Dok.Size     = (int)fi.Length;
            fi           = null;

            using (FileStream reader = new FileStream(Filename, FileMode.Open))
            {
                byte[] blob = new byte[System.Convert.ToInt32(reader.Length - 1) + 1];
                reader.Read(blob, 0, (int)reader.Length);
                //reader.Close()
                Dok.Blob = blob;
            }


            if (DeleteFile == true)
            {
                try
                {
                    File.Delete(Filename);
                }
                catch (Exception)
                {
                }
            }

            return(Dokumenter.Insert(Dok));
        }
        public static Dokumenter GetDokument(int ID)
        {
            DBAccess   db = new DBAccess();
            Dokumenter d  = new Dokumenter();

            db.AddInt("ID", ID);

            SqlDataReader dr = (SqlDataReader)(db.ExecuteReader(_SQLSelectOne));  //"Co2Db_Dokumenter_SelectOne")

            if (dr.HasRows)
            {
                //	While dr.Read
                dr.Read();
                Populate(dr, d);
                //	End While
            }
            dr.Close();
            return(d);
        }
        public static int Insert(Dokumenter d)
        {
            DBAccess db = new DBAccess();

            AddParms(ref db, d);

            SqlParameter p = new SqlParameter("@ID", 0);

            p.Direction = ParameterDirection.Output;
            db.AddParameter(p);

            int retval = db.ExecuteNonQuery(_SQLInsert); //"Co2Db_Dokumenter_Insert")

            if (retval == 1)
            {
                int ID = int.Parse(p.Value.ToString());
                return(ID); //Integer.Parse(ID.Value.ToString)
            }
            else
            {
                return(-1);
            }
        }
 public static int Delete(Dokumenter d)
 {
     return(Delete(d.ID));
 }