【问题标题】:I want to import data grid in SQL Server, but I don't want to duplicate data我想在 SQL Server 中导入数据网格,但我不想复制数据
【发布时间】:2017-08-08 08:23:56
【问题描述】:

如果我有大量数据(超过 100000 行)并且我不想在向表中添加更多数据时重复数据,那么正确的语法是什么?

这是我的插入数据按钮的样子:

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.");
    }
}

任何帮助将不胜感激...

谢谢...

【问题讨论】:

  • 你的问题简直看不懂……
  • 在数据库端处理它。添加唯一约束
  • SQL Injection alert - 你应该永远不要像这样连接你的SQL语句; 总是使用 参数化查询 来避免 SQL 注入 - 请查看 Little Bobby Tables
  • 我知道我不应该这样做,但我的老板特意问了我这个问题,所以我别无选择,只能像这样连接所有内容。
  • 完全不是你问的,但你为什么要为每一行打开和关闭该连接?您可能只想打开它,进行更新然后关闭它。

标签: c# sql-server visual-studio


【解决方案1】:

如果您选择在数据库端解决它,只需将唯一约束添加到唯一行,如他评论中提到的 apomene。

对于软件方面,有太多解决方案,例如使用 DataTable.GetChanges 来添加和编辑行,或者(如果您只是让用户添加行而不是编辑)您可以保留旧的行数在你的 for 语句中使用它,例如:

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

编辑:出于安全原因(也更容易)我强烈建议您不要为您的查询使用 String.Format 方法,使用SqlParameterCollection.AddWithValue。 p>

Edit2:感谢帕特里克的评论,我错过了“不是”这个词。

【讨论】:

  • 我强烈建议您使用 String.Format 方法进行查询 > 我希望您的意思是使用string.Format
猜你喜欢
  • 2015-06-13
  • 2014-02-11
  • 2013-12-13
  • 1970-01-01
  • 1970-01-01
  • 2023-03-09
  • 2011-05-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多