try{}catch{}问题+objrea.read()问题?????十万火急在线等。
从DataReader中读出数据,但是DataReader返回了0条数据,从而导至 ObjRead.Read()没有可读数据,然而就会产生错误。
我在try{}catch{}的 catch{}中加入了Response=HttpContext.Current.Response;
Response.Redirect("../Error.aspx");
这条语句。为什么执ObjRead.Read()这条语句没有跳到catch中去执行,却执直接产生错误提示:
ObjRead.Read();没有可读数据。。
大家能我帮解释一下吗?
讲讲解决的代码!!!!谢谢
SqlString="select popedom from ClassEnter where "
+"username=@username and password=@password";
Param表示是SQL参数
代码如下::::::
//返回用户名权限
public bool UrPwPo(string SqlString,SqlParameter[] Param)
{
//pop用来存放权限的值
bool pop;
SqlConnection ObjCon=new SqlConnection(Class1.ConStr);
Class1.ConState(ObjCon);
SqlCommand ObjCom=new SqlCommand();
Class1.PrepareCommand(ObjCon,ObjCom,SqlString,Param);
try
{
SqlDataReader ObjRead=ObjCom.ExecuteReader();
ObjRead.Read();
pop=(bool)ObjRead.GetValue(0);
ObjCon.Close();
return pop;
}
catch
{
ObjCon.Close();
Response=HttpContext.Current.Response;
Response.Redirect("../Error.aspx");
}
}
//初使Coomand对像
public static void PrepareCommand(SqlConnection objcon,SqlCommand objcom,string sqlstring,SqlParameter[] param)
{
objcom.Connection=objcon;
Class1.ConState(objcon);
objcom.CommandText=sqlstring;
if(param!=null)
{
foreach(SqlParameter par in param)
{
objcom.Parameters.Add(par);
}
}
}