在C#如何调用存储过程中返回多个值.

hdhzmx 2006-02-27 03:32:36
create proc spFuShouAll
(
@ResultFu int output,
@ResultShou int output,
@Resulta int output,
@ResultFuShou int output,
@ResultFua int output,
@ResultFSC int output
)
AS
Declare @fu int,@shou int,@FristDay datetime,
@LastDay datetime,@result int,@FuShou int,@Fua int, @FSC int

select @FristDay=DATEADD(mm, DATEDIFF(mm,0,getdate()), 0) --取当月的第一天
select @LastDay=DATEADD(ms,-3,DATEADD(mm, DATEDIFF(m,0,getdate())+1, 0))--取当月的最后一天

select @fu=sum(money) from payinvoice
where Datetime between @FristDay and @LastDay
and Cancellation='0'

select @shou=sum(Money) from acceptinvoice --counteractdate字段为抵扣日期
where (counteractdate is null or rtrim(ltrim(counteractdate))='' ) --这句表示counteractdate的值为空
where Datetime between @FristDay and @LastDay --DateTime为收发票日期
and Cancellation='0' --Cancellation字段是否作废 0表示没有作废

select @result=(@fu-@shou)/1.17
select @FuShou=@fu-@shou
select @Fua=@fu*0.01
select @FSC=(@fu-@shou)-(@fu-@shou)/1.17-@fu*0.01

set @ResultFu = @fu --输出收发票总额
set @ResultShou = @shou --输出付发票总额
set @Resulta = @result --输出付发票总额-收发票总额/1.17得到的金额
set @ResultFuShou = @FuShou --输出付发票总额-收发票总额
set @ResultFua = @Fua --输出付发票总额*0.01 应我厂应交的金额
set @ResultFSC = @FSC --输出超出我厂应交的金额数

print @ResultFu
print @ResultShou
print @Resulta
print @ResultFuShou
print @ResultFua
print @ResultFSC
go

在C#要如何调用这个存储过程.
把六个对面的值绑定到
Lab1.Text,Lab2.Text,Lab3.Text,Lab4.Text,Lab5.Text,Lab6.Text,
...全文
1117 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
hdhzmx 2006-03-27
  • 打赏
  • 举报
回复
try
{
ReplyCe.ConnOpen();
SqlCommand SqlCommand1=new SqlCommand("spFuShouAll",ReplyCe.SqlConnection1);
SqlCommand1.CommandType=CommandType.StoredProcedure;

SqlParameter Param1=new SqlParameter("@ResultFu",SqlDbType.Int); //输出参数
SqlParameter Param2=new SqlParameter("@ResultShou",SqlDbType.Int); //输出参数
SqlParameter Param3=new SqlParameter("@Resulta",SqlDbType.Int); //输出参数
SqlParameter Param4=new SqlParameter("@ResultFuShou",SqlDbType.Int); //输出参数
SqlParameter Param5=new SqlParameter("@ResultFua",SqlDbType.Int); //输出参数
SqlParameter Param6=new SqlParameter("@ResultFSC",SqlDbType.Int); //输出参数
SqlParameter Param7=new SqlParameter("@ResultSB",SqlDbType.Int); //输出参数

SqlCommand1.Parameters.Add(Param1);
SqlCommand1.Parameters.Add(Param2);
SqlCommand1.Parameters.Add(Param3);
SqlCommand1.Parameters.Add(Param4);
SqlCommand1.Parameters.Add(Param5);
SqlCommand1.Parameters.Add(Param6);
SqlCommand1.Parameters.Add(Param7);

Param1.Direction=ParameterDirection.Output; //输出参数
Param2.Direction=ParameterDirection.Output; //输出参数
Param3.Direction=ParameterDirection.Output; //输出参数
Param4.Direction=ParameterDirection.Output; //输出参数
Param5.Direction=ParameterDirection.Output; //输出参数
Param6.Direction=ParameterDirection.Output; //输出参数
Param7.Direction=ParameterDirection.Output; //输出参数

SqlDataReader reader = SqlCommand1.ExecuteReader();
if (reader.Read())
{
this.TextBox5.Text = reader[0].ToString();
this.TextBox6.Text = reader[1].ToString();
this.TextBox7.Text = reader[2].ToString();
this.TextBox8.Text = reader[3].ToString();
this.TextBox9.Text = reader[4].ToString();
this.TextBox10.Text = reader[5].ToString();
this.TextBox11.Text = reader[6].ToString();
reader.Close();
ReplyCe.ConnClose();
}
else
{
reader.Close();
ReplyCe.ConnClose();
Response.Write("<script>alert('参数错误!');history.back();</script>");
Response.End();
}
}
catch
{
Response.Write("<script>alert('参数错误!');history.back();</script>");
Response.End();
}
20011521 2006-03-21
  • 打赏
  • 举报
回复
反回一个表的记录就行了
zhangaili 2006-03-21
  • 打赏
  • 举报
回复
返回一个DATASET集,临时表也可以
hy98521 2006-03-20
  • 打赏
  • 举报
回复
UP
Alex_li 2006-03-20
  • 打赏
  • 举报
回复
正解:

这个很简单的

在 SP spFuShouAll 里最后加上一句

select @ResultFu as Fu ,@ResultShou as Shou ,@Resulta as ta,@ResultFuShou as FuShou
,@ResultFua as Fua ,@ResultFSC as FSc

然后用DataReader 去读取就可以了,

根本就不用什么temp table 的

lisa821015 2006-03-20
  • 打赏
  • 举报
回复
用企业库可以返回DATASET或DATAREADER,然后你自己去取数据,绑定到控件TEXT属性上就OK。或者直接读取输出参数
Database db = DatabaseFactory("instance");
DBCommandWrapper dbc = db.GetStoredProcCommandWrapper("procedure");
dbc.AddInParameter("@ID",DbType.String,UserID);输入参数对应存储过程里面的参数
dbc.AddOutParameter("@params",DbType.String,*);输出参数对应存储过程里面的参数
DataSet data = db.ExecuteDataSet(dbc);返回一个数据集
DataReader myReader = db.ExecuteDataReader( dbc );返回DATAREADER
string newUserID = ( string )dbc.GetParameterValue(params_name);获取输出参数的值
itmingong 2006-03-20
  • 打赏
  • 举报
回复
mark
NNPeople 2006-03-20
  • 打赏
  • 举报
回复
执行完存储过程后直接这样就行了
Label.Text = comm.Parameters["@ResultFu"].Value.ToString();
…………
ycy589 2006-03-06
  • 打赏
  • 举报
回复
学习
woxihuanbohe 2006-02-27
  • 打赏
  • 举报
回复
不用搞那么多参数的 你那些参数全部都是输出参数 你在你的存储过程里面搞一个临时表不就ok了 然后在你的客户端新建cmd connectino da datatable等对象。一会可以手工了
Brunhild 2006-02-27
  • 打赏
  • 举报
回复
将参数的Direction设置为OutPut就可以了:
this.sqlConnection1.Open();
this.sqlCommand1.Connection =this.sqlConnection1;
this.sqlCommand1.ExecuteNonQuery();
this.sqlConnection1.Close();
MessageBox.Show(this.sqlCommand1.Parameters["@ResultFu"].Value.ToString());
MessageBox.Show(this.sqlCommand1.Parameters["@ResultFSC"].Value.ToString());

-渔民- 2006-02-27
  • 打赏
  • 举报
回复
學習,ding
raulredondo 2006-02-27
  • 打赏
  • 举报
回复
存储过程只能有一个返回值,或者返回一个集合,就是一张表,或者没有返回值,你那个相当与传地址的参数,当然也是可以的,但不能叫返回值

yf1025 2006-02-27
  • 打赏
  • 举报
回复
SqlConnection conn;
conn.Open();
comm = new SqlCommand("spFuShouAll",conn);
comm.CommandType = CommandType.StoredProcedure;comm.Parameters.Add(new SqlParameter("@ResultFu",SqlDbType.Int,4,ParameterDirection.Output,false,0,0,null,DataRowVersion.Default,null));
comm.CommandType = CommandType.StoredProcedure;comm.Parameters.Add(new SqlParameter("@ResultShou ",SqlDbType.Int,4,ParameterDirection.Output,false,0,0,null,DataRowVersion.Default,null));
.....其他几个也是这样就不写了
comm.ExecuteNonQuery();
Lab1.Text = comm.Parameters["@ResultFu"].Value.ToString();
Lab2.Text = comm.Parameters["@ResultFu"].Value.ToString();
.....其他几个也是这样

iamknight 2006-02-27
  • 打赏
  • 举报
回复
调用存储过程,传入输出参数,存储过程执行成功后,参数的值就是返回的值。

110,561

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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