Chào mừng đến với Diễn đàn lập trình - Cộng đồng lập trình.
Kết quả 1 đến 7 của 7
  1. #1
    Ngày tham gia
    Sep 2015
    Bài viết
    0

    Nhập, thêm, sửa, xóa dữ liệu trên DataGridView cơ bản nhất

    Tutorial
    Unit two
    (Part 1: Do with Sql Server 2000)

    This tutorial for Programmer. This tells about to access Database in Visual Studio (Csharp ). Use DataGridView with action Connect to data about as: add, delete, update, save and search basic.

    <font size="4">1. With Sql Server 2000
    Note: I used Sql Server 2000 with server name =(local), user =sa; password= sa. You change info your server to run the application.
    The first you design the application Windows Form like :



    <font color="Blue">* And you make a database like as:

    Database’s Name = Mydata
    The tables : Demo
    This Demo have 4 record : ID, Name, Pass, Noteone
    There in ID is the Keyword

    * More information you database


    Now, We code for button Show – this use to show data in your date into dataGridView.

    //
    Code:

    Mã:
                        // show data from Database ( Mydata) to DataGridView                // Connect to server                 string connection = "server=(local);database=Mydata;user=sa;password=sa";                SqlConnection con = new SqlConnection(connection);                con.Open();                // Make command to use                SqlCommand cmd = new SqlCommand();                cmd.Connection = con;                string show = "select*from Demo";                cmd.CommandText = show;                //                SqlDataAdapter da = new SqlDataAdapter(show, con);                DataSet ds = new DataSet();                // Fill date to ds                da.Fill(ds);                // Fill Dato from DataSet to DataGridView                dataGridView1.DataSource = ds.Tables[0];                // Closed Connect                con.Dispose();                da.Dispose();            cmd.Dispose();
    b. Next – We will code for button ADD.
    You click to button2 ( this has name ADD in the your application ) then copy and paste this code underhearth.
    Code:

    Mã:
                    // Connect to server                string connection = "server=(local);database=Mydata;user=sa;password=sa";                SqlConnection con = new SqlConnection(connection);                con.Open();                // Make command to use                                           //*************** code here for all ****************************//                string ADD = "insert into Demo(ID,Name,Pass,Noteone) values('"+textBox1.Text+"','"+textBox2.Text+"','"+textBox3.Text+"','"+textBox4.Text+"')";                SqlCommand cmd = new SqlCommand();                cmd.Connection = con;                cmd.CommandText = ADD;                cmd.ExecuteNonQuery();                MessageBox.Show("Succesfull, good do", "Ok. you add Info to you data");                //***********************                              // Closed Connect                con.Dispose();                cmd.Dispose();                // clearn textbox;
    c. Delete the record in data. Delete at record ID
    Code:

    Mã:
                    // Delete info you data which has ID= textBox1.Text                 // show data from Database ( Mydata) to DataGridView                // Connect to server                string connection = "server=(local);database=Mydata;user=sa;password=sa";                SqlConnection con = new SqlConnection(connection);                con.Open();                // Make command to use                  //*************** code here for all ****************************//                string Delete = "delete from Demo where ID='"+textBox1.Text+"'";                 SqlCommand cmd = new SqlCommand();                cmd.Connection = con;                cmd.CommandText = Delete;                cmd.ExecuteNonQuery();                MessageBox.Show("Succesfull, good do. You delete ID= "+textBox1.Text+" in you data", "Ok. you Delete Info to you data");                //***********************                 // Closed Connect                con.Dispose();                cmd.Dispose();                // clearn textbox;
    d. UPDATE data following ID
    Code:

    Mã:
                    // update info which has ID = textBox1;                // show data from Database ( Mydata) to DataGridView                // Connect to server                string connection = "server=(local);database=Mydata;user=sa;password=sa";                SqlConnection con = new SqlConnection(connection);                con.Open();                // Make command to use                  //*************** code here for all ****************************//                string updatedata = "update Demo set Name='" + textBox2.Text + "',Pass='"+textBox3.Text+"',Noteone='"+textBox4.Text+"' where ID='"+textBox1.Text+"'";                 SqlCommand cmd = new SqlCommand();                cmd.Connection = con;                cmd.CommandText =updatedata;                cmd.ExecuteNonQuery();                MessageBox.Show("Succesfull, good do. You update ID= " + textBox1.Text + " in you data", "Ok. you Updatedata Info to you data");                //***********************                 // Closed Connect                con.Dispose();                cmd.Dispose();
    e. Find data which two types
    e.1. Fill fidelity at textbox key
    Code:

    Mã:
                    // Find info in you data                // show data from Database ( Mydata) to DataGridView                // Connect to server                string connection = "server=(local);database=Mydata;user=sa;password=sa";                SqlConnection con = new SqlConnection(connection);                con.Open();                // Make command to use                  //*************** code here for all ****************************//                string finddata = "select*from Demo where Name='" + textBox5.Text + "'";                 SqlCommand cmd = new SqlCommand();                cmd.Connection = con;                cmd.CommandText = finddata;                cmd.ExecuteNonQuery();                MessageBox.Show("Succesfull, good do. You find info about Name= " + textBox5.Text + " in you data", "Ok. you find Info to you data");                //***********************                // Show data find                 SqlDataAdapter da = new SqlDataAdapter(finddata, con);                DataSet ds = new DataSet();                da.Fill(ds);                dataGridView1.DataSource = ds.Tables[0];                 // Closed Connect                con.Dispose();                cmd.Dispose();                ds.Dispose();                da.Dispose();
    e.2. Find relative at the key word
    Code:

    Mã:
                    // Connect to server                string connection = "server=(local);database=Mydata;user=sa;password=sa";                SqlConnection con = new SqlConnection(connection);                con.Open();                // Make command to use                  //*************** code here for all ****************************//                string finddata = "select*from Demo where Name like '%" + textBox5.Text + "%'";                 SqlCommand cmd = new SqlCommand();                cmd.Connection = con;                cmd.CommandText = finddata;                cmd.ExecuteNonQuery();                MessageBox.Show("Succesfull, good do. You find info about like  " + textBox5.Text + " in you data", "Ok. you find Info to you data");                //***********************                // Show data find                 SqlDataAdapter da = new SqlDataAdapter(finddata, con);                DataSet ds = new DataSet();                da.Fill(ds);                dataGridView1.DataSource = ds.Tables[0];                 // Closed Connect                con.Dispose();                cmd.Dispose();                ds.Dispose();                da.Dispose();                // clearn textbox;




    hình minh họa cho bài 1

    Chúc vui

    Source code full and Demo here:
    2. With Access 2003 ( the last)
    3. Other (the last)
    4. End ( the last).

    Bổ sung: http://www.mediafire.com/?sharekey=3...43906a5faff527
    [IMG]images/smilies/17.gif[/IMG]</font></font>

  2. #2
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    bài viết hay nhưng mình xin góp ý thế này
    Trong C# có hỗ trợ phần thêm xóa sửa đối với SQL rồi mà
    code thế này các bạn tham khảo nha
    Thêm

    Mã:
                    DataRow dongmoi = bangdl.NewRow();                dongmoi["MALOAIPHONG"] = txt_maloaiphong.Text;                dongmoi["LOAIPHONG"] = txt_loaiphong.Text;                dongmoi["GIATIEN"] = txt_giatien.Text;                bangdl.Rows.Add(dongmoi);                data.Update(bangdl);
    Sửa

    Mã:
            DataRow editRow =myTable.Rows[pos];        editRow["MAKHOA"] = txtMakhoa.Text;        editRow["TENKHOA"] = txtTenkhoa.Text;        myDataAdapter.Update(myTable);
    xóa

    Mã:
            myTable.Rows[pos].Delete();        myDataAdapter.Update(myTable);
    loại bỏ sửa 1 dòng

    Mã:
    myTable.RejectChanges();
    pos là dòng hiện thời , được xác định trong sự kiện khi click vào 1 dong trong Grid

  3. #3
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Điều gì xảy ra nếu em nhập vào trường dữ liệu noteone một chuỗi văn bản hơn 50 ký tự?

  4. #4
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Trích dẫn Gửi bởi sca_romeo
    Điều gì xảy ra nếu em nhập vào trường dữ liệu noteone một chuỗi văn bản hơn 50 ký tự?
    demo csdl như thế nào ?
    --> Chắc bạn giải quyết được rùi chứ ?
    [IMG]images/smilies/1.gif[/IMG]

  5. #5
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    C# 2005 trở về trước thì hỗ trợ chức năng thêm,xóa ,sửa..
    nhưng hình như c# 2008 không còn hỗ trợ chức năng này nữa đúng ko bạn?
    nếu còn thì bạn có thể chỉ rõ nó nằm ở đâu cho mình được ko ? Cảm ơn nhiều..

  6. #6
    Ngày tham gia
    Sep 2015
    Đang ở
    24 Rạch Bùng Binh , P10,Q3 , HCM
    Bài viết
    0
    Cái này làm thô quá. Nếu đưa vào 1 class thì thật tuyệt

  7. #7
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    cho mình hỏi code xóa ghi ở đâu z?trong button hả.mình viết mà ko chạy

 

 

Quyền viết bài

  • Bạn Không thể gửi Chủ đề mới
  • Bạn Không thể Gửi trả lời
  • Bạn Không thể Gửi file đính kèm
  • Bạn Không thể Sửa bài viết của mình
  •