Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Bytea reading problem!!

Status
Not open for further replies.

Ikado

Programmer
Jan 28, 2005
4
TR
Hi,
I am developing an application on c# .net2005 using postgres.
I have created a table named "data" it has 8 fields and all field types are bytea, because i am receiving byte arrays from TCP/IP socket, after i parsed them,i am insertnig byte arrays to my table "data"..
the values i received and inserted are ;
To 1. Field :00 // 1 byte
To 2. Field :04 // 1 byte
To 3. Field :A3 // 1 byte
To 4. Field :00 // 1 byte
.....
.....
.....

So far so good,

But when i tried to read data from table using;


public void ReadDataFromDatabase()
{
string ConnectionString = "Server=localhost;Port=5432;User Id=Ikado;Password=muhammet;Database=" + DatabaseName + ";";
NpgsqlConnection conn = new NpgsqlConnection(ConnectionString);
conn.Open();
IDbCommand cmd = null;
IDataReader reader = null;
try
{
cmd = conn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = SQL;// SQL= "SELECT * FROM data"
cmd.Connection = conn;
reader = cmd.ExecuteReader();
reader.Read();
//it allows me at least 3 byte
//otherwise i am getting an error message
//telling there is no t enough space at PoolData
//array
byte[] PoolData = new byte[3];
//BytetoHex Function converts given byte array to
// hexadecimal string
while (reader.Read())
{
reader.GetBytes(1, 0, PoolData, 0, 1);
MessageBox.Show(ByteToHex(PoolData));
// Result : 30 30 20
reader.GetBytes(2, 0, PoolData, 0, 1);
MessageBox.Show(ByteToHex(PoolData));
// Result : 30 34 20
reader.GetBytes(3, 0, PoolData, 0, 1);
MessageBox.Show(ByteToHex(PoolData));
// Result : 41 33 20
reader.GetBytes(4, 0, PoolData, 0, 1);
MessageBox.Show(ByteToHex(PoolData));
// Result : 30 30 20
}
}
}

I am inserting only one byte but when i tried to read i am receiving 3 bytes per field, what is wrong?
Can you help me ??
 
Is this a postgresql-question?

Does it work with another database as expected?
Can you reproduce the error in plain sql?


Code:
 hex..........dez..........ascii
30 30 20 ... 48 48 32 ... "00 "
30 34 20 ... 48 52 32 ... "04 "
41 33 20 ... 65 51 32 ... "A3 "
30 30 20 ... 48 48 32 ... "00 "

My documentation says (datatype-binary.html):
bytea 4 bytes plus the actual binary string variable-length binary string
So maybe you're inserting allways 3 bytes when you think it's just one.

don't visit my homepage:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top