从数据库获取二进制数据时参数无效异常

时间:2016-11-16 10:09:09

标签: c# sql blob sqldatareader sqlcommand

我有一个名为" Student"以及名为" GeneralStudent"的表格。该表只包含两列名为" Id"和"照片"。我已经成功地将一些数据插入到" GeneralStudent"表。 Photos值是二进制数据。但是当我想通过以下代码检索图像时,会出现一条消息:

  

参数无效。

我该如何解决这个问题?

private void searchButton_Click(object sender, System.EventArgs e)
{
    SqlConnection con1 =new SqlConnection(@"server=RATHIN-PC\SQLEXPRESS;database=Student; integrated security=true");
    con1.Open();

    SqlDataReader myReader = null;
    SqlCommand myCommand = new SqlCommand( "select photo from GeneralStudent where id='" + searchTextBox.Text + "'", con1);

    if (searchTextBox.Text == "")
    {
        MessageBox.Show("Please Enter Studet Id.\nYou Entered Null Value\nThank You", "Error Message Window", MessageBoxButtons.OK,MessageBoxIcon.Error);
    }
    else
    {
        myReader = myCommand.ExecuteReader();
        myReader.Read();

        if (myReader.HasRows)
        {
            byte[] img = (byte[])(myReader["photo"]);

            if (img == null)
            {
                pictureBox3.Image = null;
            }
            else
            {
                MemoryStream mstrm = new MemoryStream(img);
                pictureBox3.Image = Image.FromStream(mstrm);
            }
        }
        else 
        {
            textBox1.Test= "Database is Empty";
        }
    }        
    myReader.Close();
}

0 个答案:

没有答案