我写的存储过程大致如下:
@recordcount 为输出参数
begin
SET @sql="select SELECT @recordcount=count(*) FROM table"
EXEC sp_executesql @sql,N'@recordcount int OUTPUT',@recordcount OUTPUT --语句一
set @sql="select * from table"
exec EXEC(@sql)
end
(1)我用sqldatareader.executeReader()之后,输出参数@recordcount并没用被相应的赋值;
但是第二句的exec EXEC(@sql)有正确输出;(2)我删除了了第二条语句---set @sql=select * from table exec EXEC(@sql)
再次用 sqldatareader.executeReader()的时候能够正确返回@recordCount的值;请问怎样才能获得输出参数的值和后面的一组数据;
@recordcount 为输出参数
begin
SET @sql="select SELECT @recordcount=count(*) FROM table"
EXEC sp_executesql @sql,N'@recordcount int OUTPUT',@recordcount OUTPUT --语句一
set @sql="select * from table"
exec EXEC(@sql)
end
(1)我用sqldatareader.executeReader()之后,输出参数@recordcount并没用被相应的赋值;
但是第二句的exec EXEC(@sql)有正确输出;(2)我删除了了第二条语句---set @sql=select * from table exec EXEC(@sql)
再次用 sqldatareader.executeReader()的时候能够正确返回@recordCount的值;请问怎样才能获得输出参数的值和后面的一组数据;
解决方案 »
- 急~~大神求指导
- 初学ADO.net,遇到一个关于SQLParameter的代码,不懂,求解释。
- 想要在C#中使用DotNetBarSetupVS2008Trial实现窗体加载时看不到延迟时间造成的影响怎么办?
- List<T>转换为ArrayList 在线等..急
- 状态栏的显示
- 如何限制字符串长度
- C#中如何实现“获取硬盘大小与可用空间大小”的问题。
- 急,一个日期比较的问题!!!
- cdshelf(cdshelf)请进!!!!!!!!!!!!!!!!!!!!1
- 我以前学过bc,不过不是很精通,现在不学vc而直接学c#行吗? 还c#有好用的盗版卖吗?c#对操桌系统有没有要求?98 2000 xp?
- javascript脚本中的一个正则表达式的问题
- 委托和事件概念问题
set @sql="select * from table"
exec EXEC(@sql)
return(@@rowcount)
end @@rowcount 就是记录总数第二种@recordcount int OUTPUT 为输出参数
begin
.....
@rowcount是作为第一条语句返回的总记录数返回:;
然后我又执行了第二条sql(这一条返回一组记录);
我是两次执行两条不同语句,第一条是给@recordCount赋值;
using (SqlConnection conn = new SqlConnection("uid=sa;password=密码;database=数据库名;server=127.0.0.1"))
{
try
{
int coun = 0;
conn.Open();
SqlCommand comm = new SqlCommand("getTable",conn);
comm.CommandType = CommandType.StoredProcedure;
comm.Parameters.Add(new SqlParameter("@recordcount",
SqlDbType.Int,
0,
ParameterDirection.Output,
false,
0,
0,
"",
DataRowVersion.Default,
null));
comm.Parameters["@recordcount"].Value = 0;
SqlDataReader dr = comm.ExecuteReader();
string temp = "";
while (dr.Read())
{
//读数据
}
dr.Close(); //要在这之后取存储过程的输出参数
coun = (int)comm.Parameters["@recordcount"].Value;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
@RecordCount int OUTPUT
as
/*定义局部变量*/
declare @TmpSelect NVarchar(600)
/*关闭计数*/
set nocount on select @TmpSelect = 'select * from table'
execute sp_executesql @TmpSelect
select @RecordCount=@@rowcountGO
第一条是给select @recordCount=Count(*)from table2 赋值;
第二:select * from table1 ;
还有我有关闭sqldatareader,然后才读输出参数。
我目前还发现用ExecuteScalar可以单独获取@RecordCount 的值;
so 我现在是读了两次(凑合着先):先用sqldatareader 后用ExecuteScalar,哎,无奈
[code=SQL]
CREATE PROC getTable
@RecordCount int OUTPUT
as
/*定义局部变量*/
declare @intRootRecordCount int
declare @TmpSelect NVarchar(600)
/*关闭计数*/
set nocount on
select @TmpSelect = 'set nocount on;select @SPintRootRecordCount = count(*) from area'
execute sp_executesql
@TmpSelect,
N'@SPintRootRecordCount int OUTPUT',
@SPintRootRecordCount=@RecordCount OUTPUT select @TmpSelect = 'select * from users'
execute sp_executesql @TmpSelect
GO[/SQL]
@RecordCount int OUTPUT
as
/*定义局部变量*/
declare @intRootRecordCount int
declare @TmpSelect NVarchar(600)
/*关闭计数*/
set nocount on
select @TmpSelect = 'set nocount on;select @SPintRootRecordCount = count(*) from area'
execute sp_executesql
@TmpSelect,
N'@SPintRootRecordCount int OUTPUT',
@SPintRootRecordCount=@RecordCount OUTPUT select @TmpSelect = 'select * from users'
execute sp_executesql @TmpSelect
GO