我將一個sql Transcation查詢轉換爲Linq查詢。我已經做了一點點,我試圖寫其餘的linq查詢,但我很困惑。Wcf Rest服務中的SQL查詢到Linq查詢
這是ADO.NET代碼與Sql查詢。
public bool RewardToCurrentTranscation(MoneyTransfer mopneyTransfer)
{
int amount = System.Convert.ToInt32(mopneyTransfer.Amount);
int amount1 = System.Convert.ToInt32(mopneyTransfer.Amount1);
SqlConnection cn = new SqlConnection(ConnectionString);
string sql = "select Account_Balance from Reward_Account_Details where Account_Number='" + mopneyTransfer.Sender_Account_No + "'";
SqlCommand cmd = new SqlCommand(sql, cn);
if (cn.State == ConnectionState.Closed)
cn.Open();
//amount = int.Parse(cmd.ExecuteScalar().ToString());
if (amount > 0)
{
int b;
int b1;
SqlCommand cmd1 = new SqlCommand();
SqlTransaction trans;
if (cn.State == ConnectionState.Closed)
cn.Open();
trans = cn.BeginTransaction();
cmd1.Connection = cn;
cmd1.CommandType = CommandType.Text;
cmd1.Transaction = trans;
cmd1.CommandText = "update Reward_Account_Details set Account_Balance=Account_Balance-'" + mopneyTransfer.Amount + "' where Account_Number='" + mopneyTransfer.Sender_Account_No + "'";
b = cmd1.ExecuteNonQuery();
cmd1.CommandText = "update Current_Account_Details set Account_Balance=Account_Balance+'" + mopneyTransfer.Amount1 + "' where Account_Number='" + mopneyTransfer.Receiver_Account_No + "'";
b1 = cmd1.ExecuteNonQuery();
if (b == 1 && b1 == 1)
{
trans.Commit();
using (SqlConnection con = new SqlConnection(ConnectionString))
{
//Create the SqlCommand object
//Create the SqlCommand object
SqlCommand cmd3 = new SqlCommand("Reward_Account_Wdraw", con);
//Specify that the SqlCommand is a stored procedure
cmd3.CommandType = System.Data.CommandType.StoredProcedure;
//Add the input parameters to the command object
cmd3.Parameters.AddWithValue("@Account_Number", mopneyTransfer.Sender_Account_No);
cmd3.Parameters.AddWithValue("@Account_Holder_Name", mopneyTransfer.Sender_Name);
cmd3.Parameters.AddWithValue("@Amount", mopneyTransfer.Amount);
cmd3.Parameters.AddWithValue("@Sort_Code", mopneyTransfer.Sender_Sort_Code);
cmd3.Parameters.AddWithValue("@Transcation_Type", mopneyTransfer.Transcation_Type);
cmd3.Parameters.AddWithValue("@Date", mopneyTransfer.Date);
SqlCommand cmd2 = new SqlCommand("Current_Account_Dposit", con);
//Specify that the SqlCommand is a stored procedure
cmd2.CommandType = System.Data.CommandType.StoredProcedure;
//Add the input parameters to the command object
cmd2.Parameters.AddWithValue("@Account_Number", mopneyTransfer.Receiver_Account_No);
cmd2.Parameters.AddWithValue("@Account_Holder_Name", mopneyTransfer.Receiver_Name);
cmd2.Parameters.AddWithValue("@Amount", mopneyTransfer.Amount1);
cmd2.Parameters.AddWithValue("@Sort_Code", mopneyTransfer.Receiver_Sort_Code);
cmd2.Parameters.AddWithValue("@Transcation_Type", mopneyTransfer.Transcation_Type1);
cmd2.Parameters.AddWithValue("@Date", mopneyTransfer.Date1);
//Open the connection and execute the query
con.Open();
cmd2.ExecuteNonQuery();
cmd3.ExecuteNonQuery();
return true;
//con.Close();
}
}
else
trans.Rollback();
return false;
}
return false;
}
到目前爲止,我已經做了這麼多的LINQ ...
public bool MoneyTranfer(MoneyTransfer mopneyTransfer)
{
int i = Convert.ToInt32(mopneyTransfer.Amount);
int j = Convert.ToInt32(mopneyTransfer.Amount1);
using (HalifaxDatabaseEntities ctx = new HalifaxDatabaseEntities())
{
var foundAccount = (from a in ctx.Current_Account_Details where a.Account_Number.Equals(mopneyTransfer.Sender_Account_No)
select a).Distinct().FirstOrDefault();
using (var dbContextTransaction = ctx.Database.BeginTransaction())
{
var acctDetails = (from a in ctx.Current_Account_Details
where a.Account_Number.Equals(mopneyTransfer.Sender_Account_No)
select a).Distinct().FirstOrDefault();
if (acctDetails != null)
{
ctx.SaveChanges();
dbContextTransaction.Commit();
}
else
{
dbContextTransaction.Rollback();
return false;
}
}
}
}
我對這個代碼混淆..
cmd1.CommandText = "update Reward_Account_Details set Account_Balance=Account_Balance-'" + mopneyTransfer.Amount + "' where Account_Number='" + mopneyTransfer.Sender_Account_No + "'";
任何一個可以幫助我如何完成在linq中查詢的其餘部分
LINQ不會處理事務。它用於讀取數據。 – Neolisk
是否有解決這個問題的方法? – Mohammad
不是它的設計。你應該只使用LINQ來讀取數據。使用ADO.NET更新數據非常好。如果你想要更先進的東西,看看實體框架。 – Neolisk