SqlDataReader无法获取Procedure的Output参数值?
来源:互联网 发布:maven构建java项目打包 编辑:程序博客网 时间:2024/06/14 21:36
SqlDataReader无法获取Procedure的Output参数值?
大 | 中 | 小
Post by 幻想曲 at 08 Apr 2007 11:22
<script type="text/javascript"><!--google_ad_client = "pub-3122628043669411";google_ad_output = "textlink";google_ad_format = "ref_text";google_cpa_choice = "CAAQ9cm1_wEaCBkyusVe5rXbKOm84YcB";google_ad_channel = "";//--> </script><script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"> </script><script language="JavaScript1.1" src="http://pagead2.googlesyndication.com/cpa/ads?client=ca-pub-3122628043669411&cpa_choice=CAAQ9cm1_wEaCBkyusVe5rXbKOm84YcB&oe=utf-8&dt=1180689775718&lmt=1180689775&format=ref_text&output=textlink&correlator=1180689775703&url=http%3A%2F%2Fwww.lemongtree.com%2FArchives%2FDataReader_procedure.aspx&region=_google_cpa_region_&ref=http%3A%2F%2Fwww.google.cn%2Fsearch%3Fq%3Dout%2BSqlDataReader%2B%26complete%3D1%26hl%3Dzh-CN%26newwindow%3D1&cc=100&flash=6&u_h=768&u_w=1024&u_ah=738&u_aw=1024&u_cd=32&u_tz=480&u_java=true" type="text/javascript"></script>网上冲浪更快更爽,下载带有 Google 工具栏的 Firefox
同样的参数去执行一段存储过程
返回类型为DataTable时有值,而返回SqlDataReader时却为空.
public static DataTable ExecuteDataTable(string Text, CommandType commandType, SqlParameter[] para, out int pagecount, out int recordcount)
{
using (SqlConnection conn = new SqlConnection(SqlConnectionString))
{
SqlCommand comm = conn.CreateCommand();
comm.CommandText = Text;
comm.CommandType = commandType;
foreach (SqlParameter p in para)
{
comm.Parameters.Add(p);
}
comm.Parameters.Add("@pagecount", SqlDbType.Int);
comm.Parameters.Add("@RecordCount", SqlDbType.Int);
comm.Parameters["@pagecount"].Direction = ParameterDirection.Output;
comm.Parameters["@RecordCount"].Direction = ParameterDirection.Output;
SqlDataAdapter da = new SqlDataAdapter(comm);
DataSet dst = new DataSet();
da.Fill(dst);
if (conn.State == ConnectionState.Open)
conn.Close();
pagecount = (int)comm.Parameters["@pagecount"].Value; //这里返回正确
recordcount = (int)comm.Parameters["@RecordCount"].Value; //这里也正确
return dst.Tables[0];
}
}
返回类型为SqlDataReader时
public static SqlDataReader ExecuteDataReader(string Text, CommandType commandType, SqlParameter[] para, out int pagecount, out int recordcount)
{
pagecount = 1;
recordcount = 0;
using (SqlConnection conn = new SqlConnection(SqlConnectionString))
{
SqlCommand comm = conn.CreateCommand();
comm.CommandText = Text;
comm.CommandType = commandType;
foreach (SqlParameter p in para)
{
comm.Parameters.Add(p);
}
comm.Parameters.Add("@PageCount", SqlDbType.Int);
comm.Parameters.Add("@RecordCount", SqlDbType.Int);
comm.Parameters["@PageCount"].Direction = ParameterDirection.Output;
comm.Parameters["@RecordCount"].Direction = ParameterDirection.Output;
conn.Open();
using (SqlDataReader dr = comm.ExecuteReader())
{
if (dr.Read())
{
pagecount = (int)comm.Parameters["@pagecount"].Value; //null
recordcount = (int)comm.Parameters["@recordcount"].Value; //null
}
return dr;
}
}
}
为什么会这样呢?
原因:
同样的参数去执行一段存储过程
返回类型为DataTable时有值,而返回SqlDataReader时却为空.
public static DataTable ExecuteDataTable(string Text, CommandType commandType, SqlParameter[] para, out int pagecount, out int recordcount)
{
using (SqlConnection conn = new SqlConnection(SqlConnectionString))
{
SqlCommand comm = conn.CreateCommand();
comm.CommandText = Text;
comm.CommandType = commandType;
foreach (SqlParameter p in para)
{
comm.Parameters.Add(p);
}
comm.Parameters.Add("@pagecount", SqlDbType.Int);
comm.Parameters.Add("@RecordCount", SqlDbType.Int);
comm.Parameters["@pagecount"].Direction = ParameterDirection.Output;
comm.Parameters["@RecordCount"].Direction = ParameterDirection.Output;
SqlDataAdapter da = new SqlDataAdapter(comm);
DataSet dst = new DataSet();
da.Fill(dst);
if (conn.State == ConnectionState.Open)
conn.Close();
pagecount = (int)comm.Parameters["@pagecount"].Value; //这里返回正确
recordcount = (int)comm.Parameters["@RecordCount"].Value; //这里也正确
return dst.Tables[0];
}
}
返回类型为SqlDataReader时
public static SqlDataReader ExecuteDataReader(string Text, CommandType commandType, SqlParameter[] para, out int pagecount, out int recordcount)
{
pagecount = 1;
recordcount = 0;
using (SqlConnection conn = new SqlConnection(SqlConnectionString))
{
SqlCommand comm = conn.CreateCommand();
comm.CommandText = Text;
comm.CommandType = commandType;
foreach (SqlParameter p in para)
{
comm.Parameters.Add(p);
}
comm.Parameters.Add("@PageCount", SqlDbType.Int);
comm.Parameters.Add("@RecordCount", SqlDbType.Int);
comm.Parameters["@PageCount"].Direction = ParameterDirection.Output;
comm.Parameters["@RecordCount"].Direction = ParameterDirection.Output;
conn.Open();
using (SqlDataReader dr = comm.ExecuteReader())
{
if (dr.Read())
{
pagecount = (int)comm.Parameters["@pagecount"].Value; //null
recordcount = (int)comm.Parameters["@recordcount"].Value; //null
}
return dr;
}
}
}
为什么会这样呢?
原因:
当您将 Command 对象用于存储过程时,可以将 Command 对象的 CommandType 属性设置为 StoredProcedure。当 CommandType 为 StoredProcedure 时,可以使用 Command 的 Parameters 属性来访问输入及输出参数和返回值。无论调用哪一个 Execute 方法,都可以访问 Parameters 属性。但是,当调用 ExecuteReader 时,在 DataReader 关闭之前,将无法访问返回值和输出参数。
ref:http://msdn2.microsoft.com/zh-CN/library/tyy0sz6b.aspx修改了一下:
SqlDataReader dr = comm.ExecuteReader(CommandBehavior.CloseConnection);
dr.NextResult(); //加了这一行
pagecount = (int)comm.Parameters["@pagecount"].Value;
recordcount = (int)comm.Parameters["@recordcount"].Value;
return dr;
}
这样就行了
- SqlDataReader无法获取Procedure的Output参数值?
- 使用sqldatareader时,获取不到输出参数的值【鸡蛋】
- SqlCommand.ExecuteReader 无法获取 sqlserver 存储过程 OUTPUT 返回的参数值问题
- 从带输出参数的存储过程中获取输出参数的值 OUTPUT
- iBatis.Net如何获取存储过程的Output的参数值
- Entity Framework 执行存储过程的 获取 OutPut 参数的值
- C#获取存储过程的Return返回值和Output输出参数值
- C#获取存储过程的Return返回值和Output输出参数值
- C#获取存储过程的Return返回值和Output输出参数值
- C#获取存储过程的Return返回值和Output输出参数值
- C#获取存储过程的Return返回值和Output输出参数值
- C#获取存储过程的Return返回值和Output输出参数值
- C#获取存储过程的Return返回值和Output输出参数值
- C#获取存储过程的Return返回值和Output输出参数值
- C#获取存储过程的 Return返回值和Output输出参数值
- 执行存储过程获取OUTput参数返回值
- OUTPUT 参数的使用
- dbaccess shell调用sql语句和procedure的区别 output
- K线详解【图文】
- 用驱动隐藏进程
- 对于not in 和 not exists的性能区别:
- Java虚拟机类装载:原理、实现与应用
- 如何在Action里返回值 [收藏]
- SqlDataReader无法获取Procedure的Output参数值?
- 需要注意的10个技巧
- 通过避免下列 10 个常见 ASP.NET 缺陷使网站平稳运行
- 儿童节咋就这么开心?
- 请问在oracle中如何创建临时表
- Linux 2.6内核中提高网络I/O性能的新方法-epoll
- visual studio issues
- 判断用户登录
- 数据库的事务控制和并发控制