0

我编写了这段代码来读取存储在 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();
}
4

1 回答 1

1

您已将您的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;
于 2016-03-11T21:03:59.587 回答