简体   繁体   English

使用C#WinForm向SQL Server数据库添加批量值

[英]Adding bulk values to SQL Server database with c# winform

I'm using SQL Server 2008 Express. 我正在使用SQL Server 2008 Express。

What is the best way to import bulk values into the database from a C# app? 从C#应用程序将批量值导入数据库的最佳方法是什么?

Right now I have some textboxes and I can import data without any problem. 现在,我有一些文本框,可以毫无问题地导入数据。 And I would like to import not only one row ,by "click to a button" but 3 or 4. 我不仅想通过“单击按钮”导入一行,而且要导入3或4。

Please help me! 请帮我!

For 3 or 4 rows, I would suggest setting up a SqlCommand with a parameterized insert statement, creating and binding parameters for the inserted columns, and then calling ExecuteNonQuery . 对于3或4行,我建议使用参数化的insert语句设置SqlCommand ,为插入的列创建和绑定参数,然后调用ExecuteNonQuery You can retain the same SqlCommand and SqlParameters objects, reassign new values to the SqlParameter s, and then do the next ExecuteNonQuery for the next row. 您可以保留相同的SqlCommandSqlParameters对象,将新值重新分配给SqlParameter ,然后对下一行执行下一个ExecuteNonQuery

However, if you are doing a much larger insert (as in 100k rows at a time), you should look at using SqlBulkCopy - this allows for high performance bulk inserts (and only inserts) into a table. 但是,如果您要进行大得多的插入(一次插入10万行),则应考虑使用SqlBulkCopy这允许在表中进行高性能的大容量插入(并且仅插入)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM