2017-09-09 22 views
2

Postgresに画像を挿入し、その画像をC#でpostgresqlから取得しようとしています。C#を使用してPostgreSQLから画像を挿入および取得する方法

私は、テーブル内の2つの列があります。memberid(文字変化)とmember_photo(bytea型)ここで

画像を挿入するために私のコードです:ここでは

using (FileStream pgFileStream = new FileStream("D:\\Capture.jpg", FileMode.Open, FileAccess.Read)) 
{ 
    using (BinaryReader pgReader = new BinaryReader(new BufferedStream(pgFileStream))) 
    { 
     NpgsqlCommand command = new NpgsqlCommand(); 

     byte[] ImgByteA = pgReader.ReadBytes(Convert.ToInt32(pgFileStream.Length)); 
     command.CommandText = "insert into membermaster (memberid, member_photo) VALUES ('65', @Image)"; 
     command.Connection = Program.conn; 

     Program.conn.Close(); 
     Program.conn.Open(); 

     //command.Parameters.Add(new NpgsqlParameter("Image", ImgByteA)); 
     command.Parameters.Add("@Image", ImgByteA).Value = ImgByteA; 

     command.ExecuteNonQuery(); 

     Program.conn.Close(); 
    } 
} 

は、画像を取得するために私のコードです

NpgsqlCommand command = new NpgsqlCommand(); 
command.CommandText = "select member_photo from membermaster where memberid='65'"; 
command.Connection = Program.conn; 

try 
{ 
    Program.conn.Close(); 
    Program.conn.Open(); 

    byte[] productImageByte = command.ExecuteScalar() as byte[]; 

    if (productImageByte != null) 
    { 
     using (MemoryStream productImageStream = new System.IO.MemoryStream(productImageByte)) 
     { 
      ImageConverter imageConverter = new System.Drawing.ImageConverter(); 
      pictureBox1.Image = imageConverter.ConvertFrom(productImageByte) as System.Drawing.Image; 
      // image.Save(imageFullPath, System.Drawing.Imaging.ImageFormat.Jpeg); 

      pictureBox1.Image = System.Drawing.Image.FromStream(productImageStream); 
     } 
    } 
} 
catch 
{ 
    Program.conn.Close(); 
    throw; 
} 

画像を挿入するコードは正常ですが、この画像を画像ボックスに表示することはできません。

私はエラーを取得する:

パラメータは

有効ではありませんあなたは[]は、ExecuteScalarを使用してバイトを取得することはできません私の知る限り、私はこの問題

答えて

2

を解決してください。代わりにExecuteReaderを使用する必要があります。

using (var conn = new NpgsqlConnection(connString)) 
{ 
    string sQL = "SELECT photo from picturetable WHERE id = 65"; 
    using (var command = new NpgsqlCommand(sQL, conn)) 
    { 
     byte[] productImageByte = null; 
     conn.Open(); 
     var rdr = command.ExecuteReader(); 
     if (rdr.Read()) 
     { 
      productImageByte = (byte[])rdr[0]; 
     } 
     rdr.Close(); 
     if (productImageByte != null) 
     { 
      using (MemoryStream productImageStream = new System.IO.MemoryStream(productImageByte)) 
      { 
       ImageConverter imageConverter = new System.Drawing.ImageConverter(); 
       pictureBox1.Image = imageConverter.ConvertFrom(productImageByte) as System.Drawing.Image; 
      } 
     } 
    } 
} 

using (var conn = new NpgsqlConnection(connString)) 
{ 
    string sQL = "insert into picturetable (id, photo) VALUES(65, @Image)"; 
    using (var command = new NpgsqlCommand(sQL, conn)) 
    { 
     NpgsqlParameter param = command.CreateParameter(); 
     param.ParameterName = "@Image"; 
     param.NpgsqlDbType = NpgsqlTypes.NpgsqlDbType.Bytea; 
     param.Value = ImgByteA; 
     command.Parameters.Add(param); 

     conn.Open(); 
     command.ExecuteNonQuery(); 
    } 
} 

私は、このように画像を取得して読み込むことができます:ちょうど私の挿入は次のようになりますので、パラメータを挿入するとき、私は種類を自分で指定することを好む安全側にするには挿入時にデータ型を指定すると違いがあるのか​​どうかわからないので、まずReaderを使用して取得してみてください。それがうまくいかない場合は、私のようなものにあなたの挿入ルーチンを変更することをお勧めします。

私の例では、idは整数であり、可変文字ではありません。

+0

あなたの返信ありがとうございます。同じエラーが再び発生しました。パラメータが無効です – Sivashankar

+0

@Sivashankar私のようにあなたのインサートを変更しようとしましたか? –

+0

@Sivashankarどの行がエラーをスローしますか? –

関連する問題