SqlDataReader是实时的读取数据,类似SQLSERVER里面的游标,当你存储过程里面执行了SELECT语句,存储过程运行到那个位置就会立刻返回数据给调用方,但是存储过程本身尚未执行完,要等到SELECT语句执行完也就是SqlDataReader读取到最后一条记录后,存储过程里的SELECT语句才执行完毕,之后运行存储过程中的后续语句,直到存储过程完全运行结束后,才有OUT类型参数的回写……
[/Quote]
嗯清楚了 但是我执行返回DataSet的方法就得到了 代码如下:
public static DataSet ExecuteDataSet(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
{
DataSet ds = null;
SqlCommand cmd = null;
SqlConnection conn = null;
// we use a try/catch here because if the method throws an exception we want to
// close the connection throw code, because will exist, hence the
// commandBehaviour.CloseConnection will not work
try
{
ds = new DataSet();
conn = new SqlConnection(connectionString);
cmd = new SqlCommand();
PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
SqlDataAdapter adt = new SqlDataAdapter(cmd);
adt.Fill(ds);
cmd.Parameters.Clear();
return ds;
}
catch (Exception e)
{
throw new ApplicationException(e.Message);
}
finally
{
if (conn != null && conn.State != ConnectionState.Closed)
conn.Close();
}
}
我看到 也清除了参数 但为什么能获取返回参数?
你的原因是你用到了SqlDataReader,你用DataTable缓存结果集就没问题了,因为当你的SqlDataReader没有关闭之前,那个返回型参数将得不到值,但是你在遍历那个SqlDataReader 之前就清空了Parameters,因此返回值无法回写。看看你的Helper中,有没有执行后得到DataTable或者DataSet的方法,有的话直接用那个就绝对不会有问题的。
[/Quote]
我把我的SqlHelper的SqlDataReader贴出来
public static SqlDataReader ExecuteReader(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
{
SqlCommand cmd = new SqlCommand();
SqlConnection conn = new SqlConnection(connectionString);
// we use a try/catch here because if the method throws an exception we want to
// close the connection throw code, because no datareader will exist, hence the
// commandBehaviour.CloseConnection will not work
try
{
PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
cmd.Parameters.Clear();
return rdr;
}
catch
{
conn.Close();
throw;
}
}
return rdr之前参数被清空了 所以遍历reader后得不到参数,那你说的:当你的SqlDataReader没有关闭之前,那个返回型参数将得不到值怎么理解?
因为参数是引用类型,你在代码返回之前都不能清掉,你可以清除掉除了索引是6的其他参数,清除掉后,原来索引为6的参数就变成0了,TotalCount = Convert.ToInt32(parm[0].Value);
[/Quote]
我怎么清除参数时精确到不清除那个返回参数
给你看下SqlHelper中的代码
public static SqlDataReader ExecuteReader(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
{
SqlCommand cmd = new SqlCommand();
SqlConnection conn = new SqlConnection(connectionString);
// we use a try/catch here because if the method throws an exception we want to
// close the connection throw code, because no datareader will exist, hence the
// commandBehaviour.CloseConnection will not work
try
{
PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
cmd.Parameters.Clear();
return rdr;
}
catch
{
conn.Close();
throw;
}
}
就是那句cmd.Parameters.Clear();
我把代码贴出来 你看看
List<ExecProc2012611> listAccountInfo = new List<ExecProc2012611>();
SqlParameter[] parm =
{
new SqlParameter("@where",SqlDbType.VarChar) ,
new SqlParameter("@tablename",SqlDbType.NVarChar) ,
new SqlParameter("@column",SqlDbType.NVarChar) ,
new SqlParameter("@sort",SqlDbType.NVarChar) ,
new SqlParameter("@pageIndex", SqlDbType.Int),
new SqlParameter("@pageCount",SqlDbType.Int),
new SqlParameter("@TotalCount", SqlDbType.Int)