Wednesday, April 10, 2013

Bulk insert from sharepoint to SQL

Hi

I am introducing bulk reocord from sharepoint 2010 to SQL server. Please follow the steps and modify the code as per your bussiness recuirment.

1. Create one data table which would be your source table

DataTable table = new DataTable();
table.Columns.Add(
"ID",typeof(int));table.Columns.Add(
"Name", typeof(string));table.Columns.Add(
"Address", typeof(string));table.Columns.Add(
"Phone", typeof(string));table.Columns.Add(
"Phone1", typeof(string));
 loop through 100000 item to fill data table

for (int i = 0; i <= 100000; i++){
table.Rows.Add(i,
"raj", "GangaVihar", "9873773", "2324234");}

SQLHelper.BulkUpdateExecuteNonQuery(connectionStr, table);
public static int BulkUpdateExecuteNonQuery(string connectionString, DataTable dt){

if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString");
using (SqlConnection connection = new SqlConnection(connectionString)){
connection.Open();

using (SqlBulkCopy copy = new SqlBulkCopy(connection)){
copy.ColumnMappings.Add(
"ID", "ID");copy.ColumnMappings.Add(
"Name", "Phone");copy.ColumnMappings.Add(
"Address", "Name");copy.ColumnMappings.Add(
"Phone", "Phone1");
// copy.ColumnMappings.Add(4, 4);copy.DestinationTableName = "TestDeliverable";copy.WriteToServer(dt);
}

return 1;
//return ExecuteNonQuery(connection, commandType, commandText, commandParameters);}
}