Help with inserting bytea

Discussion of open issues, suggestions and bugs regarding ADO.NET provider for PostgreSQL
Post Reply
bigpoint

Help with inserting bytea

Post by bigpoint » Tue 23 Nov 2004 19:40

I have a string which I want to INSERT into a bytea field. I dont know how to convert the string into to correct format:

I tried this´but it does not work (column "content" is of type bytea but expression is of type integer)


com.CommandText = "INSERT INTO test (partner_id,autor_id,title,content) VALUES (@1,@2,@3,@4)"
com.Parameters.Add("@1", CoreLab.PostgreSql.PgSqlType.Int)
com.Parameters.Add("@2", CoreLab.PostgreSql.PgSqlType.Int)
com.Parameters.Add("@3", CoreLab.PostgreSql.PgSqlType.VarChar)
com.Parameters.Add("@4", CoreLab.PostgreSql.PgSqlType.ByteA)

dim b as byte()
b = Convert.FromBase64String("test")

com.Parameters("@1").Value = partner_id
com.Parameters("@2").Value = autoren_id
com.Parameters("@3").Value = titel
com.Parameters("@4").Value = b

com.Connection = dh.GetConnection()

com.ExecuteNonQuery()

Yuri
Posts: 140
Joined: Mon 08 Nov 2004 12:07

Post by Yuri » Thu 25 Nov 2004 09:46

PgSqlType.ByteA type corresponds to .NET type Byte().
To convert a string to Byte() type you can use conversion functions from
System.Text.Encoding class.

For example,

Code: Select all

    Imports System.Text
    ...
    Dim encoding As Encoding = encoding.Default
    Dim strValue As String = "text"
    Dim bytes As Byte() = encoding.GetBytes(strValue)
    ...
    pgSqlCommand.Parameters.Add("@1", CoreLab.PostgreSql.PgSqlType.ByteA)
    pgSqlCommand.Parameters("@1").Value = bytes
Last edited by Yuri on Mon 29 Nov 2004 08:39, edited 1 time in total.

Guest

Post by Guest » Sun 28 Nov 2004 13:09

...I tried your proposal but always get to message:

ERROR: column "inhalt" is of type bytea but expression is of type integer

Can you help?

Best regards,
Christoph

Yuri
Posts: 140
Joined: Mon 08 Nov 2004 12:07

Inserting bytea

Post by Yuri » Mon 29 Nov 2004 10:08

Probably, you try to insert integer value into Bytea field.
The same exception will be raised in the following example:

Code: Select all

CREATE TABLE test.blob_test
(
  id int8 NOT NULL,
  f_blob bytea,
  CONSTRAINT blob_test_pkey PRIMARY KEY (id)
) 
WITH OIDS;

Code: Select all

 Dim integerValue As Integer = 4
...
pgCommand.CommandText = "insert into test.blob_test(id, f_blob) values(12, :p1)"
pgCommand.Parameters.Add("p1", integerValue)
pgCommand.ExecuteNonQuery() ->Exception
This exception is raised:
'ERROR: column "f_blob" is of type bytea but expression is of type integer
'You will need to rewrite or cast the expression

Please check a type of PgSqlParameter value.

ckress

Post by ckress » Tue 14 Dec 2004 17:21

By updating to the latest driver version (v1.85) the problem magically disappeared ...
Thanks for your help!

Post Reply