Enterprise企业库如何接收存储过程的输出参数

wylp_19 2009-05-17 03:35:23
存储过程
---------------------------------------------------------------
ALTER PROCEDURE [dbo].[Bank_ADDandUpdate]
@username nvarchar(50),
@username2 nvarchar(50),
@money float,
@k int output
AS
BEGIN
declare @i int,@j int
begin tran
update BankA set [money]=[money]-@money where username=@username
set @i=@@rowcount
update BankB set [money]=[money]+@money where username=@username2
set @j=@@rowcount

if (@i=1 and @j=1)
begin
set @k=0
commit
end
else
begin
set @k=1
rollback
end
END


----------------------------------------------------------
代码

private void Bank3()
{
try
{
DbCommand cmd = db.GetStoredProcCommand("Bank_ADDandUpdate");


db.AddInParameter(cmd, "@username", DbType.String, txtName.Text);
db.AddInParameter(cmd, "@username2", DbType.String, txtName2.Text);
db.AddInParameter(cmd, "@money", DbType.Double, Convert.ToDouble(txtMoney.Text));

if (i == 1)
{
db.ExecuteNonQuery(cmd);
MessageBox.Show("转账成功!");
}
else
{
MessageBox.Show("转账XX!");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}



让i接收存储过程里k的返回值 来判断 事务是否执行
...全文
345 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
kensouterry 2009-05-17
  • 打赏
  • 举报
回复
接受输出参数的值就从cmd.Parameters集合中取;
接受返回值就从cmd.ExecuteScalar()里面取(MS是这样)
马少华 2009-05-17
  • 打赏
  • 举报
回复

private void Bank3()
{
try
{
DbCommand cmd = db.GetStoredProcCommand("Bank_ADDandUpdate");


db.AddInParameter(cmd, "@username", DbType.String, txtName.Text);
db.AddInParameter(cmd, "@username2", DbType.String, txtName2.Text);
db.AddInParameter(cmd, "@money", DbType.Double, Convert.ToDouble(txtMoney.Text));
//加入输出值参数
db.AddInParameter(cmd, "@outvalue", DbType.Int, Convert.ToDouble(txtMoney.Text));
if (i == 1)
{
db.ExecuteNonQuery(cmd);
//在ExecuteNoeQuery后面就可以获得你的值了cmd.Paramater["outvalue"].value
MessageBox.Show("转账成功!");
}
else
{
MessageBox.Show("转账XX!");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

cnming 2009-05-17
  • 打赏
  • 举报
回复
sample


System.Data.Common.DbTransaction Tran = null;
try
{
string m_strCommand = "pro_T_Common_Labour_SelectPagedDynamic";

Microsoft.Practices.EnterpriseLibrary.Data.Database DB = Microsoft.Practices.EnterpriseLibrary.Data.DatabaseFactory.CreateDatabase(p_ConnString);

System.Data.Common.DbConnection Conn = DB.CreateConnection();
Conn.Open();
Tran = Conn.BeginTransaction();

System.Data.Common.DbCommand dbc = DB.GetStoredProcCommand(m_strCommand);

DB.AddInParameter(dbc, "@PageSize", System.Data.DbType.Int32, entity.PageSize);
DB.AddInParameter(dbc, "@PageIndex", System.Data.DbType.Int32, entity.PageIndex);
DB.AddOutParameter(dbc, "@TotalRowCount", System.Data.DbType.Int32, 4); //这里就是OutPut参数
DB.AddInParameter(dbc, "@WhereCondition", System.Data.DbType.String, entity.WhereCondition);
dbc.CommandTimeout = 90;

p_DS = DB.ExecuteDataSet(dbc, Tran);
Tran.Commit();

Conn.Close();

entity.TotalRowCount = Convert.ToInt32(DB.GetParameterValue(dbc, "@TotalRowCount"));
//这里就是获取OutPut传回来的值

return true;
}
catch (System.Exception ex)
{
throw Ex;
}
kingtiy 2009-05-17
  • 打赏
  • 举报
回复
db.AddInParameter(cmd, "@k", DbType.Int, -1); //为了不影响结果,这里给个-1的初始值吧.先把k的参数传给command 执行完毕后再检查参数k的值就可以了.
kingtiy 2009-05-17
  • 打赏
  • 举报
回复
db.AddInParameter(cmd, "@k", DbType.Int, 0);
先把k的参数传给command 执行完毕后再检查参数k的值就可以了.
wuyq11 2009-05-17
  • 打赏
  • 举报
回复

110,500

社区成员

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

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

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