gpt4 book ai didi

c# - SqlBulkInsert 和 Guid 问题

转载 作者:行者123 更新时间:2023-11-30 18:42:40 25 4
gpt4 key购买 nike

目前我正在从包含 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);
}
}
}

最佳答案

您是否尝试过这种格式:'35313532-3200-0000-0000-000000000000',PRT100,MYCORP ENTERPRISES, ...?

关于c# - SqlBulkInsert 和 Guid 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5377931/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com