datagridviewがあり、テーブルにいくつかのデータがあります。datagridviewを使用すると、ユーザーは新しい行のデータを入力できます
ここでの質問:
複製される既存のデータを追加せずに、(追加された行数に関係なく)新しく挿入されたデータの行をデータベースに取得する方法を知りたいです。
誰?
編集:SQLデータベースを使用しているim。これは私の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();