2015-01-12 182 views
-1

我正在研究ASP.NET MVC/C#应用程序。使用存储过程将多行插入到SQL Server 2005中

我有一个对象列表可以说,从CSV导入的学生说List<Student>

我想使用存储过程和事务将它们插入到SQL Server 2005数据库中。如果一行失败,它应该回滚。

有什么建议吗?

+2

尼斯 - 所以,尽管编写一些代码! –

回答

0

你使用ADO或实体?

您可以使用代码中的交易。 对于ADO

try 
{ 
    sqlTransaction = sqlConnection.BeginTransaction(); 

    //call insert in loop 

    sqlTransaction.Commit(); 
} 
catch{ 
    sqlTransaction.Rollback(); 
} 

对于实体

try 
{ 
    entityTransaction = Context.Connection.BeginTransaction(); 

    //call insert in loop 

    entityTransaction.Commit(); 
} 
catch{ 
    entityTransaction.Rollback(); 
}