Wednesday, 4 September 2013

Bulk Insert DataTable To SQL Database


This DLL provide method to insert multiple data record in bulk.

You can insert DataTable object into SQL Table. The Only convention required is, the DataTable must have the same number of columns and of same datatype as in SQL Table.

You need to just keep the order of columns same as that of SQL Table while columns name can be anything i.e. You can keep the name of columns in DataTable as par your convenience.
How to use
You must have add this DLL to your project first. Then, create an instance of it for example:
Insert srt = new Insert();
Their after, you need to prepare your data in the form of DataTable, example:
DataTable dt = new DataTable();
dt.Columns.Add("id", typeof(string));
dt.Columns.Add("pwd", typeof(string));
dt.Columns.Add("email", typeof(string));
dt.Columns.Add("code", typeof(string));

DataRow dr = dt.NewRow();
dr["id"] = "yuor id";
dr["pwd"] = "your password;
dr["email"] = "your email;
dr["code"] = your code";
dt.Rows.Add(dr);

DataRow dr1 = dt.NewRow();
dr1["id"] = "yuor id1";
dr1["pwd"] = "your password1;
dr1["email"] = "your email1;
dr1["code"] = your code1";
dt.Rows.Add(dr1);

DataRow dr2 = dt.NewRow();
dr2["id"] = "yuor id2";
dr2["pwd"] = "your password2;
dr2["email"] = "your email2;
dr2["code"] = your code2";
dt.Rows.Add(dr2);
Now we are inthe condition to insert our date into SQL Table with the help of "BulkInsert" DLL.
To insert Data, your code should look like this:
srt.BulkInsert(string Destination_Table_Name, DataTable Your_DataTable, string ConnectionString);
Now have look on code at all together
Insert srt = new Insert();
DataTable dt = new DataTable();
dt.Columns.Add("id", typeof(string));
dt.Columns.Add("pwd", typeof(string));
dt.Columns.Add("email", typeof(string));
dt.Columns.Add("code", typeof(string));

DataRow dr = dt.NewRow();
dr["id"] = "yuor id";
dr["pwd"] = "your password;
dr["email"] = "your email;
dr["code"] = your code";
dt.Rows.Add(dr);

DataRow dr1 = dt.NewRow();
dr1["id"] = "yuor id1";
dr1["pwd"] = "your password1;
dr1["email"] = "your email1;
dr1["code"] = your code1";
dt.Rows.Add(dr1);

DataRow dr2 = dt.NewRow();
dr2["id"] = "yuor id2";
dr2["pwd"] = "your password2;
dr2["email"] = "your email2;
dr2["code"] = your code2";
dt.Rows.Add(dr2);
srt.BulkInsert("tableName", dt, ConnectionString);

4 comments: