2016-12-17 5 views
1

私は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.

任意の助けを?

ありがとうございます。

+0

for文はforループ内にあるべきですか?それは新しいものを開始せず、同じパラメーターを複数回割り当てようとしていますか? – detellda

+0

DGVにそのテーブルに基づくデータソースがある場合、そのコードは必要ありません。 DataAdapter – Plutonix

答えて

3

あなたの問題は、ループの中でパラメータを定義し続けていることです。同じコマンドについて同じパラメータを複数回定義することはできません。あなたの問題を解決する2つの方法。ループ内のあなたのパラメータに

方法1

コールClear()。 2

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(); 
    scm.Parameters.Clear(); 
} 

メソッドは、ループ内の値を追加しますが、ループの外に一度、あなたのパラメータを宣言します。

// Define here just once 
var paramRecord = scm.Parameters.Add("@record", System.Data.SqlDbType.Variant); // There are 6 Add methods in total. 5 of them return SqlParameter so you can use any of those 5 
// the other parameters 
for (int i = 0; i < dataGridView1.Rows.Count; i++) 
{ 
    paramRecord.Value = dataGridView1.Rows[i].Cells["Record"].Value.ToString()); 
    // other parameter values here 
    scm.ExecuteNonQuery(); 
} 
+0

の2番目のメソッドを使用して更新するだけです。paramRecord.Value .Valueに赤色(下線)の下線が表示されますか?@ CodingYoshi – ahmad

+0

編集を参照してください。将来、興味のあるメソッド(この場合はAdd)にカーソルを置くとF12を押すだけで、使用しているメソッドに移動します。私が使用していたメソッドは、コレクション内のパラメータのint(インデックス)を返すことで、 'scm.Parameters [返されたint値] .Valueを使用するようにしました。今私は、SqlParameterオブジェクトを返す別のメソッドを使用して、それはValueプロパティを持っています。 – CodingYoshi

+0

@ahmadkはうまくいきましたか? – CodingYoshi

関連する問題