日期:2014-05-19  浏览次数:21101 次

Convert.ToInt32(com.ExecuteScalar()); 异常 谢谢
public   static   int   GetIn(   string   Uid,   string   Pwd   )
{
OleDbConnection   con   =   new   OleDbConnection(
"Provider=SQLOLEDB.1;Integrated   Security=SSPI;Persist   Security   Info=False;User   ID=saa;Initial   Catalog=test;Data   Source=A ");  
string   sql   = "select   privilage   as   num   from   pri   whre   id=?   and   ps=? ";  
OleDbCommand   com   =   new   OleDbCommand(   sql,   con   );
com.Parameters.Add(   "? ",   SqlDbType.NVarChar   ).Value   =   Uid;
com.Parameters.Add(   "? ",   SqlDbType.NVarChar   ).Value   =   Pwd;  
int   level   =   -10;
con.Open();
level   =   Convert.ToInt32(com.ExecuteScalar());
con.Close();  
switch(   level   )
{
case   0:   return   0;     //非法用户
case   1:   return   1;     //合法学生
case   10:   return   10;   //管理员  
}  
return   0;  
}


level   =   Convert.ToInt32(com.ExecuteScalar());   执行到这行发生ledbexception

请问怎么回事?     谢谢

------解决方案--------------------
返回的值为空?

sql server的话
最好用sql的库类吧
------解决方案--------------------
level = Convert.ToInt32(com.ExecuteScalar());
object obj = com.ExecuteScalar();
if (obj is int)
{
level = (int)obj);
}
else if (obj is string)
{
int.TryParse(obj.ToString(), out level);
}
else
{
....
}