首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用PostgreSql插入和检索C#图像

如何使用PostgreSql插入和检索C#图像
EN

Stack Overflow用户
提问于 2017-09-09 07:14:57
回答 1查看 10.7K关注 0票数 6

我试图在Postgres中插入一个图像,并使用C#从postgresql检索该图像。

我的表中有两列:memberid (字符变化)和member_photo (字节)

下面是插入图像的代码:

代码语言:javascript
运行
复制
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();
    }
}

以下是检索图像的代码

代码语言:javascript
运行
复制
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;
}

插入图像的代码运行良好,但无法在picturebox中显示此图像。

我收到一个错误:

参数无效。

请帮我解决这个问题

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-09-09 08:51:59

AFAIK您不能使用byte[]检索ExecuteScalar。您应该使用ExecuteReader代替。在插入参数时,为了安全起见,我更愿意自己指定类型,所以我的插入看起来如下:

代码语言:javascript
运行
复制
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();
    }
}

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

代码语言:javascript
运行
复制
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;
            }
        }
    }
}

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

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

票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46128132

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档