伙计们,我需要一点帮助。这是我第一次开发一个有DB的应用程序,考虑到这一点,请原谅我的错误。ExecuteScalar:对象引用未设置等
我想从数据库中获取一个布尔值,并应用if,否则循环它......但它一直在ExecuteScalar函数上抛出“Object reference not set”错误。
下面是代码: -
string sql = " // here is my sql query ";
string connectionstring = " // Here is my connection string.... ";
SqlConnection connection = new SqlConnection(connectionstring);
SqlCommand command = new SqlCommand(sql,connection);
command.Connection = connection;
connection.Open();
bool ev = (bool)command.ExecuteScalar();
if (ev == true)
{
MessageBox.Show("some error");
}
else
{
// Some Code
}
我在做什么错?
帮助将非常感激。
问候。
你检查了返回值吗?这可能是因为它没有连接或什么。 – joell