参数不能通过blob访问数据库中的图像。

时间:2020-12-22 16:33:08

I'm creating project in c# desktop application. I want to add functionality like when I select row from datagrid view then image from database is shown into pichure box. but there is some error like :

我在c#桌面应用程序中创建项目。我想添加一些功能,比如从datagrid视图中选择row,然后从数据库中显示图像到pichure框中。但是有一些错误,比如:

"Parameter is not valid"

“参数是无效的”

my code is ..

我的代码是. .

private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            foreach (DataGridViewRow row in dataGridView1.SelectedRows)
            {
                // display content
                string value1 = row.Cells[0].Value.ToString();
                string value2 = row.Cells[1].Value.ToString();
                label2.Text = value1;
                label4.Text = value2;

            //Display Image
            SqlConnection cn = new SqlConnection();
            string str = "Data Source=.\\SQLEXPRESS;AttachDbFilename=D:\\PROJECT\\NEW\\CASTING CALCULATING SYSTEM\\CASTING CALCULATING SYSTEM\\DB_CASTING.MDF;Integrated Security=True;Connect Timeout=30;User Instance=True;";
            cn.ConnectionString = str;
            SqlCommand cmd = new SqlCommand ();
            cmd.Connection = cn;
            string strsql = "select image from EmpMaster WHERE Fname = '" +value2+ "'";
            cmd.CommandText = strsql ;
            cn.Open();
            SqlDataReader dr;

            try
            {

              dr = cmd.ExecuteReader();
              if (dr.Read())
              {
                 byte[] picarr = (byte[])dr["image"];
                 MemoryStream ms = new MemoryStream(picarr);
                 ms.Seek(0, SeekOrigin.Begin);
                 pictureBox1.Image = Image.FromStream(ms);
              }
            }
            catch (Exception ex)
            {
               MessageBox.Show(ex.Message);
            }
            finally
            {
              cn.Close();
            }
        }

    }

1 个解决方案

#1


0  

It is most likely a memory cap issue, see this thread for an example.

它很可能是一个内存上限问题,请参见该线程的示例。

So what that says is basically that the issue is on the Image side, not your SQL. It might be different reasons than pure memory, there are many reasons the picture constructor could fail.

这说明问题是在图像方面,而不是SQL。这可能与纯粹的内存不同,有很多原因导致图片构造函数失败。

Also, DJ KRAZE is right, you need to do some work on your SQL queries. As a minimum use parameters (significantly reduces the risk of SQL injection attacks) and put that SqlConnection and SqlDataReader into a using block to ensure they are properly disposed.

另外,DJ KRAZE是正确的,你需要在你的SQL查询上做一些工作。作为一个最小的使用参数(显著降低SQL注入攻击的风险),并将SqlConnection和SqlDataReader放入一个using块中,以确保它们得到适当的处理。

#1


0  

It is most likely a memory cap issue, see this thread for an example.

它很可能是一个内存上限问题,请参见该线程的示例。

So what that says is basically that the issue is on the Image side, not your SQL. It might be different reasons than pure memory, there are many reasons the picture constructor could fail.

这说明问题是在图像方面,而不是SQL。这可能与纯粹的内存不同,有很多原因导致图片构造函数失败。

Also, DJ KRAZE is right, you need to do some work on your SQL queries. As a minimum use parameters (significantly reduces the risk of SQL injection attacks) and put that SqlConnection and SqlDataReader into a using block to ensure they are properly disposed.

另外,DJ KRAZE是正确的,你需要在你的SQL查询上做一些工作。作为一个最小的使用参数(显著降低SQL注入攻击的风险),并将SqlConnection和SqlDataReader放入一个using块中,以确保它们得到适当的处理。