在C#如何调用存储过程中返回多个值.
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,
问题点数:20、回复次数:15Top
1 楼iamknight(侠客)回复于 2006-02-27 15:46:17 得分 0
调用存储过程,传入输出参数,存储过程执行成功后,参数的值就是返回的值。Top
2 楼yf1025(小桥,流水,人家)回复于 2006-02-27 15:49:17 得分 10
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();
.....其他几个也是这样
Top
3 楼raulredondo()回复于 2006-02-27 15:52:00 得分 0
存储过程只能有一个返回值,或者返回一个集合,就是一张表,或者没有返回值,你那个相当与传地址的参数,当然也是可以的,但不能叫返回值
Top
4 楼wanyong775(渔民:小小的网少年)回复于 2006-02-27 16:47:13 得分 0
學習,dingTop
5 楼Brunhild()回复于 2006-02-27 17:29:50 得分 0
将参数的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());
Top
6 楼woxihuanbohe(我喜欢)回复于 2006-02-27 17:51:18 得分 0
不用搞那么多参数的 你那些参数全部都是输出参数 你在你的存储过程里面搞一个临时表不就ok了 然后在你的客户端新建cmd connectino da datatable等对象。一会可以手工了Top
7 楼ycy589(ycy589)回复于 2006-03-06 17:10:15 得分 0
学习Top
8 楼NNPeople(春春)回复于 2006-03-20 15:52:22 得分 0
执行完存储过程后直接这样就行了
Label.Text = comm.Parameters["@ResultFu"].Value.ToString();
…………Top
9 楼itmingong(nous+wisdom+courage)回复于 2006-03-20 16:36:01 得分 0
markTop
10 楼lisa821015()回复于 2006-03-20 17:05:19 得分 0
用企业库可以返回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);获取输出参数的值Top
11 楼Alex_li(Atomsoft)回复于 2006-03-20 17:14:09 得分 10
正解:
这个很简单的
在 SP spFuShouAll 里最后加上一句
select @ResultFu as Fu ,@ResultShou as Shou ,@Resulta as ta,@ResultFuShou as FuShou
,@ResultFua as Fua ,@ResultFSC as FSc
然后用DataReader 去读取就可以了,
根本就不用什么temp table 的
Top
12 楼hy98521(斯达克)回复于 2006-03-20 17:18:45 得分 0
UPTop
13 楼zhangaili(火兮·静心熊)回复于 2006-03-21 11:19:47 得分 0
返回一个DATASET集,临时表也可以Top
14 楼20011521()回复于 2006-03-21 11:33:48 得分 0
反回一个表的记录就行了Top
15 楼hdhzmx()回复于 2006-03-27 19:08:59 得分 0
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();
}Top




