//InsertobGetId public int _InsertObGetId(Ob_DH_SPMua ob, SqlConnection conn) { int num = 0; if (conn.ConnectionString != "") { conn.Open(); SqlCommand command = new SqlCommand { Connection = conn, CommandType = CommandType.Text, CommandText = @"Insert Into DH_SPMua (ID_SanPham, ID_NhomGiaoHang) Values(@ID_SanPham, @ID_NhomGiaoHang)Select @@IDENTITY" }; command.Parameters.Add("ID_SanPham", SqlDbType.Int).Value = ob.ID_SANPHAM; command.Parameters.Add("ID_NhomGiaoHang", SqlDbType.Int).Value = ob.ID_NHOMGIAOHANG; 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); }
//DeleteOB public bool _DeleteOb(Ob_DH_SPMua ob, SqlConnection conn) { if (conn.ConnectionString == "") { return(false); } conn.Open(); SqlCommand command = new SqlCommand { Connection = conn, CommandType = CommandType.Text, CommandText = @"Delete From DH_SPMua Where ID_SanPham=@ID_SanPham" }; command.Parameters.Add(new SqlParameter("ID_SanPham", ob.ID_SANPHAM)); 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); }
//Insert ob public bool _InsertOb(Ob_DH_SPMua ob, SqlConnection conn) { if (conn.ConnectionString == "") { return(false); } conn.Open(); SqlCommand command = new SqlCommand { Connection = conn, CommandType = CommandType.Text, CommandText = @"Insert Into DH_SPMua (ID_SanPham, ID_NhomGiaoHang) Values(@ID_SanPham, @ID_NhomGiaoHang)" }; command.Parameters.Add("ID_SanPham", SqlDbType.Int).Value = ob.ID_SANPHAM; command.Parameters.Add("ID_NhomGiaoHang", SqlDbType.Int).Value = ob.ID_NHOMGIAOHANG; try { command.ExecuteNonQuery(); } 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(false); } command.Dispose(); command = null; conn.Close(); return(true); }
//GetObWhere public Ob_DH_SPMua _GetObWhere(string keyword, SqlConnection conn) { if (conn.ConnectionString == "") { return(null); } conn.Open(); Ob_DH_SPMua ob = new Ob_DH_SPMua(); SqlCommand command = new SqlCommand { Connection = conn, CommandType = CommandType.Text, CommandText = @"Select ID_SanPham, ID_NhomGiaoHang From DH_SPMua Where " + keyword + "" }; SqlDataReader reader = command.ExecuteReader(); int num = 0; while (reader.Read()) { num++; if (reader[0] != DBNull.Value) { ob.ID_SANPHAM = Convert.ToInt32(reader[0]); } if (reader[1] != DBNull.Value) { ob.ID_NHOMGIAOHANG = Convert.ToInt32(reader[1]); } } if (num == 0) { command.Dispose(); command = null; reader.Dispose(); reader = null; conn.Close(); return(null); } command.Dispose(); command = null; reader.Dispose(); reader = null; conn.Close(); return(ob); }
//ListWhere_sp public List <Ob_DH_SPMua> _ListWhere_sp(string StoredName, SqlParameter[] ArrayParameter, SqlConnection conn) { if (conn.ConnectionString == "") { return(null); } conn.Open(); List <Ob_DH_SPMua> list = new List <Ob_DH_SPMua>(); SqlCommand command = new SqlCommand { Connection = conn, CommandType = CommandType.Text, CommandText = StoredName }; command.Parameters.AddRange(ArrayParameter); SqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { Ob_DH_SPMua ob = new Ob_DH_SPMua(); if (reader[0] != DBNull.Value) { ob.ID_SANPHAM = Convert.ToInt32(reader[0]); } if (reader[1] != DBNull.Value) { ob.ID_NHOMGIAOHANG = Convert.ToInt32(reader[1]); } list.Add(ob); } command.Dispose(); command = null; reader.Dispose(); reader = null; conn.Close(); return(list); }
//ListAll public List <Ob_DH_SPMua> _ListAll(SqlConnection conn) { if (conn.ConnectionString == "") { return(null); } conn.Open(); List <Ob_DH_SPMua> list = new List <Ob_DH_SPMua>(); SqlCommand command = new SqlCommand { Connection = conn, CommandType = CommandType.Text, CommandText = @"Select ID_SanPham, ID_NhomGiaoHang From DH_SPMua" }; SqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { Ob_DH_SPMua ob = new Ob_DH_SPMua(); if (reader[0] != DBNull.Value) { ob.ID_SANPHAM = Convert.ToInt32(reader[0]); } if (reader[1] != DBNull.Value) { ob.ID_NHOMGIAOHANG = Convert.ToInt32(reader[1]); } list.Add(ob); } command.Dispose(); command = null; reader.Dispose(); reader = null; conn.Close(); return(list); }
public Ob_DH_SPMua(Ob_DH_SPMua newOb) { id_sanpham = newOb.ID_SANPHAM; id_nhomgiaohang = newOb.ID_NHOMGIAOHANG; }