我有一個datagridview,並在表中的一些數據。 在DataGridView允許用戶輸入數據的新行如何從datagridview向數據庫中插入新添加的數據行?
的問題在這裏:
我想知道我如何能得到數據的新插入的行(無論多少行已添加)到數據庫中,而不添加將被複制的現有數據。
有人嗎?
編輯:即時通訊使用SQL數據庫,這是我的datagridview。
顯示的數據是已經在數據庫裏面,現在,如果用戶插入什麼數據的新的多行插入的datagridview?我應該放什麼代碼?
我的代碼如下:
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; 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();
你能提供有關要添加什麼類型的數據多一點細節,什麼是依託數據庫? – Moop 2012-08-08 02:14:49