SqlDataReader无法获取Procedure的Output参数值?

yibin2006 2007-04-08 11:51:45
同样的参数去执行一段存储过程
返回类型为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;
}
}
}
为什么会这样呢?
...全文
365 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
amandag 2007-04-08
  • 打赏
  • 举报
回复
http://www.knowsky.com/340290.html

当您将 Command 对象用于存储过程时,可以将 Command 对象的 CommandType 属性设置为 StoredProcedure。当 CommandType 为 StoredProcedure 时,可以使用 Command 的 Parameters 属性来访问输入及输出参数和返回值。无论调用哪一个 Execute 方法,都可以访问 Parameters 属性。但是,当调用 ExecuteReader 时,在 DataReader 关闭之前,将无法访问返回值和输出参数
using System;using System.Collections.Generic;using System.Text;using System.Data;using System.Data.SqlClient; namespace DatabaseOperate{ class SqlOperateInfo { //Suppose your ServerName is "aa",DatabaseName is "bb",UserName is "cc", Password is "dd" private string sqlConnectionCommand = "Data Source=aa;Initial Catalog=bb;User ID=cc;Pwd=dd"; //This table contains two columns:KeywordID int not null,KeywordName varchar(100) not null private string dataTableName = "Basic_Keyword_Test"; private string storedProcedureName = "Sp_InertToBasic_Keyword_Test"; private string sqlSelectCommand = "Select KeywordID, KeywordName From Basic_Keyword_Test"; //sqlUpdateCommand could contain "insert" , "delete" , "update" operate private string sqlUpdateCommand = "Delete From Basic_Keyword_Test Where KeywordID = 1"; public void UseSqlReader() { SqlConnection sqlConnection = new SqlConnection(sqlConnectionCommand); SqlCommand sqlCommand = new SqlCommand(); sqlCommand.CommandType = System.Data.CommandType.Text; sqlCommand.Connection = sqlConnection; sqlCommand.CommandText = sqlSelectCommand; sqlConnection.Open(); SqlDataReader sqlDataReader = sqlCommand.ExecuteReader(); while(sqlDataReader.Read()) { //Get KeywordID and KeywordName , You can do anything you like. Here I just output them. int keywordid = (int)sqlDataReader[0]; //the same as: int keywordid = (int)sqlDataReader["KeywordID"] string keywordName = (string)sqlDataReader[1]; //the same as: string keywordName = (int)sqlDataReader["KeywordName"] Console.WriteLine("KeywordID = " + keywordid + " , KeywordName = " + keywordName); } sqlDataReader.Close(); sqlCommand.Dispose(); sqlConnection.Close(); } public void UseSqlStoredProcedure() { SqlConnection sqlConnection = new SqlConnection(sqlConnectionCommand); SqlCommand sqlCommand = new SqlCommand(); sqlCommand.CommandType = CommandType.StoredProcedure; sqlCommand.Connection = sqlConnection; sqlCommand.CommandText = storedProcedureName; sqlConnection.Open(); sqlCommand.ExecuteNonQuery(); //you can use reader here,too.as long as you modify the sp and let it like select * from .... sqlCommand.Dispose(); sqlConnection.Close(); } public void UseSqlDataSet() { SqlConnection sqlConnection = new SqlConnection(sqlConnectionCommand); SqlCommand sqlCommand = new SqlCommand(); sqlCommand.CommandType = System.Data.CommandType.Text; sqlCommand.Connection = sqlConnection; sqlCommand.CommandText = sqlSelectCommand; sqlConnection.Open(); SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(); sqlDataAdapter.SelectCommand = sqlCommand; DataSet dataSet = new DataSet(); //sqlCommandBuilder is for update the dataset to database SqlCommandBuilder sqlCommandBuilder = new SqlCommandBuilder(sqlDataAdapter); sqlDataAdapter.Fill(dataSet, dataTableName); //Do something to dataset then you can update it to Database.Here I just add a row DataRow row = dataSet.Tables[0].NewRow(); row[0] = 10000; row[1] = "new row"; dataSet.Tables[0].Rows.Add(row); sqlDataAdapter.Update(dataSet, dataTableName); sqlCommand.Dispose(); sqlDataAdapter.Dispose(); sqlConnection.Close(); } }}

62,041

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧