SQL BulkCopy
-
Hi, I need to import data from a CSV file to an SQL table i have used tohe following in the past to import an excel file but wondered if/how i can adapt it to import from a CSV. Also, it would be a great help if you could show me how to convert it to use column mapping.
string excelConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\qs_upload.xls;Extended Properties=""Excel 8.0;HDR=YES;""";
// Create Connection to Excel Workbook using (OleDbConnection connection = new OleDbConnection(excelConnectionString)) { OleDbCommand command = new OleDbCommand("Select \* FROM \[quality\_standard$\]", connection); //OleDbCommand command = new OleDbCommand("Select qs\_Id, qs\_Organisation, Convert(varchar,qs\_Start,111), qs\_End FROM \[quality\_standard$\]", connection); connection.Open(); // Create DbDataReader to Data Worksheet using (OleDbDataReader dr = command.ExecuteReader()) { // SQL Server Connection String string sqlConnectionString = "Data Source=wibblewobble.com;Initial Catalog=comfound;Persist Security Info=True;User ID=##########;Password=##########"; // Bulk Copy to SQL Server using (SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlConnectionString)) { bulkCopy.DestinationTableName = "tbl\_QualityStandard"; bulkCopy.WriteToServer(dr); } } }
Thanks.
-
Hi, I need to import data from a CSV file to an SQL table i have used tohe following in the past to import an excel file but wondered if/how i can adapt it to import from a CSV. Also, it would be a great help if you could show me how to convert it to use column mapping.
string excelConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\qs_upload.xls;Extended Properties=""Excel 8.0;HDR=YES;""";
// Create Connection to Excel Workbook using (OleDbConnection connection = new OleDbConnection(excelConnectionString)) { OleDbCommand command = new OleDbCommand("Select \* FROM \[quality\_standard$\]", connection); //OleDbCommand command = new OleDbCommand("Select qs\_Id, qs\_Organisation, Convert(varchar,qs\_Start,111), qs\_End FROM \[quality\_standard$\]", connection); connection.Open(); // Create DbDataReader to Data Worksheet using (OleDbDataReader dr = command.ExecuteReader()) { // SQL Server Connection String string sqlConnectionString = "Data Source=wibblewobble.com;Initial Catalog=comfound;Persist Security Info=True;User ID=##########;Password=##########"; // Bulk Copy to SQL Server using (SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlConnectionString)) { bulkCopy.DestinationTableName = "tbl\_QualityStandard"; bulkCopy.WriteToServer(dr); } } }
Thanks.
Your question is not properly visible.please post it properly.
himanshu