目前我从包含Guid的ASCII文件导入大量数据。 导入数据的示例行将是:
35313532-3200-0000-0000-000000000000,PRT100,MYCORP ENTERPRISES,...
问题在于SQL Server中的第一列映射为Guid。我收到以下错误: 数据源中String类型的给定值无法转换为指定目标列的uniqueidentifier类型。
专栏对我来说似乎没问题。任何人都知道如何将Guid的字符串表示导入真正的Guid?
这是有问题的功能:
private static void ImportBc(string sInputConnectionString,
string sOutputConnectionString,
string sInputTable,
string sOutputTable,
string[,] arrMap,
bool bDelete)
{
Console.WriteLine("Import datoteke {0}......", sInputTable);
if (bDelete)
{
var sqlCnn = new SqlConnection { ConnectionString = sOutputConnectionString };
sqlCnn.Open();
//var deleteCommand = new SqlCommand("TRUNCATE TABLE " + sOutputTable) { Connection = sqlCnn };
var deleteCommand = new SqlCommand("DELETE FROM " + sOutputTable) { Connection = sqlCnn };
deleteCommand.ExecuteNonQuery();
sqlCnn.Close();
}
string oledbConnectionString = sInputConnectionString;
var connection = new OleDbConnection(oledbConnectionString);
var command = new OleDbCommand(sInputTable, connection);
connection.Open();
// Create DbDataReader
using (DbDataReader dr = command.ExecuteReader())
{
// SQL Server Connection String
string sqlConnectionString = sOutputConnectionString;
// Bulk Copy to SQL Server
using (var bulkCopy = new SqlBulkCopy(sqlConnectionString))
{
bulkCopy.BatchSize = 10000;
bulkCopy.NotifyAfter = 10000;
bulkCopy.SqlRowsCopied += OnSqlRowsCopied;
bulkCopy.DestinationTableName = sOutputTable;
/* Zbog timeout expired problema */
bulkCopy.BulkCopyTimeout = 9000;
for (int i = 0; i < arrMap.GetLength(0); i++)
{
bulkCopy.ColumnMappings.Add(arrMap[i, 0], arrMap[i, 1]);
Console.WriteLine("{0} {1}", arrMap[i, 0], arrMap[i, 1]);
}
bulkCopy.WriteToServer(dr);
}
}
}
答案 0 :(得分:0)
您是否尝试过这种格式:'35313532-3200-0000-0000-000000000000',PRT100,MYCORP ENTERPRISES,......?
答案 1 :(得分:0)
几天前我发表了an answer to this question:
我在思考你应该做的两件事,
a)添加映射,但您似乎已正确添加它们:
// Let's fix tons of mapping issues by
// Setting the column mapping in SqlBulkCopy instance:
foreach (DataColumn dataColumn in _dataTable.Columns)
{
bulkCopy.ColumnMappings.Add(dataColumn.ColumnName, dataColumn.ColumnName);
}
b)设置KeepIdentity选项:
SqlBulkCopyOptions.KeepIdentity
查看你的代码我猜你的ArrMap没有要映射的所有列,为什么不试试我的Generic Bulk Insert类? (使用它就像在答案中解释的那样。
我希望找出错误来源有帮助。
涓