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を使用してバイトを取得することはできません私の知る限り、私はこの問題
あなたの返信ありがとうございます。同じエラーが再び発生しました。パラメータが無効です – Sivashankar
@Sivashankar私のようにあなたのインサートを変更しようとしましたか? –
@Sivashankarどの行がエラーをスローしますか? –