私はcsvファイルを持っていて、それをグリッドビューに挿入して表示しています。その後、私はそれをデータベースに格納し、gridviewからのすべてのレコードを格納する必要があります。複数のレコードをgridviewからSQL Serverテーブルに追加する方法はありますか。
だから、ボタン上の私は、次のコードを使用していますクリックしてください:
using (SqlConnection scn = new SqlConnection("Data Source=(local); Database='WaselProject'; Integrated Security = yes;"))
{
scn.Open();
string query = "INSERT INTO Wasel_Card (RECORD, CARD_ID, FULL_NAME, SERVICE_ID, PROC_COUNT, READ_COUNT) VALUES (@record, @cardid, @fullname, @serviceid, @proccount, @readcount)";
using (SqlCommand scm = new SqlCommand(query, scn))
{
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
scm.Parameters.AddWithValue("@record", dataGridView1.Rows[i].Cells["Record"].Value.ToString());
scm.Parameters.AddWithValue("@cardid", dataGridView1.Rows[i].Cells["CARD_ID"].Value.ToString());
scm.Parameters.AddWithValue("@fullname", dataGridView1.Rows[i].Cells["FULL_NAME"].Value.ToString());
scm.Parameters.AddWithValue("@serviceid", dataGridView1.Rows[i].Cells["SERVICE_ID"].Value.ToString());
scm.Parameters.AddWithValue("@proccount", dataGridView1.Rows[i].Cells["PROC_COUNT"].Value.ToString());
scm.Parameters.AddWithValue("@readcount", dataGridView1.Rows[i].Cells["READ_COUNT"].Value.ToString());
scm.ExecuteNonQuery();
}
}
}
が、私はエラーを取得:
Additional information: The variable name '@record' has already been declared. Variable names must be unique within a query batch or stored procedure.
任意の助けを?
ありがとうございます。
for文はforループ内にあるべきですか?それは新しいものを開始せず、同じパラメーターを複数回割り当てようとしていますか? – detellda
DGVにそのテーブルに基づくデータソースがある場合、そのコードは必要ありません。 DataAdapter – Plutonix