简体   繁体   English

通过Ado.Net 3.5和C#多次插入SQL Server 2008

[英]Multiple insert into SQL Server 2008 via Ado.Net 3.5 and C#

I have a collection of data. 我有数据收集。 I need to insert these records into SQL Server 2008. I also need to maintain transaction, so that if one insert fails, I need to rollback all the changes done. 我需要将这些记录插入SQL Server2008。我还需要维护事务,因此,如果一次插入失败,则需要回滚所有已完成的更改。

Can some one suggest a best way to do this? 有人可以建议这样做的最佳方法吗? I use .Net 3.5. 我使用.Net 3.5。

You need to create a transaction using your SqlConnection object's BeginTransaction method. 您需要使用SqlConnection对象的BeginTransaction方法创建事务。 This will return an SqlTransaction object that you need to keep track of. 这将返回您需要跟踪的SqlTransaction对象。

When all of your items have been successfully inserted into the database, you will call Commit on the transaction object. 将所有项目成功插入数据库后,您将在事务对象上调用Commit。

If an exception is encountered during your processing, you will need to call Rollback on the transaction object. 如果在处理期间遇到异常,则需要在事务对象上调用回滚。

Roughly speaking, the code is: 大致来说,代码是:

SqlConnection conn;
// Create and open your connection here

SqlTransaction trans = conn.BeginTransaction();
try {
   // Perform your inserts

   trans.Commit();
} catch(Exception ex) {
   trans.Rollback();

   // Report the exception
}

阅读有关TransactionScope和System.Transactions的全部信息。

Depending of wheather you are using ADO.NET/plain SQL you can use bulk insert, or, if you are using Linq-to-SQL you can try using LINQ Entity Data Reader to insert large amounts of data at once. 根据使用的是ADO.NET/普通SQL,可以使用批量插入,或者,如果使用的是Linq-to-SQL,则可以尝试使用LINQ实体数据读取器一次插入大量数据。

By using bulk insert each batch is copied to the server as one transaction. 通过使用批量插入,每个批次将作为一个事务复制到服务器。 If this fails, SQL Server commits or rolls back the transaction for every batch. 如果失败,SQL Server将为每个批处理提交或回滚事务。

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

相关问题 如何使用ADO.Net C#将null插入到sql数据库中 - how to insert null into sql database with ADO.Net C# 多次插入事务连接超时-ADO.NET-SQL Server - Multiple Insert transaction connection timeout - ADO.NET - SQL Server 通过C#使用ado.net将值插入SQL Server数据库 - Inserting values into a SQL Server database using ado.net via C# 通过ado.net从C#调用SQL Server存储过程超时 - Calling SQL Server stored procedure from C# via ado.net times out 通过ADO.NET的Oracle和SQL Server上的C#事务失败 - C# Transactions on Oracle & SQL Server via ADO.NET failing C#ADO.NET SQL Server连接 - C# ADO.NET SQL Server connection 关于将SQL Server连接到C#(使用ADO.NET) - About connecting sql server to c# (with ADO.NET) 如何使用C#winform与带有ADO.NET实体的SQL Server 2008建立远程连接 - How to make a remote connection with SQL Server 2008 with ADO.NET entity Data model from C# winform 如何使用C#ADO.Net从多行SQL Server插入命令获取输出? - How to get output from SQL Server Insert Command with more than one row using C# ADO.Net? ADO.NET,SQL Server,C#:在数据库中执行插入之前如何检查某个参数是否不存在 - ADO.NET, SQL Server, C#: how to check if a certain parameter does not exist before performing an Insert in a database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM