我正在研究ASP.NET MVC/C#應用程序。使用存儲過程將多行插入到SQL Server 2005中
我有一個對象列表可以說,從CSV導入的學生說List<Student>
。
我想使用存儲過程和事務將它們插入到SQL Server 2005數據庫中。如果一行失敗,它應該回滾。
有什麼建議嗎?
我正在研究ASP.NET MVC/C#應用程序。使用存儲過程將多行插入到SQL Server 2005中
我有一個對象列表可以說,從CSV導入的學生說List<Student>
。
我想使用存儲過程和事務將它們插入到SQL Server 2005數據庫中。如果一行失敗,它應該回滾。
有什麼建議嗎?
你使用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();
}
你可以做到這一點有兩種方式
請檢查該
How to insert xml data into table in sql server 2005
請檢查該
How to insert xml data into table in sql server 2005
感謝, 阿南德
要插入列表到數據庫,請檢查這個http://stackoverflow.com/questions/12426300/how-to-insert-list-from-c-sharp-into-sql-server-2008 –
尼斯 - 所以,儘管編寫一些代碼! –