-1

I am working on a ASP.NET MVC / C# application.

I have a list of objects lets say List<Student> students that were imported from a CSV.

I want to insert them into a SQL Server 2005 database using stored procedures and transactions. In case a row fails it should rollback.

Any suggestions?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
AdrianD
  • 279
  • 4
  • 20

2 Answers2

0

You can do it in two ways

  1. Bulk insert using Bulk Insert command (from files to DB)

Please check this

How to insert xml data into table in sql server 2005

  1. You can generate XML and then insert.

Please check this

How to insert xml data into table in sql server 2005

Thanks, Anand

Community
  • 1
  • 1
  • To insert list to database, please check this http://stackoverflow.com/questions/12426300/how-to-insert-list-from-c-sharp-into-sql-server-2008 – Anand Gangadhara Jan 12 '15 at 15:32
0

Do you use ADO or Entity?

You can use transactions from your code. For ADO

try
{
    sqlTransaction = sqlConnection.BeginTransaction();

    //call insert in loop

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

For Entity

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

    //call insert in loop

    entityTransaction.Commit();
}
catch{
    entityTransaction.Rollback();
}
Giorgi Nakeuri
  • 35,155
  • 8
  • 47
  • 75