新手求助:返回类型为SqlDataReader的方法用作WebMethod时出现“未将对象引用设置到对象的实例”的错误?
代码如下:
[WebMethod()]
public SqlDataReader GetCategorys()
{
//定义数据库的Connection and Command
SqlConnection myConnection = new SqlConnection (SQLHelper.DBCONNECTIONSTRING);
SqlCommand myCommand = new SqlCommand("Pr_GetCategorys",myConnection);
//定义访问数据库的方式为存储过程
myCommand.CommandType = CommandType.StoredProcedure;
SqlDataReader dr = null;
try
{
//打开数据库的连接
myConnection.Open();
}
catch(Exception ex)
{
throw new MyException("10001","数据库连接失败!",ex);
}
try
{
//执行数据库的存储过程(访问数据库)
dr = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
}
catch(Exception ex)
{
throw new MyException("10001",ex.Message,ex);
}
//返回 dr
return dr;
}