Tuesday, November 16, 2010

Insert multiple rows from gridview into database

you can use Sql bulk copy for this purpose

private void StartImport()
{
SqlBulkCopy bulkCopy = new SqlBulkCopy("Server=ServerName;Database=test;Trusted_Connection=True;",
SqlBulkCopyOptions.TableLock);
bulkCopy.DestinationTableName = "dest table Name";

DataTable dt = CreateDataTableFromFile();
bulkCopy.WriteToServer(dt);
}


private DataTable CreateDataTableFromFile()
{

//create a datatable and fill it with the values from the grridview

return the Datatable;
}


No comments:

Post a Comment