gpt4 book ai didi

c# - 08P01 : insufficient data left in message for Nullable DateTime

转载 作者:太空宇宙 更新时间:2023-11-03 21:11:22 27 4
gpt4 key购买 nike

我有一个数据库表,其中有一列定义为 timestamp without time zone。现在,在我的 C# 应用程序中,当我尝试使用 NpgSql BeginBinaryImport 在该列中插入空值时,它会给出如下所述的错误消息:

08P01: insufficient data left in message

下面是我要执行的代码:

static void Main(string[] args)
{
BulkInsert();
}

private static void BulkInsert()
{

DataTable table = new DataTable();
table.Columns.Add("firstname", typeof(String));
table.Columns.Add("lastname", typeof(String));
table.Columns.Add("logdatetime", typeof(DateTime));
table.Columns.Add("status", typeof(int));
table.Columns.Add("id", typeof(long));

var dataRow = table.NewRow();
dataRow["firstname"] = "MyFirstName";
dataRow["lastname"] = "MyLastName";
dataRow["logdatetime"] = DBNull.Value;
dataRow["status"] = 1;
dataRow["id"] = 10;
table.Rows.Add(dataRow);

var data = new DataAccess();

using (var npgsqlConn = new NpgsqlConnection([ConnectionString]))
{
npgsqlConn.Open();
var commandFormat = string.Format(CultureInfo.InvariantCulture, "COPY {0} {1} FROM STDIN BINARY", "logging.testtable", "(firstName,LastName,logdatetime,status,id)");
using (var writer = npgsqlConn.BeginBinaryImport(commandFormat))
{
foreach (DataRow item in dataTable.Rows)
{
writer.StartRow();
foreach (var item1 in item.ItemArray)
{
writer.Write(item1);
}

}
}

npgsqlConn.Close();
}

最佳答案

问题是您尝试编写的 DBNull.Value - Npgsql 不支持以这种方式编写空值。要写入 null,您需要改用 WriteNull() 方法。

我可以让 Npgsql 接受 DBNull.Value,但仅限于 Write() 的重载,它也接受 NpgsqlDbType(因为 Npgsql 必须写入数据类型,对于 DBNull.Value,我们不知道那是什么)。

编辑:已完成此操作,请参阅 https://github.com/npgsql/npgsql/issues/1122 .

关于c# - 08P01 : insufficient data left in message for Nullable DateTime,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37431054/

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