我正在嘗試使用由SQL Server管理導入導出嚮導創建的平面文件中的SqlBulkCopy
類執行批量插入。這些文件用逗號分隔。在該文件中用SqlBulkCopy插入GUID
一行可以是這樣的:
{DCAD82A9-32EC-4351-BEDC-2F8291B40AB3},,{ca91e768-072d-4e30-aaf1-bfe32c24008f},900001:1792756,900001:1792757,basladdning,2011-04-29 02:54:15.380000000,basladdning,2011-04-29 02:54:15.380000000,{20A3C50E-8029-41DE-86F1-DDCDB9A78BA5}
我得到的錯誤是:
System.InvalidOperationException was unhandled
Message=The given value of type String from the data source cannot be converted to type uniqueidentifier of the specified target column.
所以,問題是從字符串轉換爲GUID。
我曾嘗試以下:
- 嘗試了不同的編碼,UTF8和ANSI
- 刪除了{}周圍的GUIDS
- 加入 '' 圍繞GUIDS,有和沒有{}
有什麼建議嗎?在''的平面文件中,是一個也具有Guid作爲數據類型的列。在嚮導將其導出爲''時,該列爲NULL。這可能是問題嗎?
代碼:
using (var file = new StreamReader(filePath))
using (var csv = new CsvReader(file, false))
using (var bcp = new SqlBulkCopy(CreateConnectionString()))
{
bcp.DestinationTableName = "table";
bcp.WriteToServer(csv);
}
表定義:
CREATE TABLE [beata].[T_FeatureInstance](
[FeatureInstanceId] [uniqueidentifier] NOT NULL,
[FeatureInstanceParentId] [uniqueidentifier] NULL,
[FeatureTypeAliasId] [uniqueidentifier] NOT NULL,
[Uuid] [varchar](1000) NOT NULL,
[VersionId] [varchar](50) NOT NULL,
[SkapadAv] [varchar](255) NULL,
[Skapad] [datetime] NOT NULL,
[UppdateradAv] [varchar](255) NOT NULL,
[Uppdaterad] [datetime] NOT NULL,
[Gs] [uniqueidentifier] NOT NULL,
顯示一些代碼,請 –
檢查列和它們的類型的數量匹配您的CSV。看起來DB Guid列正在更新來自csv的非guid數據。據我所知,微軟聲明花括號在GUID字符串中是有效的。 – Artemix
@Artemix幾乎肯定是正確的。這將是值得發佈您的目標表DDL。 –