private void button2_Click(object sender, EventArgs e)
        {
            byte[] MyData = new byte[0];
            DBHelper.connection.Open();
            MemoryStream mystream = null;
            try
            {
                SqlCommand command = new SqlCommand();
                command.CommandText = "select * from Im_Info";
                SqlDataReader sdr = command.ExecuteReader();//提示有错,求高手帮忙
                sdr.Read();
                MyData = (byte[])sdr["Pr_Info"];
                mystream = new MemoryStream(MyData);
                System.Drawing.Image img = System.Drawing.Image.FromStream(mystream, true);
                pictureBox1.Image = img;
                mystream.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                
                DBHelper.connection.Close();
            }
        }
调试的时候,到了那句就直接跳到catch语句,后面的不执行,提示ExecuteReader: Connection 属性尚未初始化,希望高手指教,感激不尽

解决方案 »

  1.   

    SqlConnection con=new SqlConnection("这是你的连接字符串");
    private void button2_Click(object sender, EventArgs e)
      {
      byte[] MyData = new byte[0];
      //DBHelper.connection.Open(); //不要替换成下面的
      SqlConnection con=new SqlConnection("这是你的连接字符串"); //加上这句
      MemoryStream mystream = null;
      try
      {
      SqlCommand command = new SqlCommand();
      command.CommandText = "select * from Im_Info";
      command.Connection=con;  //加上这句
      con.Open(); //加上这句
      SqlDataReader sdr = command.ExecuteReader();//提示有错,求高手帮忙
      sdr.Read();
      MyData = (byte[])sdr["Pr_Info"];
      mystream = new MemoryStream(MyData);
      System.Drawing.Image img = System.Drawing.Image.FromStream(mystream, true);
      pictureBox1.Image = img;
      mystream.Close();
      }
      catch (Exception ex)
      {
      MessageBox.Show(ex.Message);
      }
      finally
      {
        
      //DBHelper.connection.Close();
      con.Close(); //加上这句
      }
      }
      

  2.   

    SqlConnection con=new SqlConnection("这是你的连接字符串");
    最开始的这句不要了
      

  3.   

    既然你有用DBHelper.connection.Open(),那么你的DBHelper类里面应该也会有提供相应的操作方法,下面的代码应该是直接传一个sql语句进去就可以。
    如果你需要直接使用try...catch里面的代码,那么就改为1楼的方法。