我有以下的功能,它檢查以查看是否用戶(strU
)在表中的列存在,如果是這樣返回1,否則返回0:爲什麼的ExecuteScalar返回的NullReferenceException錯誤
public int AddDataScalar(string strU)
{
string strQueryExistence = "SELECT 1 FROM [OB].[h].[OP_PEONS] WHERE Executive= '" + strU + "'";
int inNum;
using (SqlConnection con = new SqlConnection(strConn))
{
con.Open();
SqlCommand cmd = new SqlCommand(strQueryExistence, con);
object value = cmd.ExecuteScalar().ToString();
if (value != null)
{
inNum = 1;
}
else
{
inNum = 0;
}
con.Close();
}
return inNum;
}
它是未能在這一行:object value = cmd.ExecuteScalar().ToString();
,出現以下錯誤:Object reference not set to an instance of an object.
我該如何解決呢?
http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and怎麼辦我修好了 – 2014-10-01 14:34:19