首页 > 解决方案 > 检索图像时出现“参数无效”错误

问题描述

我想使用 C# 从 SQL Server 数据库中检索图像。但是,我收到错误消息

参数无效

请帮我。

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
     string q = "select * from Rough where name='" + comboBox1.Text + "'";

     SqlConnection con = new SqlConnection("Data Source=(local);Initial Catalog=SMS_DB;Integrated Security=True");

     SqlCommand cmd = new SqlCommand(q, con);
     SqlDataReader dr;

    if (con.State == ConnectionState.Closed)
    {
        con.Open();
        dr = cmd.ExecuteReader();
        byte[] img = null;

        while (dr.Read())
        {
            textBox1.Text = (dr["ID"].ToString());
            textBox2.Text = (dr["Name"].ToString());
            textBox3.Text = (dr["Image"].ToString());
            img = (byte[])(dr["Image"]);

            if (img == null)
            {
                pictureBox2.Image = null;
            }
            else
            {
                MemoryStream ms = new MemoryStream(img);
                pictureBox2.Image = Image.FromStream(ms);
                // ms.Close();
            }
        }

        con.Close();
    }
}

标签: c#sql-server

解决方案


推荐阅读