[英]ExecuteScalar: Connection property has not been initialized. SQL Server Connection
我写了这段代码来读取存储在SQL Server数据库中的图像,但是出现了此错误:
ExecuteScalar:连接属性尚未初始化。
因为我已经初始化了连接,所以我不确定是什么问题。
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();
}
您已经将Select
语句和连接都放在了双引号( "
)中。即,您实际上没有指定SqlCommand
的Connection
属性。因此,请更改SqlCommand
:
SqlCommand myCommand = new SqlCommand ("SELECT imagedata FROM Database , myConnection");
对此:
SqlCommand myCommand = new SqlCommand ("SELECT imagedata FROM Database" , myConnection);
或像这样:
SqlCommand myCommand = new SqlCommand("SELECT imagedata FROM Database");
myCommand.Connection = myConnection;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.