c# - 如何使用 C# 从 PostgreSql 插入和检索图像

标签 c# sql postgresql

我正在尝试将图像插入 Postgres 并使用 C# 从 postgresql 检索该图像。

我的表中有两列: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;
}

插入图片的代码工作正常,但我无法在图片框中显示该图片。

我得到一个错误:

Parameter is not valid

请帮我解决这个问题

最佳答案

据我所知,您无法使用 ExecuteScalar 检索 byte[]。您应该改用 ExecuteReader。为了安全起见,在插入参数时,我更喜欢自己指定类型,所以我的插入看起来像这样:

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();
    }
}

然后我可以像这样检索和加载图像:

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;
            }
        }
    }
}

我不知道在插入时指定数据类型是否有任何区别,所以先尝试使用 Reader 进行检索。如果这不起作用,那么我建议将您的插入例程更改为类似我的例程。

请注意,在我的示例中,id 是一个整数,而不是一个字符变量!

关于c# - 如何使用 C# 从 PostgreSql 插入和检索图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46128132/

相关文章:

sql - 您如何创建和更新 SQL 脚本?

sql - 汇总重叠段以测量有效长度

postgresql - 在 Postgres 中使用 INSERT INTO 与关联

wordpress - 无法使用Docker部署带有Postgres数据库的Wordpress

c# - 从 Roslyn 的 goto 案例中获取符号

c# - 从 C# 调用 FORTRAN 子例程

c# - 在 Windows 窗体 (C#) 中使用一个声明设置多个属性

sql - 帮助 SQL 查询吗?

python - PostgreSQL 和 Python

c# - 当我运行我的网站时, GridView 没有出现