Winform Kết hợp truy vấn dữ liệu với ADO.NET

0

Lập trình phần mềm  Quản lý thông tin sách có thêm các chức năng thêm mới, xóa thông tin sách từ CSDL

Chức năng của ứng dụng được mô tả như sau:

  • Khi load ứng dụng
  • Hiển thị thông tin về tất cả các loại sách trong ứng dụng trên ListView
  • Các TextBox đều bị vô hiệu hóa
  • Button Thêm mới được kích hoạt cho phép thêm thông tin sá ch mới
  • Button Lưu, Xóa, Bỏ qua bị vô hiệu hóa
  • Khi một cuốn sách trên ListView được chọn
  • Thông tin về cuốn sách được hiển thị trên các TextBox vẫn đang bị vô hiệu hóa
  • Button Thêm mới, Xóa được kích hoạt
  • Button Lưu, Bỏ qua bị vô hiệu hóa

    • Hình 2. Form khi một item trên ListView được chọn .
      • Khi button Thêm mới được nhấn
      • Các TextBox được kích hoạt cho phép nhập thông tin
      • Button Lưu được kích hoạt cho phép lưu thông tin
      • Button Bỏ qua được kích hoạt cho phép bỏ qua thao tác Thêm mới
      • Button Thêm mới, Xóa, bị vô hiệu hóa
      • Khi button Xóa được nhấn
      • Hiển thị thông báo hỏi người dùng có chắc chắn xóa không
      • Xóa thông tin

      Yêu cầu: Thực hiện thao tác thêm hàng, xóa hàng trên DataSet, sau đó cập nhật lại CSDL

      Phần thiết kế thì bạn có thể kéo thả và sau đó đặt tên cho từng ô textbox và nút button,ở đây mình sẽ trình bày về phần code xử lý

       public partial class Form1 : Form
      {
      public Form1()
      {
      InitializeComponent();
      }
      
      private void load_data()
      {
      SqlConnection con = new SqlConnection(“server=.;database=sachdb;integrated security=true;”);
      SqlDataAdapter da = new SqlDataAdapter(“select * from products”,con);
      DataTable tb = new DataTable();
      da.Fill(tb);
      dataGridView1.DataSource = tb;
       
      //databinding
      textBox1.DataBindings.Clear();
      textBox2.DataBindings.Clear();
      textBox3.DataBindings.Clear();
      textBox4.DataBindings.Clear();
       
      textBox1.DataBindings.Add(“Text”, dataGridView1.DataSource, “productcode”);
      textBox2.DataBindings.Add(“Text”, dataGridView1.DataSource, “Description”);
      textBox3.DataBindings.Add(“Text”, dataGridView1.DataSource, “UnitPrice”);
      textBox4.DataBindings.Add(“Text”, dataGridView1.DataSource, “OnHandQuantity”);
       
       
      }
      private void Form1_Load(object sender, EventArgs e)
      {
      load_data();
      textBox1.Enabled = false;
      textBox2.Enabled = false;
      textBox3.Enabled = false;
      textBox4.Enabled = false;
       
      button2.Enabled = false;
      button3.Enabled = false;
      button4.Enabled = false;
      }
       
      private void button1_Click(object sender, EventArgs e)
      {
      textBox1.Enabled = true;
       
      textBox2.Enabled = true;
      textBox3.Enabled = true;
      textBox4.Enabled = true;
      textBox1.Text = “”;
      textBox2.Text = “”;
      textBox3.Text = “”;
      textBox4.Text = “”;
      textBox1.Focus();
       
      button1.Enabled = false;
      button3.Enabled = false;
      button2.Enabled = true;
      button4.Enabled = true;
      }
       
      private void button2_Click(object sender, EventArgs e)
      {
      SqlConnection con = new SqlConnection(“server=.;database=sachdb;integrated security=true;”);
      SqlCommand cmd = new SqlCommand(“insert into products values(‘” + textBox1.Text + “‘,’” + textBox2.Text + “‘,’” + textBox3.Text + “‘,’” + textBox4.Text + “‘)”, con);
      con.Open();
      cmd.ExecuteNonQuery();
      con.Close();
      load_data();
      }
       
      private void button3_Click(object sender, EventArgs e)
      {
      DialogResult kq = MessageBox.Show(“ban muon xoa khong ?”,”tieu de”,MessageBoxButtons.YesNo);
       
      if (kq == System.Windows.Forms.DialogResult.Yes)
      {
      SqlConnection con = new SqlConnection(“server=.;database=sachdb;integrated security=true;”);
      SqlCommand cmd = new SqlCommand(“delete from products where productcode = ‘”+textBox1.Text+”‘”, con);
      con.Open();
      cmd.ExecuteNonQuery();
      con.Close();
      load_data();
      }
      }
       
      
      private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
      {
      button3.Enabled = true;
      button4.Enabled = true;
      }
       
      private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
      {
      dataGridView1_CellClick(sender, e);
      }
      }
      

       

Leave A Reply

Your email address will not be published.