ExecuteReader: Connection 属性尚未初始化
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 属性尚未初始化,希望高手指教,感激不尽
------解决方案--------------------
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(); //加上这句
}
}