本文介绍了Postgres的bytea的列返回,而不是字节数组的字符串(char数组)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直用C#编写我们对不同的数据库产品的具体提供者实现。 W /出进入的细节,其中一列是字节数组类型(bytea的在Postgres - 由于喜好BYTEA被选择了BLOB)。唯一的问题是,它不返回插入相同的值。当我插入的Int32(0),我得到8 92倍和8倍48](而不是[0,0,0,0])。我需要一个性能明智的解决方案,这将在8个字节返回,而不是值0的ASCII表示我已经插入纯字节。

I have been using C# to write a concrete provider implementation for our product for different databases. W/out getting into details, one of the columns is of byte array type (bytea in postgres - due to the preferences bytea was chosen over blob). The only problem, is that it does not return same value that was inserted. When I insert Int32 ("0") I get 8 [92 and 8x 48] (instead of [0,0,0,0]). I need a performance wise solution, that will return pure bytes I have inserted, instead of ASCII representation of value "0" on 8 bytes.

我使用Npgsql以检索数据。如果有人知道C#的解决方案,我会很乐意去学习它,以及

I am using Npgsql to retrive data. If someone knows solution for c# I will be happy to learn it as well.

编辑:
的Postgres 9.0,NET 3.5的

Postgres 9.0, .Net 3.5

简化

命令查询: - 里面只做一个插入说明书

Command query: - inside it only does an insert statment

select InsertOrUpdateEntry(:nodeId, :timeStamp, :data)

数据参数:

byte [] value = BitConverter.GetBytes((int)someValue);



参数被分配如下

Parameter is assigned as below

command.Parameters.Add(new NpgsqlParameter("data", NpgsqlDbType.Bytea) 
{ Value = value });

选择statments:

Select statments:

select * from Entries

我已经进入了相同的字节数组,我想背部。我会很感激你的帮助。

Same byte array I have entered, I want to get back. I would really appreciate your help.

输入:0 0 0 0

电流输出:92 48 48 48 48 48 48 48 48

的预期输出:0 0 0 0

Input: 0 0 0 0
Current Output: 92 48 48 48 48 48 48 48 48
Expected Output: 0 0 0 0

推荐答案

在Npgsql存在的类检索插入的行,例如:

In Npgsql there is NpgsqlDataReader class to retrieve inserted rows, e.g:

NpgsqlConnection conn = new NpgsqlConnection(connStr);
conn.Open();

NpgsqlCommand insertCmd =
    new NpgsqlCommand("INSERT INTO binaryData (data) VALUES(:dataParam)", conn);
NpgsqlParameter param = new NpgsqlParameter("dataParam", NpgsqlDbType.Bytea);

byte[] inputBytes = BitConverter.GetBytes((int)0);
Console.Write("Input:");
foreach (byte b in inputBytes)
    Console.Write(" {0}", b);
Console.WriteLine();

param.Value = inputBytes;
insertCmd.Parameters.Add(param);
insertCmd.ExecuteNonQuery();

NpgsqlCommand selectCmd = new NpgsqlCommand("SELECT data FROM binaryData", conn);
NpgsqlDataReader dr = selectCmd.ExecuteReader();
if(dr.Read())
{
    Console.Write("Output:");
    byte[] result = (byte[])dr[0];
    foreach(byte b in result)
        Console.Write(" {0}", b);
    Console.WriteLine();
}

conn.Close();



从C#应用程序结果:

Result from C# app:

Input: 0 0 0 0
Output: 0 0 0 0

从pgAdmin的结果:

Result from pgAdmin:

"\000\000\000\000"

编辑:

我发现解释为什么你越来越:

I found explanation why you getting:

92 48 48 48 48 48 48 48 48

我检查我的代码是以前的版本 Npgsql2.0.10-bin-ms.net3.5sp1.zip 并获得上述结果(当然pgAdmin的回报率 \000\000\000\000 ),所以我觉得最你可以做的是使用另一种版本,而这个bug

I checked my code with previous version Npgsql2.0.10-bin-ms.net3.5sp1.zip and get above result (of course pgAdmin returns \000\000\000\000), so I think that best what you can do is to use another version without this bug.

答:用户更高版本Npgsql比2.0.10

ANSWER: User higher version of Npgsql than 2.0.10

这篇关于Postgres的bytea的列返回,而不是字节数组的字符串(char数组)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 09:52