//ListAll
        public List <Ob_KH_NhapTu> _ListAll_sp(string StoredName, SqlConnection conn)
        {
            if (conn.ConnectionString == "")
            {
                return(null);
            }
            conn.Open();
            List <Ob_KH_NhapTu> list    = new List <Ob_KH_NhapTu>();
            SqlCommand          command = new SqlCommand
            {
                Connection  = conn,
                CommandType = CommandType.Text,
                CommandText = StoredName
            };
            SqlDataReader reader = command.ExecuteReader();

            while (reader.Read())
            {
                Ob_KH_NhapTu ob = new Ob_KH_NhapTu();
                if (reader[0] != DBNull.Value)
                {
                    ob.ID_NHAPTU = Convert.ToInt32(reader[0]);
                }
                if (reader[1] != DBNull.Value)
                {
                    ob.MANHAPTU = Convert.ToString(reader[1]);
                }
                if (reader[2] != DBNull.Value)
                {
                    ob.TENNHAPTU = Convert.ToString(reader[2]);
                }
                if (reader[3] != DBNull.Value)
                {
                    ob.GHICHU = Convert.ToString(reader[3]);
                }
                if (reader[4] != DBNull.Value)
                {
                    ob.MODIFIEDTIME = Convert.ToDateTime(reader[4]);
                }
                if (reader[5] != DBNull.Value)
                {
                    ob.MODIFIEDBY = Convert.ToString(reader[5]);
                }
                if (reader[6] != DBNull.Value)
                {
                    ob.CREATEDTIME = Convert.ToDateTime(reader[6]);
                }
                if (reader[7] != DBNull.Value)
                {
                    ob.CREATEDBY = Convert.ToString(reader[7]);
                }
                list.Add(ob);
            }
            command.Dispose();
            command = null;
            reader.Dispose();
            reader = null;
            conn.Close();
            return(list);
        }
        //DeleteOB
        public bool _DeleteOb(Ob_KH_NhapTu ob, SqlConnection conn)
        {
            if (conn.ConnectionString == "")
            {
                return(false);
            }
            conn.Open();
            SqlCommand command = new SqlCommand
            {
                Connection  = conn,
                CommandType = CommandType.Text,
                CommandText = @"Delete From KH_NhapTu Where Id_Nhaptu=@Id_Nhaptu"
            };

            command.Parameters.Add(new SqlParameter("Id_Nhaptu", ob.ID_NHAPTU));
            try
            {
                command.ExecuteNonQuery();
            }
            catch (Exception exception)
            {
                MessageBox.Show("Không thể xóa dữ liệu. \r\n" + exception.Message, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                command.Dispose();
                command = null;
                conn.Close();
                return(false);
            }
            command.Dispose();
            command = null;
            conn.Close();
            return(true);
        }
        //ListWhere
        public List <Ob_KH_NhapTu> _ListWhere(string keyword, SqlConnection conn)
        {
            if (conn.ConnectionString == "")
            {
                return(null);
            }
            conn.Open();
            List <Ob_KH_NhapTu> list    = new List <Ob_KH_NhapTu>();
            SqlCommand          command = new SqlCommand
            {
                Connection  = conn,
                CommandType = CommandType.Text,
                CommandText = @"Select Id_Nhaptu, MaNhapTu, TenNhapTu, GhiChu, ModifiedTime, ModifiedBy, CreatedTime, CreatedBy From KH_NhapTu Where " + keyword + ""
            };
            SqlDataReader reader = command.ExecuteReader();

            while (reader.Read())
            {
                Ob_KH_NhapTu ob = new Ob_KH_NhapTu();
                if (reader[0] != DBNull.Value)
                {
                    ob.ID_NHAPTU = Convert.ToInt32(reader[0]);
                }
                if (reader[1] != DBNull.Value)
                {
                    ob.MANHAPTU = Convert.ToString(reader[1]);
                }
                if (reader[2] != DBNull.Value)
                {
                    ob.TENNHAPTU = Convert.ToString(reader[2]);
                }
                if (reader[3] != DBNull.Value)
                {
                    ob.GHICHU = Convert.ToString(reader[3]);
                }
                if (reader[4] != DBNull.Value)
                {
                    ob.MODIFIEDTIME = Convert.ToDateTime(reader[4]);
                }
                if (reader[5] != DBNull.Value)
                {
                    ob.MODIFIEDBY = Convert.ToString(reader[5]);
                }
                if (reader[6] != DBNull.Value)
                {
                    ob.CREATEDTIME = Convert.ToDateTime(reader[6]);
                }
                if (reader[7] != DBNull.Value)
                {
                    ob.CREATEDBY = Convert.ToString(reader[7]);
                }
                list.Add(ob);
            }
            command.Dispose();
            command = null;
            reader.Dispose();
            reader = null;
            conn.Close();
            return(list);
        }
Exemple #4
0
 public Ob_KH_NhapTu(Ob_KH_NhapTu newOb)
 {
     id_nhaptu    = newOb.ID_NHAPTU;
     manhaptu     = newOb.MANHAPTU;
     tennhaptu    = newOb.TENNHAPTU;
     ghichu       = newOb.GHICHU;
     modifiedtime = newOb.MODIFIEDTIME;
     modifiedby   = newOb.MODIFIEDBY;
     createdtime  = newOb.CREATEDTIME;
     createdby    = newOb.CREATEDBY;
 }
        //UpdateOb ob
        public bool _UpdateOb(Ob_KH_NhapTu ob, SqlConnection conn)
        {
            if (conn.ConnectionString == "")
            {
                return(false);
            }
            conn.Open();
            SqlCommand command = new SqlCommand {
                Connection  = conn,
                CommandType = CommandType.Text,
                CommandText = @"Update KH_NhapTu Set MaNhapTu=@MaNhapTu, TenNhapTu=@TenNhapTu, GhiChu=@GhiChu, ModifiedTime=@ModifiedTime, ModifiedBy=@ModifiedBy, CreatedTime=@CreatedTime, CreatedBy=@CreatedBy Where Id_Nhaptu=@Id_Nhaptu"
            };

            command.Parameters.Add("Id_Nhaptu", SqlDbType.Int).Value = ob.ID_NHAPTU;
            if (ob.MANHAPTU.Trim() == "")
            {
                command.Parameters.Add("MaNhapTu", SqlDbType.VarChar).Value = DBNull.Value;
            }
            else
            {
                command.Parameters.Add("MaNhapTu", SqlDbType.VarChar).Value = ob.MANHAPTU;
            }
            if (ob.TENNHAPTU.Trim() == "")
            {
                command.Parameters.Add("TenNhapTu", SqlDbType.NVarChar).Value = DBNull.Value;
            }
            else
            {
                command.Parameters.Add("TenNhapTu", SqlDbType.NVarChar).Value = ob.TENNHAPTU;
            }
            if (ob.GHICHU.Trim() == "")
            {
                command.Parameters.Add("GhiChu", SqlDbType.NVarChar).Value = DBNull.Value;
            }
            else
            {
                command.Parameters.Add("GhiChu", SqlDbType.NVarChar).Value = ob.GHICHU;
            }
            if (ob.MODIFIEDTIME == DateTime.MinValue)
            {
                command.Parameters.Add("ModifiedTime", SqlDbType.DateTime).Value = DBNull.Value;
            }
            else
            {
                command.Parameters.Add("ModifiedTime", SqlDbType.DateTime).Value = ob.MODIFIEDTIME;
            }
            if (ob.MODIFIEDBY.Trim() == "")
            {
                command.Parameters.Add("ModifiedBy", SqlDbType.VarChar).Value = DBNull.Value;
            }
            else
            {
                command.Parameters.Add("ModifiedBy", SqlDbType.VarChar).Value = ob.MODIFIEDBY;
            }
            if (ob.CREATEDTIME == DateTime.MinValue)
            {
                command.Parameters.Add("CreatedTime", SqlDbType.DateTime).Value = DBNull.Value;
            }
            else
            {
                command.Parameters.Add("CreatedTime", SqlDbType.DateTime).Value = ob.CREATEDTIME;
            }
            if (ob.CREATEDBY.Trim() == "")
            {
                command.Parameters.Add("CreatedBy", SqlDbType.VarChar).Value = DBNull.Value;
            }
            else
            {
                command.Parameters.Add("CreatedBy", SqlDbType.VarChar).Value = ob.CREATEDBY;
            }
            try
            {
                command.ExecuteNonQuery();
            }
            catch (Exception exception)
            {
                MessageBox.Show("Không thể cập nhật dữ liệu. \r\n" + exception.Message, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                command.Dispose();
                command = null;
                conn.Close();
                return(false);
            }
            if (command.ExecuteNonQuery() == 0)
            {
                MessageBox.Show("Không thể cập nhật dữ liệu. \r\n", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                command.Dispose();
                command = null;
                conn.Close();
                return(false);
            }
            command.Dispose();
            command = null;
            conn.Close();
            return(true);
        }
        //InsertobGetId
        public int _InsertObGetId(Ob_KH_NhapTu ob, SqlConnection conn)
        {
            int num = 0;

            if (conn.ConnectionString != "")
            {
                conn.Open();
                SqlCommand command = new SqlCommand
                {
                    Connection  = conn,
                    CommandType = CommandType.Text,
                    CommandText = @"Insert Into KH_NhapTu (MaNhapTu, TenNhapTu, GhiChu, ModifiedTime, ModifiedBy, CreatedTime, CreatedBy) Values(@MaNhapTu, @TenNhapTu, @GhiChu, @ModifiedTime, @ModifiedBy, @CreatedTime, @CreatedBy)Select @@IDENTITY"
                };
                if (ob.MANHAPTU.Trim() == "")
                {
                    command.Parameters.Add("MaNhapTu", SqlDbType.VarChar).Value = DBNull.Value;
                }
                else
                {
                    command.Parameters.Add("MaNhapTu", SqlDbType.VarChar).Value = ob.MANHAPTU;
                }
                if (ob.TENNHAPTU.Trim() == "")
                {
                    command.Parameters.Add("TenNhapTu", SqlDbType.NVarChar).Value = DBNull.Value;
                }
                else
                {
                    command.Parameters.Add("TenNhapTu", SqlDbType.NVarChar).Value = ob.TENNHAPTU;
                }
                if (ob.GHICHU.Trim() == "")
                {
                    command.Parameters.Add("GhiChu", SqlDbType.NVarChar).Value = DBNull.Value;
                }
                else
                {
                    command.Parameters.Add("GhiChu", SqlDbType.NVarChar).Value = ob.GHICHU;
                }
                if (ob.MODIFIEDTIME == DateTime.MinValue)
                {
                    command.Parameters.Add("ModifiedTime", SqlDbType.DateTime).Value = DBNull.Value;
                }
                else
                {
                    command.Parameters.Add("ModifiedTime", SqlDbType.DateTime).Value = ob.MODIFIEDTIME;
                }
                if (ob.MODIFIEDBY.Trim() == "")
                {
                    command.Parameters.Add("ModifiedBy", SqlDbType.VarChar).Value = DBNull.Value;
                }
                else
                {
                    command.Parameters.Add("ModifiedBy", SqlDbType.VarChar).Value = ob.MODIFIEDBY;
                }
                if (ob.CREATEDTIME == DateTime.MinValue)
                {
                    command.Parameters.Add("CreatedTime", SqlDbType.DateTime).Value = DBNull.Value;
                }
                else
                {
                    command.Parameters.Add("CreatedTime", SqlDbType.DateTime).Value = ob.CREATEDTIME;
                }
                if (ob.CREATEDBY.Trim() == "")
                {
                    command.Parameters.Add("CreatedBy", SqlDbType.VarChar).Value = DBNull.Value;
                }
                else
                {
                    command.Parameters.Add("CreatedBy", SqlDbType.VarChar).Value = ob.CREATEDBY;
                }
                try
                {
                    num = Convert.ToInt32(command.ExecuteScalar());
                }
                catch (Exception exception)
                {
                    MessageBox.Show("Không thể thêm dữ liệu. \r\n" + exception.Message, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    command.Dispose();
                    command = null;
                    conn.Close();
                    return(num);
                }
                command.Dispose();
                command = null;
                conn.Close();
            }
            return(num);
        }