使用.CSV
时,无法将数据从SqlBulkCopy
文件导入多个SQL Server表中。
正在导入单个表。
我正在尝试SqlBulkCopy
,因为在我阅读过的大多数主题中,它都说这是最快的解决方案,我每天需要将1-2百万行之间的数据导入8个表中。
在此先感谢您的推荐。
namespace SQLBulkDT2012
{
public class CsvBulkCopyDataIntoSqlServer
{
public static void Main(string[] args)
{
LoadCsvDataIntoSqlServer();
Console.WriteLine("Done!");
Console.ReadKey();
}
static string GetPath()
{
string filePath = Path.Combine(Environment.CurrentDirectory, "test.csv");
return (filePath);
}
static void LoadCsvDataIntoSqlServer()
{
// This should be the full path
string filePath = GetPath();
bool IsSuccessSave = false;
SqlTransaction _transaction = null;
using (var textFieldParser = new TextFieldParser(filePath))
{
textFieldParser.TextFieldType = FieldType.Delimited;
textFieldParser.Delimiters = new[] { "," };
textFieldParser.HasFieldsEnclosedInQuotes = true;
var connectionString = ConfigurationManager.ConnectionStrings["SQLBulkDT2012.Properties.Settings.`CMSConnectionString`"].ConnectionString;
var dataTable = new DataTable("Temp_Table");
// Add the columns in the temp table
// 001
dataTable.Columns.Add("COLUMN_001");
// 002
dataTable.Columns.Add("COLUMN_002");
// 003
dataTable.Columns.Add("COLUMN_003");
// 004
dataTable.Columns.Add("COLUMN_004");
using (var sqlConnection = new SqlConnection(connectionString))
{
sqlConnection.Open();
using (_transaction = sqlConnection.BeginTransaction())
{
// Truncate the live table
// Create the bulk copy object
using (SqlBulkCopy sqlBulkCopy001 = new SqlBulkCopy(sqlConnection, SqlBulkCopyOptions.KeepIdentity, _transaction))
{
sqlBulkCopy001.BatchSize = 99999;
sqlBulkCopy001.DestinationTableName = "SY001_tst";
// Setup the column mappings, anything ommitted is skipped
// 001
sqlBulkCopy001.ColumnMappings.Add("COLUMN_001", "COLUMN_001");
// 002
sqlBulkCopy001.ColumnMappings.Add("COLUMN_002", "COLUMN_002");
sqlBulkCopy001.WriteToServer(Table001);
}
// Create the bulk copy object
using (SqlBulkCopy sqlBulkCopy002 = new SqlBulkCopy(sqlConnection, SqlBulkCopyOptions.KeepIdentity, _transaction))
{
sqlBulkCopy002.BatchSize = 99999;
sqlBulkCopy002.DestinationTableName = "SY002_tst";
// Setup the column mappings, anything ommitted is skipped
// 001
sqlBulkCopy002.ColumnMappings.Add("COLUMN_003", "COLUMN_003");
// 002
sqlBulkCopy002.ColumnMappings.Add("COLUMN_004", "COLUMN_004");
sqlBulkCopy002.WriteToServer(Table002);
}
_transaction.Commit();
IsSuccessSave = true;
}
sqlConnection.Close();
}
}
}
}
}