gpt4 book ai didi

c# - 如何将这些调用放在 SqlTransaction 中?

转载 作者:行者123 更新时间:2023-11-30 15:46:47 26 4
gpt4 key购买 nike

我有一个类,其中包含我的 ASP.NET 4.0 应用程序的所有数据访问代码。类中有两种方法可以将数据插入数据库。我想在 SqlTransaction 中加入这些插入,并在其中一个插入失败时回滚事务。但是,由于我的编码方式,我不确定该怎么做。这是我的数据访问代码:

public class DBUtil
{

private static readonly string _connectionString;

static DBUtil()
{
_connectionString = WebConfigurationManager.ConnectionStrings["MooDB"].ConnectionString;
if (string.IsNullOrEmpty(_connectionString))
throw new Exception("Connection string not configured in Web.Config file");
}

public int InsertTrade(
string symbol,
string tradeSetupId,
int tradeTypeId,
decimal lotsPerUnit,
string chartTimeFrame,
decimal pctAccountRisked,
int? tradeGrade = null,
int? executionGrade = null,
int? MFEPips = null,
int? MAEPips = null
)
{
SqlCommand cmd = new SqlCommand("usp_InsertTrade");
// required parameters
cmd.Parameters.AddWithValue("@symbol", symbol);
cmd.Parameters.AddWithValue("@tradeSetupId", tradeSetupId);
cmd.Parameters.AddWithValue("@tradeTypeId", tradeTypeId);
cmd.Parameters.AddWithValue("@lotsPerUnit", lotsPerUnit);
cmd.Parameters.AddWithValue("@chartTimeFrame", chartTimeFrame);
cmd.Parameters.AddWithValue("@pctAccountRisked", pctAccountRisked);

// optional parameters
if (MAEPips.HasValue)
cmd.Parameters.AddWithValue("@MAEPips", MAEPips);
if (MFEPips.HasValue)
cmd.Parameters.AddWithValue("@MFEPips", MFEPips);
if (tradeGrade.HasValue)
cmd.Parameters.AddWithValue("@tradeGrade", tradeGrade);
if (executionGrade.HasValue)
cmd.Parameters.AddWithValue("@executionGrade", executionGrade);
return (InsertData(cmd, "trade"));
}

public int InsertOrder(
int tradeId,
int units,
string side,
decimal price,
decimal spread,
int strategyId,
string signalTypeId,
int brokerId,
string orderTypeId,
DateTime orderDateTime,
string comment,
int? accountId = null
)
{
SqlCommand cmd = new SqlCommand("usp_InsertOrder");
// required parameters
cmd.Parameters.Add(new SqlParameter("@tradeId", tradeId));
cmd.Parameters.Add(new SqlParameter("@units", units));
cmd.Parameters.Add(new SqlParameter("@side", side));
cmd.Parameters.Add(new SqlParameter("@price", price));
cmd.Parameters.Add(new SqlParameter("@spread", spread));
cmd.Parameters.Add(new SqlParameter("@strategyId", strategyId));
cmd.Parameters.Add(new SqlParameter("@signalTypeId", signalTypeId));
cmd.Parameters.Add(new SqlParameter("@brokerId", brokerId));
cmd.Parameters.Add(new SqlParameter("@orderTypeId", orderTypeId));
cmd.Parameters.Add(new SqlParameter("@orderDateTime", orderDateTime));
cmd.Parameters.Add(new SqlParameter("@comment", comment));

// optional parameters
if (accountId.HasValue)
cmd.Parameters.Add(new SqlParameter("@accountId", accountId));
return (InsertData(cmd, "order"));
}

private int InsertData(SqlCommand cmd, string tableName)
{
SqlConnection con = new SqlConnection(_connectionString);
cmd.Connection = con;
cmd.CommandType = CommandType.StoredProcedure;
int rc = -1;
try
{
con.Open();
rc = (int) cmd.ExecuteScalar();
}
finally
{
con.Close();

}
return rc;
}
}

我正在像这样从我的 ASP.NET 页面访问该代码:

int tradeId = DB.InsertTrade (
ddlSymbols.SelectedValue,
ddlTradeSetups.SelectedValue,
int.Parse(ddlTradeTypes.SelectedValue),
decimal.Parse(txtLotsPerUnit.Text),
ddlTimeFrames.Text,
decimal.Parse(txtAcctRisk.Text));

int orderId = DB.InsertOrder (
tradeId,
int.Parse(txtUnits.Text),
radSide.SelectedValue,
Decimal.Parse(txtEntryPrice.Text),
Decimal.Parse(txtSpread.Text),
int.Parse(ddlStrategies.SelectedValue),
"IE",
int.Parse(ddlBrokers.SelectedValue),
radSide.SelectedValue + radOrderType.SelectedValue,
DateTime.Parse(txtEntryDate.Text + " " + txtEntryTime.Text),
txtEntryComments.Text,
int.Parse(ddlAccounts.SelectedValue));

我想要做的是将来自 ASP.NET 页面的调用包装在 SqlTransaction 中。做这个的最好方式是什么?我是否需要稍微重构我的代码?

非常感谢。

最佳答案

使用 TransactionScope反对在一个事务中放置多个语句:

using (TransactionScope scope = new TransactionScope())
{
// Database call 1 - within transaction
// Database call 2 - within same transaction

scope.Complete(); // Commit the transaction, or face an automatic rollback
}

关于c# - 如何将这些调用放在 SqlTransaction 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4124477/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com