从DataGridView到数据库中插入数据 [英] inserting data from datagridview to database

查看:811
本文介绍了从DataGridView到数据库中插入数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一些现有的数据库中的数据和用户将输入新的数据到了我想添加到数据库中的datagridview



和我真的不知道知道如何只添加新的数据插入的行中的DataGridView到数据库中。我怎么只得到了新插入的数据?有人告诉我,我可以用tempID将其分配给新插入的行,但我对如何做到这一点不知道。 ?谁能给我一些链接到它



这是我的代码现在:

 私人无效的button1_Click(对象发件人,EventArgs五)
{
CON =新System.Data.SqlClient.SqlConnection();
con.ConnectionString =数据源= TCP:沉-PC,49172\\SQLEXPRESS;初始目录= LSEStock;集成安全性=真;
con.Open();
SqlDataAdapter的大=新的SqlDataAdapter();

的for(int i = 0; I< dataGridView1.Rows.Count-2;我++)
{

字符串insertData =INSERT INTO CostList(SupplierName, CostPrice,PartsID)VALUES(@SupplierName,@CostPrice,@PartsID);
的SqlCommand CMD =新的SqlCommand(insertData,CON);
cmd.Parameters.AddWithValue(@ SupplierName,dataGridView1.Rows [I] .Cells [0] .value的);
cmd.Parameters.AddWithValue(@ CostPrice,dataGridView1.Rows [I] .Cells [1]。价值);
cmd.Parameters.AddWithValue(@ PartsID,textBox1.Text);
da.InsertCommand = CMD;
cmd.ExecuteNonQuery();
}

con.Close();
}


解决方案

我觉得你应该接近DataGrid中插入不同。检查了这一点:



http://www.codeguru.com/csharp/.net/net_data/datagrid/article.php/c13041/Add-Edit-and-删除 - 在DataGridView.htm



我不认为允许地方工作,然后在这里插入所有新行是最好的办法,当电网能够让用户插入新行到数据库时,有数据绑定。该示例执行数据库调用异步释放的UI。



我也想你至少应该使用StringBuilder的为您查询;更易于管理,我希望你是不是真的要离开连接字符串有这样的。


There are some existing data in the database and users will be entering new data into the datagridview which I wanted to add into database.

And I really don't know how to add ONLY the new inserted rows of data in datagridview into the database. How do I get the new inserted data only? I was told that I can use tempID to assign it to the new inserted row, but I have no idea on how to do it. Can anyone give me some links to it?

This is my code for now:

private void button1_Click(object sender, EventArgs e)
{
    con = new System.Data.SqlClient.SqlConnection();
    con.ConnectionString = "Data Source=tcp:SHEN-PC,49172\\SQLEXPRESS;Initial Catalog=LSEStock;Integrated Security=True";
    con.Open();
    SqlDataAdapter da = new SqlDataAdapter();

    for (int i = 0; i<dataGridView1.Rows.Count-2; i++ )
    {

        String insertData = "INSERT INTO CostList(SupplierName, CostPrice, PartsID) VALUES (@SupplierName, @CostPrice, @PartsID)" ;
        SqlCommand cmd = new SqlCommand(insertData, con);
        cmd.Parameters.AddWithValue("@SupplierName", dataGridView1.Rows[i].Cells[0].Value);
        cmd.Parameters.AddWithValue("@CostPrice", dataGridView1.Rows[i].Cells[1].Value);
        cmd.Parameters.AddWithValue("@PartsID", textBox1.Text);
        da.InsertCommand = cmd;
        cmd.ExecuteNonQuery();
    }

    con.Close();
}

解决方案

I think you should approach the datagrid insert differently. check this out:

http://www.codeguru.com/csharp/.net/net_data/datagrid/article.php/c13041/Add-Edit-and-Delete-in-DataGridView.htm

I don't think allowing local work and then inserting all the new rows is the best approach here when the grid is able to let users insert new rows to the database when there is databound. the example does the database calls asynchronously to free the UI.

also I think you should at least use StringBuilder for your query; more manageable and I hope you are not really going to leave the connection string there like that.

这篇关于从DataGridView到数据库中插入数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆