存储过程操作问题
-1.转帐的事务存储过程create procedure proc_transfer @card1 char(19),@card2 char(19),@outmoney money
AS
begin tran
print '开始转帐,请稍后......'
DECLARE @errors int
set @errors=0
INSERT INTO transInfo(transType,cardID,transMoney) VALUES('支取',@card1,@outmoney)
set @errors=@errors+@@error
INSERT INTO transInfo(transType,cardID,transMoney) VALUES('存入',@card2,@outmoney)
set @errors=@errors+@@error
if (@errors>0)
begin
print '转帐失败!'
rollback tran
end
else
begin
print '转帐成功!'
commit tran
end
GO
--2.测试上述事务存储过程
--从李四的帐户转帐2000到张三的帐户
--同上一样,现实中的取款机依靠读卡器读出张三/李四的卡号,这里根据张三/李四的名字查出考号来模拟
declare @card1 char(19),@card2 char(19)
select @card1=cardID from cardInfo Inner Join userInfo ON
cardInfo.customerID=userInfo.customerID where customerName='李四'
select @card2=cardID from cardInfo Inner Join userInfo ON
cardInfo.customerID=userInfo.customerID where customerName='张三'
这段代码怎么在c#中操作?
下面的方法用那个最方便
/// <summary>
/// 为存储过程生成一个SqlCommand对象
/// </summary>
/// <param name="procName">存储过程名</param>
/// <param name="prams">存储过程参数</param>
/// <returns>SqlCommand对象</returns>
private SqlCommand CreateCmd(string procName, SqlParameter[] prams, SqlDataReader Dr)
{
sqlcon.Open();
SqlCommand Cmd = new SqlCommand(procName, sqlcon);
Cmd.CommandType = CommandType.StoredProcedure;
if (prams != null)
{
foreach (SqlParameter parameter in prams)
Cmd.Parameters.Add(parameter);
}
Cmd.Parameters.Add(
new SqlParameter("ReturnValue", SqlDbType.Int, 4,
ParameterDirection.ReturnValue, false, 0, 0,
string.Empty, DataRowVersion.Default, null));
return Cmd;
}
/// <summary>
/// 运行存储过程,返回.
/// </summary>
/// <param name="procName">存储过程名</param>
/// <param name="prams">存储过程参数</param>
/// <param name="dataReader">SqlDataReader对象</param>
public void RunProc(string procName, SqlParameter[] prams, SqlDataReader Dr)
{
SqlCommand Cmd = CreateCmd(procName, prams, Dr);
Dr = Cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
return;
}
/// <summary>
/// 运行存储过程,返回.
/// </summary>
/// <param name="procName">存储过程名</param>
/// <param name="prams">存储过程参数</param>
public string RunProc(string procName, SqlParameter[] prams)
{
SqlDataReader Dr;
SqlCommand Cmd = CreateCmd(procName, prams);
Dr = Cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
if (Dr.Read())
{
return Dr.GetValue(0).ToString();
}
else
{
return "";
}
}
/// <summary>
/// 运行存储过程,返回dataset.
/// </summary>
/// <param name="procName">存储过程名.</param>
/// <param name="prams">存储过程入参数组.</param>
/// <returns>dataset对象.</returns>
public DataSet RunProc(string procName, SqlParameter[] prams, DataSet Ds)
{
SqlCommand Cmd = CreateCmd(procName, prams);
SqlDataAdapter Da = new SqlDataAdapter(Cmd);
try
{
Da.Fill(Ds);
}
catch (Exception Ex)
{
throw Ex;
}
return Ds;
}
--------------------编程问答-------------------- 没人啊
补充:.NET技术 , C#