How to insert bulk values in to a table by using C#?

The below method is used to bulk insert in to a table by using c# from a datatable.

using System.Data;
using System.Data.SqlClient;

public int BulkInsert(DataTable dt, string TableName, string ConnectionString)
{
int returnValue = -1;
SqlConnection oConn = new SqlConnection(ConnectionString);
oConn.Open();
SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM " + TableName,oConn);
da.SelectCommand.Connection = oConn;
DataSet ds = new DataSet();
SqlCommandBuilder oCmd = new SqlCommandBuilder(da);
try
{
da.Fill(ds);
returnValue = da.Update(dt);
}
catch (Exception ex)
{
throw ex;
}
finally
{
oConn.Close();
oCmd.Dispose();
}
return returnValue;
}


...S.VinothkumaR.

2 comments:

SteveBrad said...

I am simply out of words after reading your blog. I want to appreciate the way you handled such a complicated subject.
Buy Caverta
buy Edegra
Buy Forzest
Buy generic Levitra
Online Generic Viagra

Manish said...

Nice post...... A big task in a very simple word
Dot Net World