我如何在Windows窗体中从Datagridview更新数据库

问题描述:

我如何在windows窗体中从datagridview更新数据库,我尝试更新数据库中的表,但数据库中没有任何更改,为什么数据库没有更改,我使用的是visual studio 2012



How i can update database from datagridview in windows form, i try to update the table in database but nothing is changed in the database, why the databse is not changed, i am using visual studio 2012

public partial class Form1 : Form
{
    SqlConnection con;
    SqlDataAdapter adap;
    DataSet ds;
    SqlCommandBuilder scb;
    SqlCommand scmd;

    public Form1()
    {
        InitializeComponent();
        load_student();
    }

    private void load_student()
    {
        try
        {
            con = new SqlConnection();
            con.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["test_update.Properties.Settings.dbupdateConnectionString"].ConnectionString; ;
            con.Open();
            string sql = "select * from student";
            adap = new SqlDataAdapter(sql, con);
            ds = new System.Data.DataSet();
            adap.Fill(ds, "student");
            dataGridView1.DataSource = ds.Tables[0];
            }
        catch (Exception ex)
        {
            MessageBox.Show("Error! " + ex.Message);
        }
        finally
        {
            con.Close();
        }     
    }
     
    private void update_btn_Click(object sender, EventArgs e)
    {
        try
        {
            /*scb = new SqlCommandBuilder(adap);
            adap.Update(ds, "student");*/
            string sql = "update student set name='ali' where id=2";
            scmd = new SqlCommand(sql, con);
            
            con.Open();
            scmd.ExecuteNonQuery();
            MessageBox.Show("Updating is had been successfully");
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error " + ex.Message);
        }
        finally
        {
            con.Close();
            scb = new SqlCommandBuilder(adap);
            adap.UpdateCommand = scmd;
            adap.Update(ds, "student");
            ds.AcceptChanges();
            load_student();
        }
    }
     
    private void button1_Click(object sender, EventArgs e)
    {
        con = new SqlConnection();
        con.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["test_update.Properties.Settings.dbupdateConnectionString"].ConnectionString; ;
        string sql1 = "delete from student where id=1";
        scmd = new SqlCommand(sql1, con);
        try
        {
            con.Open();
            scmd.ExecuteNonQuery();
            MessageBox.Show("Successfully deleting");
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        finally
        {
            con.Close();
            load_student();
        }
    }
}

我已经运行了你的代码,对我来说已经更新了数据库。



请检查您的表名,连接是否正确。

检查您要更新的数据是是否存在于DB中。
I have run your code, for me it has updated the DB.

Please check your table name, connection sting is it correct or not.
Check once that which data you are updating is present in DB or not.


我认为您修复了查询中的条件:



I think you are fixed the condition in your query :

"update student set name='ali' where id=2";




"delete from student where id=1";





你应该使用类似的东西:

如果你需要更新一个记录所选的一个用途:



you should use something like that :
if you need to update one recorde the selected one use :

int k = dataGridView1.CurrentRow.Index;




"update student set name='"+dataGridView1.Rows[k].Cells["the cell name"].Value.ToString()+"' where id="+dataGridView1.Rows[k].Cells["the number of the cell or the cell id name"].Value





如果你需要更新它们你应该使用循环

for(int i = 0; i< datagridview1.rows.count; i ++)>

和这里的工作



if you need to update them all you should use loop
for(int i =0;i<datagridview1.rows.count;i++)>
and the work here