Ejemplo n.º 1
0
        //DeleteOB
        public bool _DeleteOb(Ob_DM_DuLieu ob, SqlConnection conn)
        {
            if (conn.ConnectionString == "")
            {
                return(false);
            }
            conn.Open();
            SqlCommand command = new SqlCommand
            {
                Connection  = conn,
                CommandType = CommandType.Text,
                CommandText = @"Delete From DM_DuLieu Where ID_DuLieu=@ID_DuLieu"
            };

            command.Parameters.Add(new SqlParameter("ID_DuLieu", ob.ID_DULIEU));
            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);
        }
Ejemplo n.º 2
0
 public Ob_DM_DuLieu(Ob_DM_DuLieu newOb)
 {
     id_dulieu = newOb.ID_DULIEU;
     tenbang   = newOb.TENBANG;
     tencot    = newOb.TENCOT;
     mota      = newOb.MOTA;
 }
Ejemplo n.º 3
0
        //InsertobGetId
        public int _InsertObGetId(Ob_DM_DuLieu ob, SqlConnection conn)
        {
            int num = 0;

            if (conn.ConnectionString != "")
            {
                conn.Open();
                SqlCommand command = new SqlCommand
                {
                    Connection  = conn,
                    CommandType = CommandType.Text,
                    CommandText = @"Insert Into DM_DuLieu (ID_DuLieu, TenBang, TenCot, MoTa) Values(@ID_DuLieu, @TenBang, @TenCot, @MoTa)Select @@IDENTITY"
                };
                command.Parameters.Add("ID_DuLieu", SqlDbType.Int).Value = ob.ID_DULIEU;
                if (ob.TENBANG.Trim() == "")
                {
                    command.Parameters.Add("TenBang", SqlDbType.VarChar).Value = DBNull.Value;
                }
                else
                {
                    command.Parameters.Add("TenBang", SqlDbType.VarChar).Value = ob.TENBANG;
                }
                if (ob.TENCOT.Trim() == "")
                {
                    command.Parameters.Add("TenCot", SqlDbType.VarChar).Value = DBNull.Value;
                }
                else
                {
                    command.Parameters.Add("TenCot", SqlDbType.VarChar).Value = ob.TENCOT;
                }
                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);
        }
Ejemplo n.º 4
0
        //Insert ob
        public bool _InsertOb(Ob_DM_DuLieu ob, SqlConnection conn)
        {
            if (conn.ConnectionString == "")
            {
                return(false);
            }
            conn.Open();
            SqlCommand command = new SqlCommand
            {
                Connection  = conn,
                CommandType = CommandType.Text,
                CommandText = @"Insert Into DM_DuLieu (ID_DuLieu, TenBang, TenCot, MoTa) Values(@ID_DuLieu, @TenBang, @TenCot, @MoTa)"
            };

            command.Parameters.Add("ID_DuLieu", SqlDbType.Int).Value = ob.ID_DULIEU;
            if (ob.TENBANG.Trim() == "")
            {
                command.Parameters.Add("TenBang", SqlDbType.VarChar).Value = DBNull.Value;
            }
            else
            {
                command.Parameters.Add("TenBang", SqlDbType.VarChar).Value = ob.TENBANG;
            }
            if (ob.TENCOT.Trim() == "")
            {
                command.Parameters.Add("TenCot", SqlDbType.VarChar).Value = DBNull.Value;
            }
            else
            {
                command.Parameters.Add("TenCot", SqlDbType.VarChar).Value = ob.TENCOT;
            }
            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ể 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);
        }
Ejemplo n.º 5
0
        //GetObWhere
        public Ob_DM_DuLieu _GetObWhere(string keyword, SqlConnection conn)
        {
            if (conn.ConnectionString == "")
            {
                return(null);
            }
            conn.Open();
            Ob_DM_DuLieu ob      = new Ob_DM_DuLieu();
            SqlCommand   command = new SqlCommand
            {
                Connection  = conn,
                CommandType = CommandType.Text,
                CommandText = @"Select ID_DuLieu, TenBang, TenCot, MoTa From DM_DuLieu Where " + keyword + ""
            };
            SqlDataReader reader = command.ExecuteReader();
            int           num    = 0;

            while (reader.Read())
            {
                num++;
                if (reader[0] != DBNull.Value)
                {
                    ob.ID_DULIEU = Convert.ToInt32(reader[0]);
                }
                if (reader[1] != DBNull.Value)
                {
                    ob.TENBANG = Convert.ToString(reader[1]);
                }
                if (reader[2] != DBNull.Value)
                {
                    ob.TENCOT = Convert.ToString(reader[2]);
                }
                if (reader[3] != DBNull.Value)
                {
                    ob.MOTA = Convert.ToString(reader[3]);
                }
            }
            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);
        }
Ejemplo n.º 6
0
        //ListWhere_sp
        public List <Ob_DM_DuLieu> _ListWhere_sp(string StoredName, SqlParameter[] ArrayParameter, SqlConnection conn)
        {
            if (conn.ConnectionString == "")
            {
                return(null);
            }
            conn.Open();
            List <Ob_DM_DuLieu> list    = new List <Ob_DM_DuLieu>();
            SqlCommand          command = new SqlCommand
            {
                Connection  = conn,
                CommandType = CommandType.Text,
                CommandText = StoredName
            };

            command.Parameters.AddRange(ArrayParameter);
            SqlDataReader reader = command.ExecuteReader();

            while (reader.Read())
            {
                Ob_DM_DuLieu ob = new Ob_DM_DuLieu();
                if (reader[0] != DBNull.Value)
                {
                    ob.ID_DULIEU = Convert.ToInt32(reader[0]);
                }
                if (reader[1] != DBNull.Value)
                {
                    ob.TENBANG = Convert.ToString(reader[1]);
                }
                if (reader[2] != DBNull.Value)
                {
                    ob.TENCOT = Convert.ToString(reader[2]);
                }
                if (reader[3] != DBNull.Value)
                {
                    ob.MOTA = Convert.ToString(reader[3]);
                }
                list.Add(ob);
            }
            command.Dispose();
            command = null;
            reader.Dispose();
            reader = null;
            conn.Close();
            return(list);
        }
Ejemplo n.º 7
0
        //ListAll
        public List <Ob_DM_DuLieu> _ListAll(SqlConnection conn)
        {
            if (conn.ConnectionString == "")
            {
                return(null);
            }
            conn.Open();
            List <Ob_DM_DuLieu> list    = new List <Ob_DM_DuLieu>();
            SqlCommand          command = new SqlCommand
            {
                Connection  = conn,
                CommandType = CommandType.Text,
                CommandText = @"Select ID_DuLieu, TenBang, TenCot, MoTa From DM_DuLieu"
            };
            SqlDataReader reader = command.ExecuteReader();

            while (reader.Read())
            {
                Ob_DM_DuLieu ob = new Ob_DM_DuLieu();
                if (reader[0] != DBNull.Value)
                {
                    ob.ID_DULIEU = Convert.ToInt32(reader[0]);
                }
                if (reader[1] != DBNull.Value)
                {
                    ob.TENBANG = Convert.ToString(reader[1]);
                }
                if (reader[2] != DBNull.Value)
                {
                    ob.TENCOT = Convert.ToString(reader[2]);
                }
                if (reader[3] != DBNull.Value)
                {
                    ob.MOTA = Convert.ToString(reader[3]);
                }
                list.Add(ob);
            }
            command.Dispose();
            command = null;
            reader.Dispose();
            reader = null;
            conn.Close();
            return(list);
        }