繁体   English   中英

如何使用 NPGSQL.GetBytes() 函数

[英]How to use NPGSQL.GetBytes() function

在我的 Postgres 数据库中,我有一个 bytea 列。 在我的程序中,我可以插入到此列,但是,当我去检索该数据时,我得到

System.InvalidOperationException: '没有可用的行'

我可以访问该记录中的另一列,但是当我尝试以下任一方法时:

byte[] bytes = new byte[16];
bytes = (byte[])dr[3];


byte[] bytes = new byte[16];
dr.GetBytes(3, 0, bytes, 0, 16);

错误被抛出。 提前致谢。

编辑:添加了查询

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);
            }
        }

在此处输入图片说明

所解释的问题是由数据库中的错误引起的,而不是 NPGSQL 函数。 删除并重新创建表解决了这个问题。 以下是我认为从 bytea 列访问数据的可接受方式。

byte[] bytes = new byte[16];
dr.GetBytes(3, 0, bytes, 0, 16);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM