[英]How to use NPGSQL.GetBytes() function
In my Postgres database, I have a bytea column.在我的 Postgres 数据库中,我有一个 bytea 列。 In my program, I can insert to this column, however, when I go to retrieve that data I get在我的程序中,我可以插入到此列,但是,当我去检索该数据时,我得到
System.InvalidOperationException: 'No row is available' System.InvalidOperationException: '没有可用的行'
I can access the other column in that record but when I try either of these methods:我可以访问该记录中的另一列,但是当我尝试以下任一方法时:
byte[] bytes = new byte[16];
bytes = (byte[])dr[3];
byte[] bytes = new byte[16];
dr.GetBytes(3, 0, bytes, 0, 16);
The error gets thrown.错误被抛出。 Thanks in advance.提前致谢。
EDIT: Added the query编辑:添加了查询
string sql = "SELECT * FROM as_users WHERE name = @name";
if (OpenConnection())
{
NpgsqlCommand npgsqlCommand = new NpgsqlCommand(sql, Connection);
npgsqlCommand.Parameters.AddWithValue("name", username);
npgsqlCommand.Prepare();
NpgsqlDataReader dr = npgsqlCommand.ExecuteReader();
if (dr.Read())
{
//Doesnt work
byte[] bytes = new byte[16];
dr.GetBytes(3, 0, bytes, 0, 16);
//Does work
string name = dr.GetString(1);
}
}
The issues explained were caused by an error within the database, not the NPGSQL function.所解释的问题是由数据库中的错误引起的,而不是 NPGSQL 函数。 Dropping and recreating the table solved the issue.删除并重新创建表解决了这个问题。 Below is what I believe is an acceptable way to access data from a bytea column.以下是我认为从 bytea 列访问数据的可接受方式。
byte[] bytes = new byte[16];
dr.GetBytes(3, 0, bytes, 0, 16);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.