Problem inserting Bytea in postgreSQL

0

I made this update to update a column in my company table, my column in the database is of type bytea and I am saving an image as a byte [] ... in my company class, I have the variable is of type Byte [] Foto

 public int Update(clsEmpresa E)
  {
     int r = 0;
     string sql = @"update empresa set emp_figura = ?;";

 string connString = String.Format("Server={0};Port={1};User Id={2};Password={3};Database={4};",
 clsConfigBanco.SERVERNAME, clsConfigBanco.PORT, clsConfigBanco.USERNAME, clsConfigBanco.PASSWORD, clsConfigBanco.DATABASENAME);

 using (NpgsqlConnection conexao = new NpgsqlConnection(connString))
 {
     conexao.Open();
     using (NpgsqlCommand cmd = new NpgsqlCommand(sql, conexao))
     {
        NpgsqlParameter param = new NpgsqlParameter("emp_figura", 
        NpgsqlTypes.NpgsqlDbType.Bytea); 
        param.Value = (E.Foto == null ? null : (E.Foto.Length == 0 ? null : E.Foto));
        cmd.Parameters.Add(param);
        r = cmd.ExecuteNonQuery();
     }
     conexao.Close();
 }

     return r;
}

When I run this method the following error occurs ERROR: 42601: syntax error at or near ";" when I do cmd.ExecuteNonQuery;

Would the error be referring to using NpgsqlTypes.NpgsqlDbType.Bytea related to the type of my parameter? What would be the correct way to save this byte array in the database?

    
asked by anonymous 12.09.2017 / 15:00

1 answer

1

switch

"update empresa set emp_figura = ?;"

by

"update empresa set emp_figura = :emp_figura;"

I would put a where there with the primary key of the company as well. Even if I only have one, I think it's better.

? is used when using an ODBC driver, at least postgresql . Now with Npgsql , you name the parameter in the query, with the prefix :

    
12.09.2017 / 15:05