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()
Help with inserting bytea
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,
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.
Inserting bytea
Probably, you try to insert integer value into Bytea field.
The same exception will be raised in the following example:
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.
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
'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.