在创建新数据库后,C# 中的批量插入工作。后失败。始终在控制台中工作
Bulk Insert in c# works after creating fresh db. Fails after. Always works in console
这是我正在执行的 SQL 命令:
DECLARE @SQLString nvarchar(4000);
SET @SQLString = N'BULK INSERT events FROM '+ QUOTENAME(@p0) +' WITH ( FIELDTERMINATOR = ''|'', ROWTERMINATOR =''\n'' )'
EXECUTE sp_executesql @SQLString
参数@p0为csv文件路径,加上command.parameters.AddWithValue
当我删除数据库并让我的程序从头开始创建它然后写入它时,这工作正常。
但是,当我关闭并重新打开我的应用程序并使用现在存在的数据库时,它失败了。
错误是:
{"Bulk load data conversion error (type mismatch or invalid character
for the specified codepage) for row 1, column 4 (Timestamp).\r\nBulk
load data conversion error (type mismatch or invalid character for the
specified codepage) for row 2, column 4 (Timestamp).\r\nBulk load data
conversion error (type mismatch or invalid character for the specified
codepage) for row 3, column 4 (Timestamp).\r\nBulk load data
conversion error (type mismatch or invalid character for the specified
codepage) for row 4, column 4 (Timestamp).\r\nBulk load data
conversion error (type mismatch or invalid character for the specified
codepage) for row 5, column 4 (Timestamp).\r\nBulk load data
conversion error (type mismatch or invalid character for the specified
codepage) for row 6, column 4 (Timestamp).\r\nBulk load data
conversion error (type mismatch or invalid character for the specified
codepage) for row 7, column 4 (Timestamp).\r\nBulk load data
conversion error (type mismatch or invalid character for the specified
codepage) for row 8, column 4 (Timestamp).\r\nBulk load data
conversion error (type mismatch or invalid character for the specified
codepage) for row 9, column 4 (Timestamp).\r\nBulk load data
conversion error (type mismatch or invalid character for the specified
codepage) for row 10, column 4 (Timestamp).\r\nBulk load data
conversion error (type mismatch or invalid character for the specified
codepage) for row 11, column 4 (Timestamp).\r\nCannot bulk load
because the maximum number of errors (10) was exceeded.\r\nThe OLE DB
provider \"BULK\" for linked server \"(null)\" reported an error. The
provider did not give any information about the error.\r\nCannot fetch
a row from OLE DB provider \"BULK\" for linked server \"(null)\"."}
当我在 sql 控制台中通过查询使用完全相同的 sql 命令时,它可以完美运行。
奇怪的是 (Timestamp) 不是第 4 列。它实际上是 csv 中的第一列。为什么完全相同的查询在 C# 中与控制台一起工作,但只是有时(使用新数据库)?
感谢@JohnBevan
事实上,当我创建数据库时,我也更改了目录。但是当我使用已经存在的版本时,我没有调用 ChangeDatabase...
这是我正在执行的 SQL 命令:
DECLARE @SQLString nvarchar(4000);
SET @SQLString = N'BULK INSERT events FROM '+ QUOTENAME(@p0) +' WITH ( FIELDTERMINATOR = ''|'', ROWTERMINATOR =''\n'' )'
EXECUTE sp_executesql @SQLString
参数@p0为csv文件路径,加上command.parameters.AddWithValue
当我删除数据库并让我的程序从头开始创建它然后写入它时,这工作正常。
但是,当我关闭并重新打开我的应用程序并使用现在存在的数据库时,它失败了。
错误是:
{"Bulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 1, column 4 (Timestamp).\r\nBulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 2, column 4 (Timestamp).\r\nBulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 3, column 4 (Timestamp).\r\nBulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 4, column 4 (Timestamp).\r\nBulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 5, column 4 (Timestamp).\r\nBulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 6, column 4 (Timestamp).\r\nBulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 7, column 4 (Timestamp).\r\nBulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 8, column 4 (Timestamp).\r\nBulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 9, column 4 (Timestamp).\r\nBulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 10, column 4 (Timestamp).\r\nBulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 11, column 4 (Timestamp).\r\nCannot bulk load because the maximum number of errors (10) was exceeded.\r\nThe OLE DB provider \"BULK\" for linked server \"(null)\" reported an error. The provider did not give any information about the error.\r\nCannot fetch a row from OLE DB provider \"BULK\" for linked server \"(null)\"."}
当我在 sql 控制台中通过查询使用完全相同的 sql 命令时,它可以完美运行。
奇怪的是 (Timestamp) 不是第 4 列。它实际上是 csv 中的第一列。为什么完全相同的查询在 C# 中与控制台一起工作,但只是有时(使用新数据库)?
感谢@JohnBevan
事实上,当我创建数据库时,我也更改了目录。但是当我使用已经存在的版本时,我没有调用 ChangeDatabase...