//DeleteOB public bool _DeleteOb(Ob_DM_LoaiCK ob, SqlConnection conn) { if (conn.ConnectionString == "") { return(false); } conn.Open(); SqlCommand command = new SqlCommand { Connection = conn, CommandType = CommandType.Text, CommandText = @"Delete From DM_LoaiCK Where MaLoaiCK=@MaLoaiCK" }; command.Parameters.Add(new SqlParameter("MaLoaiCK", ob.MALOAICK)); 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); }
//UpdateOb ob public bool _UpdateOb(Ob_DM_LoaiCK ob, SqlConnection conn) { if (conn.ConnectionString == "") { return(false); } conn.Open(); SqlCommand command = new SqlCommand { Connection = conn, CommandType = CommandType.Text, CommandText = @"Update DM_LoaiCK Set MoTa=@MoTa Where MaLoaiCK=@MaLoaiCK" }; if (ob.MALOAICK.Trim() == "") { command.Parameters.Add("MaLoaiCK", SqlDbType.VarChar).Value = DBNull.Value; } else { command.Parameters.Add("MaLoaiCK", SqlDbType.VarChar).Value = ob.MALOAICK; } if (ob.MOTA.Trim() == "") { command.Parameters.Add("MoTa", SqlDbType.NVarChar).Value = DBNull.Value; } else { command.Parameters.Add("MoTa", SqlDbType.NVarChar).Value = ob.MOTA; } 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_LoaiCK ob, SqlConnection conn) { int num = 0; if (conn.ConnectionString != "") { conn.Open(); SqlCommand command = new SqlCommand { Connection = conn, CommandType = CommandType.Text, CommandText = @"Insert Into DM_LoaiCK (MaLoaiCK, MoTa) Values(@MaLoaiCK, @MoTa)Select @@IDENTITY" }; if (ob.MALOAICK.Trim() == "") { command.Parameters.Add("MaLoaiCK", SqlDbType.VarChar).Value = DBNull.Value; } else { command.Parameters.Add("MaLoaiCK", SqlDbType.VarChar).Value = ob.MALOAICK; } if (ob.MOTA.Trim() == "") { command.Parameters.Add("MoTa", SqlDbType.NVarChar).Value = DBNull.Value; } else { command.Parameters.Add("MoTa", SqlDbType.NVarChar).Value = ob.MOTA; } 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); }
//GetObWhere public Ob_DM_LoaiCK _GetObWhere(string keyword, SqlConnection conn) { if (conn.ConnectionString == "") { return(null); } conn.Open(); Ob_DM_LoaiCK ob = new Ob_DM_LoaiCK(); SqlCommand command = new SqlCommand { Connection = conn, CommandType = CommandType.Text, CommandText = @"Select MaLoaiCK, MoTa From DM_LoaiCK Where " + keyword + "" }; SqlDataReader reader = command.ExecuteReader(); int num = 0; while (reader.Read()) { num++; if (reader[0] != DBNull.Value) { ob.MALOAICK = Convert.ToString(reader[0]); } if (reader[1] != DBNull.Value) { ob.MOTA = Convert.ToString(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_DM_LoaiCK> _ListWhere_sp(string StoredName, SqlParameter[] ArrayParameter, SqlConnection conn) { if (conn.ConnectionString == "") { return(null); } conn.Open(); List <Ob_DM_LoaiCK> list = new List <Ob_DM_LoaiCK>(); SqlCommand command = new SqlCommand { Connection = conn, CommandType = CommandType.Text, CommandText = StoredName }; command.Parameters.AddRange(ArrayParameter); SqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { Ob_DM_LoaiCK ob = new Ob_DM_LoaiCK(); if (reader[0] != DBNull.Value) { ob.MALOAICK = Convert.ToString(reader[0]); } if (reader[1] != DBNull.Value) { ob.MOTA = Convert.ToString(reader[1]); } list.Add(ob); } command.Dispose(); command = null; reader.Dispose(); reader = null; conn.Close(); return(list); }
//ListWhere public List <Ob_DM_LoaiCK> _ListWhere(string keyword, SqlConnection conn) { if (conn.ConnectionString == "") { return(null); } conn.Open(); List <Ob_DM_LoaiCK> list = new List <Ob_DM_LoaiCK>(); SqlCommand command = new SqlCommand { Connection = conn, CommandType = CommandType.Text, CommandText = @"Select MaLoaiCK, MoTa From DM_LoaiCK Where " + keyword + "" }; SqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { Ob_DM_LoaiCK ob = new Ob_DM_LoaiCK(); if (reader[0] != DBNull.Value) { ob.MALOAICK = Convert.ToString(reader[0]); } if (reader[1] != DBNull.Value) { ob.MOTA = Convert.ToString(reader[1]); } list.Add(ob); } command.Dispose(); command = null; reader.Dispose(); reader = null; conn.Close(); return(list); }
public Ob_DM_LoaiCK(Ob_DM_LoaiCK newOb) { maloaick = newOb.MALOAICK; mota = newOb.MOTA; }