2017-08-08 2 views
1

データがたくさんあり(100000 +行)、データを複製したくない場合は、正しい構文は何ですか?テーブルにデータを追加するとどうなりますか?私の挿入データボタンがどのように見えるかSQL Serverでデータグリッドをインポートしたいが、データを複製したくない

は、これは次のとおりです。

任意の助けが理解されるであろう

private void button8_Click(object sender, EventArgs e) 
{ 
    string constring = @"DELETED FOR SECURITY REASONS"; 

    using (SqlConnection con = new SqlConnection(constring)) 
    { 
     for (int i = 0; i < dgvEmployees.Rows.Count - 1; i++) 
     { 
      var str = @"INSERT INTO USERSTable VALUES ('" + 
       dgvEmployees.Rows[i].Cells["Issuer"].Value + "', '" + 
       dgvEmployees.Rows[i].Cells["Customer"].Value + "'," + 
       dgvEmployees.Rows[i].Cells["Card"].Value + ",'" + 
       dgvEmployees.Rows[i].Cells["License plate No"].Value + 
       dgvEmployees.Rows[i].Cells["Transactiondate"].Value + "', '" +     dgvEmployees.Rows[i].Cells["Product description (com.)"].Value + "', '" + 
       dgvEmployees.Rows[i].Cells["Quantity"].Value + "', '" + 
       dgvEmployees.Rows[i].Cells["Gross CC"].Value + "', '" + 
       dgvEmployees.Rows[i].Cells["VAT1"].Value + "', '" + 
       dgvEmployees.Rows[i].Cells["Voucher"].Value + "', '" + 
       dgvEmployees.Rows[i].Cells["Mileage"].Value + "', '" + 
       dgvEmployees.Rows[i].Cells["Additional info"].Value + "', '" +     dgvEmployees.Rows[i].Cells["Supply country"].Value + "', '" +     dgvEmployees.Rows[i].Cells["Site Town"].Value + "', '" + 
       dgvEmployees.Rows[i].Cells["Product DEL"].Value + "', '" + 
       dgvEmployees.Rows[i].Cells["Unitprice"].Value + "', '" + 
       dgvEmployees.Rows[i].Cells["Amount"].Value + "', '" + 
       dgvEmployees.Rows[i].Cells["Discount"].Value + "', '" + 
       dgvEmployees.Rows[i].Cells["Surcharge"].Value + "', '" + 
       dgvEmployees.Rows[i].Cells["VAT"].Value + "', '" + 
       dgvEmployees.Rows[i].Cells["Suppliercurrency"].Value + "', '" +    dgvEmployees.Rows[i].Cells["Invoice No"].Value + "', '" + 
       dgvEmployees.Rows[i].Cells["Invoice date"].Value + "', '" + 
       dgvEmployees.Rows[i].Cells["Invoiced?"].Value + "', '" + 
       dgvEmployees.Rows[i].Cells["Vat2010"].Value + "', '" + 
       dgvEmployees.Rows[i].Cells["State"].Value + "', '" + 
       dgvEmployees.Rows[i].Cells["Supplier"].Value + "', '" + 
       dgvEmployees.Rows[i].Cells["Cost 1"].Value + "', '" + 
       dgvEmployees.Rows[i].Cells["Cost 2"].Value + "', '" + 
       dgvEmployees.Rows[i].Cells["Reference No"].Value + "', '" + 
       dgvEmployees.Rows[i].Cells["Recordtype"].Value + "', '" + 
       dgvEmployees.Rows[i].Cells["Amount other"].Value + "', '" + 
       dgvEmployees.Rows[i].Cells["Is listprice ?"].Value + "', '" +     dgvEmployees.Rows[i].Cells["Date to"].Value + "', '" + 
       dgvEmployees.Rows[i].Cells["Final Trx."].Value + "', '" + 
       dgvEmployees.Rows[i].Cells["LPI"].Value + "', '" + "');"; 

      try 
      { 
       using (SqlCommand com = new SqlCommand(str, con)) 
       { 
        con.Open(); 
        com.ExecuteNonQuery(); 
       } 
      } 
      catch (Exception ex) 
      { 
       MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); 
      } 

      con.Close(); 
     } 

     MessageBox.Show("Records uploaded."); 
    } 
} 
...

ありがとう...

+0

あなたの質問はただ読むことができません... – mauro

+0

データベース側でそれを処理します。一意の制約 – apomene

+2

[SQLインジェクションアラート](http://msdn.microsoft.com/en-us/library/ms161953%28v=sql.105%29.aspx)を追加してください。**決して結合してはなりません**このようなあなたのSQLステートメント。 **常に** SQLのインジェクションを避けるために**パラメータ化されたクエリ**を使用する - [Little Bobby Tables](https://xkcd.com/327/)をチェックしてください –

答えて

0

データベース上でそれを解決するためにチョイスした場合彼のコメントで言及されているように、ユニークな行にユニークな制約を追加するだけです。

は、ソフトウェア側の場合(あなただけ行ない編集、追加にユーザーを許可すれば)古い行が、その後カウント文のあなたにそれを使用続けることができます追加、編集、行またはを取得するためにDataTable.GetChangesを使用してのようにあまりにも多くのソリューションがあります以下のような:

for (int i = oldRowsCount-1; i < dgvEmployees.Rows.Count - 1; i++) 

EDIT:セキュリティ上の理由(ともそれは簡単です)私は強くSqlParameterCollection.AddWithValueを使用し、あなたのクエリにString.Formatのメソッドを使用するないあなたをreccomend。

編集2:パトリックのコメントをありがとう。「not」という言葉。

+0

*あなたの質問にString.Formatメソッドを使用することを強くお勧めします。*> 'string.Format'を使用することはできません。 –

関連する問題