在Wcf Servive中将Sql查询转换为Linq查询

时间:2017-09-28 00:08:10

标签: c# linq wcf

Linq Query提供了更好的可读性和编译错误优势。我有写Linq查询的基本知识但是有一些Sql查询很难转换成Linq。这是一个,我将面临很大的痛苦转换为Linq。如何在Linq下面写下Sql查询。

public bool CurrentAccountDepositDetails(Current_Account_Deposit_Details current_Account_Deposit_Details)
        {
            int j = Convert.ToInt32(current_Account_Deposit_Details.Account_Number);

            using (SqlConnection con = new SqlConnection(ConnectionString))
            {

                SqlConnection cn = new SqlConnection(ConnectionString);
                SqlCommand cmd = new SqlCommand("SELECT(*)FROM Current_Account_Details");
                // cmd.Parameters.AddWithValue("@Account_Number", depositDetails.Account_Number);


                SqlTransaction trans;
                cn.Open();
                trans = cn.BeginTransaction();
                cmd.Connection = cn;

                cmd.Transaction = trans;
                cmd.CommandType = CommandType.Text;

                cmd.CommandText = "UPDATE Current_Account_Details set Account_Balance=Account_Balance +'" + current_Account_Deposit_Details.Amount + "' where Account_Number ='" + current_Account_Deposit_Details.Account_Number + "'";
                //cmd.Parameters.AddWithValue("@Account_Number", depositDetails.Account_Number);

                j = cmd.ExecuteNonQuery();
                if (j == 1)
                {
                    trans.Commit();


                    //Create the SqlCommand object
                    SqlCommand cmd1 = new SqlCommand("Current_Account_Dposit", con);
                    //Specify that the SqlCommand is a stored procedure
                    cmd1.CommandType = System.Data.CommandType.StoredProcedure;

                    //Add the input parameters to the command object
                    cmd1.Parameters.AddWithValue("@Account_Number", current_Account_Deposit_Details.Account_Number);
                    cmd1.Parameters.AddWithValue("@Account_Holder_Name", current_Account_Deposit_Details.Account_Holder_Name);
                    cmd1.Parameters.AddWithValue("@Amount", current_Account_Deposit_Details.Amount);


                    cmd1.Parameters.AddWithValue("@Sort_Code", current_Account_Deposit_Details.Sort_Code);
                    cmd1.Parameters.AddWithValue("@Transcation_Type", current_Account_Deposit_Details.Transcation_Type);
                    cmd1.Parameters.AddWithValue("@Date", current_Account_Deposit_Details.Date);



                    //Open the connection and execute the query
                    con.Open();
                    cmd1.ExecuteNonQuery();
                    con.Close();
                    return true;

                    //Retrieve the value of the output parameter
                }
                else
                {
                    trans.Rollback();
                    return false;
                }



            }
        }

任何建议或反馈或帮助都会受到高度赞赏......

1 个答案:

答案 0 :(得分:0)

是的,可以使用交易 并且很容易将它们转换为Entity框架。 1)开始将DB对象添加到实体框架中(ADO.NET实体数据模型) 2)根据需要开始使用LINQ / LAMBDA。 3)相应地使用Database.BeginTransaction()和context.commit / rollback。

以下代码只是为了给您一个想法。可能无法编译

<强>代码

    date        week        end_date
0   2016-02-22  2016 W08    2016-02-29
1   2016-02-29  2016 W09    2016-03-07
2   2016-03-07  2016 W10    2016-03-14
3   2016-03-14  2016 W11    2016-03-21