我写了这段代码来读取存储在SQL Server数据库中的图像但是我收到了这个错误:
ExecuteScalar:尚未初始化Connection属性。
由于我已经初始化了连接,我不确定是什么问题。
SqlConnection myConnection = null;
try
{
myConnection = new SqlConnection("Data Source=Source; Initial Catalog=Database; user ID=Test; Password=Test");
SqlCommand myCommand = new SqlCommand ("SELECT imagedata FROM Database , myConnection");
myConnection.Open();
// Get the image from the database.
byte[] imagedata = (byte[])myCommand.ExecuteScalar();
if (imagedata != null)
{
return image;
}
else
{
return null;
}
}
finally
{
myConnection.Close();
}
答案 0 :(得分:1)
您已将Select
语句和连接放在双引号("
)中。即你没有实际指定SqlCommand
的{{1}}财产。从此处更改Connection
:
SqlCommand
对此:
SqlCommand myCommand = new SqlCommand ("SELECT imagedata FROM Database , myConnection");
或者像这样:
SqlCommand myCommand = new SqlCommand ("SELECT imagedata FROM Database" , myConnection);