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

            while (reader.Read())
            {
                Ob_DM_DonViTien ob = new Ob_DM_DonViTien();
                if (reader[0] != DBNull.Value)
                {
                    ob.ID_DONVITIEN = Convert.ToInt32(reader[0]);
                }
                if (reader[1] != DBNull.Value)
                {
                    ob.MADONVITIEN = Convert.ToString(reader[1]);
                }
                if (reader[2] != DBNull.Value)
                {
                    ob.DONVITIEN = Convert.ToString(reader[2]);
                }
                if (reader[3] != DBNull.Value)
                {
                    ob.TRANGTHAI = Convert.ToBoolean(reader[3]);
                }
                if (reader[4] != DBNull.Value)
                {
                    ob.CREATEDDATE = Convert.ToDateTime(reader[4]);
                }
                if (reader[5] != DBNull.Value)
                {
                    ob.CREATEDBY = Convert.ToString(reader[5]);
                }
                if (reader[6] != DBNull.Value)
                {
                    ob.MODIFIEDDATE = Convert.ToDateTime(reader[6]);
                }
                if (reader[7] != DBNull.Value)
                {
                    ob.MODIFIEDBY = Convert.ToString(reader[7]);
                }
                list.Add(ob);
            }
            command.Dispose();
            command = null;
            reader.Dispose();
            reader = null;
            conn.Close();
            return(list);
        }
        //DeleteOB
        public bool _DeleteOb(Ob_DM_DonViTien ob, SqlConnection conn)
        {
            if (conn.ConnectionString == "")
            {
                return(false);
            }
            conn.Open();
            SqlCommand command = new SqlCommand
            {
                Connection  = conn,
                CommandType = CommandType.Text,
                CommandText = @"Delete From DM_DonViTien Where ID_DonViTien=@ID_DonViTien"
            };

            command.Parameters.Add(new SqlParameter("ID_DonViTien", ob.ID_DONVITIEN));
            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_DM_DonViTien> _ListWhere(string keyword, SqlConnection conn)
        {
            if (conn.ConnectionString == "")
            {
                return(null);
            }
            conn.Open();
            List <Ob_DM_DonViTien> list    = new List <Ob_DM_DonViTien>();
            SqlCommand             command = new SqlCommand
            {
                Connection  = conn,
                CommandType = CommandType.Text,
                CommandText = @"Select ID_DonViTien, MaDonViTien, DonViTien, TrangThai, CreatedDate, CreatedBy, ModifiedDate, ModifiedBy From DM_DonViTien Where " + keyword + ""
            };
            SqlDataReader reader = command.ExecuteReader();

            while (reader.Read())
            {
                Ob_DM_DonViTien ob = new Ob_DM_DonViTien();
                if (reader[0] != DBNull.Value)
                {
                    ob.ID_DONVITIEN = Convert.ToInt32(reader[0]);
                }
                if (reader[1] != DBNull.Value)
                {
                    ob.MADONVITIEN = Convert.ToString(reader[1]);
                }
                if (reader[2] != DBNull.Value)
                {
                    ob.DONVITIEN = Convert.ToString(reader[2]);
                }
                if (reader[3] != DBNull.Value)
                {
                    ob.TRANGTHAI = Convert.ToBoolean(reader[3]);
                }
                if (reader[4] != DBNull.Value)
                {
                    ob.CREATEDDATE = Convert.ToDateTime(reader[4]);
                }
                if (reader[5] != DBNull.Value)
                {
                    ob.CREATEDBY = Convert.ToString(reader[5]);
                }
                if (reader[6] != DBNull.Value)
                {
                    ob.MODIFIEDDATE = Convert.ToDateTime(reader[6]);
                }
                if (reader[7] != DBNull.Value)
                {
                    ob.MODIFIEDBY = Convert.ToString(reader[7]);
                }
                list.Add(ob);
            }
            command.Dispose();
            command = null;
            reader.Dispose();
            reader = null;
            conn.Close();
            return(list);
        }
Example #4
0
 public Ob_DM_DonViTien(Ob_DM_DonViTien newOb)
 {
     id_donvitien = newOb.ID_DONVITIEN;
     madonvitien  = newOb.MADONVITIEN;
     donvitien    = newOb.DONVITIEN;
     trangthai    = newOb.TRANGTHAI;
     createddate  = newOb.CREATEDDATE;
     createdby    = newOb.CREATEDBY;
     modifieddate = newOb.MODIFIEDDATE;
     modifiedby   = newOb.MODIFIEDBY;
 }
        //UpdateOb ob
        public bool _UpdateOb(Ob_DM_DonViTien ob, SqlConnection conn)
        {
            if (conn.ConnectionString == "")
            {
                return(false);
            }
            conn.Open();
            SqlCommand command = new SqlCommand {
                Connection  = conn,
                CommandType = CommandType.Text,
                CommandText = @"Update DM_DonViTien Set MaDonViTien=@MaDonViTien, DonViTien=@DonViTien, TrangThai=@TrangThai, CreatedDate=@CreatedDate, CreatedBy=@CreatedBy, ModifiedDate=@ModifiedDate, ModifiedBy=@ModifiedBy Where ID_DonViTien=@ID_DonViTien"
            };

            command.Parameters.Add("ID_DonViTien", SqlDbType.Int).Value = ob.ID_DONVITIEN;
            if (ob.MADONVITIEN.Trim() == "")
            {
                command.Parameters.Add("MaDonViTien", SqlDbType.VarChar).Value = DBNull.Value;
            }
            else
            {
                command.Parameters.Add("MaDonViTien", SqlDbType.VarChar).Value = ob.MADONVITIEN;
            }
            if (ob.DONVITIEN.Trim() == "")
            {
                command.Parameters.Add("DonViTien", SqlDbType.NVarChar).Value = DBNull.Value;
            }
            else
            {
                command.Parameters.Add("DonViTien", SqlDbType.NVarChar).Value = ob.DONVITIEN;
            }
            command.Parameters.Add("TrangThai", SqlDbType.Bit).Value = ob.TRANGTHAI;
            if (ob.CREATEDDATE == DateTime.MinValue)
            {
                command.Parameters.Add("CreatedDate", SqlDbType.DateTime).Value = DBNull.Value;
            }
            else
            {
                command.Parameters.Add("CreatedDate", SqlDbType.DateTime).Value = ob.CREATEDDATE;
            }
            if (ob.CREATEDBY.Trim() == "")
            {
                command.Parameters.Add("CreatedBy", SqlDbType.VarChar).Value = DBNull.Value;
            }
            else
            {
                command.Parameters.Add("CreatedBy", SqlDbType.VarChar).Value = ob.CREATEDBY;
            }
            if (ob.MODIFIEDDATE == DateTime.MinValue)
            {
                command.Parameters.Add("ModifiedDate", SqlDbType.DateTime).Value = DBNull.Value;
            }
            else
            {
                command.Parameters.Add("ModifiedDate", SqlDbType.DateTime).Value = ob.MODIFIEDDATE;
            }
            if (ob.MODIFIEDBY.Trim() == "")
            {
                command.Parameters.Add("ModifiedBy", SqlDbType.VarChar).Value = DBNull.Value;
            }
            else
            {
                command.Parameters.Add("ModifiedBy", SqlDbType.VarChar).Value = ob.MODIFIEDBY;
            }
            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_DM_DonViTien ob, SqlConnection conn)
        {
            int num = 0;

            if (conn.ConnectionString != "")
            {
                conn.Open();
                SqlCommand command = new SqlCommand
                {
                    Connection  = conn,
                    CommandType = CommandType.Text,
                    CommandText = @"Insert Into DM_DonViTien (MaDonViTien, DonViTien, TrangThai, CreatedDate, CreatedBy, ModifiedDate, ModifiedBy) Values(@MaDonViTien, @DonViTien, @TrangThai, @CreatedDate, @CreatedBy, @ModifiedDate, @ModifiedBy)Select @@IDENTITY"
                };
                if (ob.MADONVITIEN.Trim() == "")
                {
                    command.Parameters.Add("MaDonViTien", SqlDbType.VarChar).Value = DBNull.Value;
                }
                else
                {
                    command.Parameters.Add("MaDonViTien", SqlDbType.VarChar).Value = ob.MADONVITIEN;
                }
                if (ob.DONVITIEN.Trim() == "")
                {
                    command.Parameters.Add("DonViTien", SqlDbType.NVarChar).Value = DBNull.Value;
                }
                else
                {
                    command.Parameters.Add("DonViTien", SqlDbType.NVarChar).Value = ob.DONVITIEN;
                }
                command.Parameters.Add("TrangThai", SqlDbType.Bit).Value = ob.TRANGTHAI;
                if (ob.CREATEDDATE == DateTime.MinValue)
                {
                    command.Parameters.Add("CreatedDate", SqlDbType.DateTime).Value = DBNull.Value;
                }
                else
                {
                    command.Parameters.Add("CreatedDate", SqlDbType.DateTime).Value = ob.CREATEDDATE;
                }
                if (ob.CREATEDBY.Trim() == "")
                {
                    command.Parameters.Add("CreatedBy", SqlDbType.VarChar).Value = DBNull.Value;
                }
                else
                {
                    command.Parameters.Add("CreatedBy", SqlDbType.VarChar).Value = ob.CREATEDBY;
                }
                if (ob.MODIFIEDDATE == DateTime.MinValue)
                {
                    command.Parameters.Add("ModifiedDate", SqlDbType.DateTime).Value = DBNull.Value;
                }
                else
                {
                    command.Parameters.Add("ModifiedDate", SqlDbType.DateTime).Value = ob.MODIFIEDDATE;
                }
                if (ob.MODIFIEDBY.Trim() == "")
                {
                    command.Parameters.Add("ModifiedBy", SqlDbType.VarChar).Value = DBNull.Value;
                }
                else
                {
                    command.Parameters.Add("ModifiedBy", SqlDbType.VarChar).Value = ob.MODIFIEDBY;
                }
                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);
        }