夥計們,我需要一點幫助。這是我第一次開發一個有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