我无法在C#& SQLSERVER2008 [英] i can't get 2 query in C# & SQLSERVER2008

查看:121
本文介绍了我无法在C#& SQLSERVER2008的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不能得到2个查询

只是工作字符串查询

和查询不要





i can''t get 2 query
just working string query
and query dont


      internal bool saveNewPayment(Guid ID, Guid creditID, decimal paymentAmount, DateTime dateTime)
      {
          bool flag = true;
          using (SqlConnection con = new SqlConnection(constring))
          {
              con.Open();
              SqlTransaction sqlTranc = con.BeginTransaction();
              SqlCommand com = con.CreateCommand();
              com.Transaction = sqlTranc;

              try
              {
                  string payAmount = paymentAmount.ToString(CultureInfo.InvariantCulture.NumberFormat);


                 string query = string.Format("INSERT INTO Payment (ID, CreditsID, Amount, PaymenDate)" + " VALUES ('{0}', '{1}', '{2}', '{3}')",
                   ID, creditID, payAmount, dateTime.ToString("MM/dd/yyyy"));

                  com.CommandText = query;
                  com.ExecuteNonQuery();

                 query = string.Format("UPDATE Credits SET Balance = (Balance - {0}) WHERE ID = '{1}'", payAmount, creditID);

                  com.CommandText = query;
                  com.ExecuteNonQuery();


                  sqlTranc.Commit();

              }
              catch (Exception)
              {
                  sqlTranc.Rollback();
                  flag = false;
              }
              finally
              {
                  if (con.State == System.Data.ConnectionState.Open)
                      con.Dispose();
              }
          }
          return flag;

      }
  }
}

推荐答案

因为你使用语句开头之前的con.BeginTransaction()实际插入将不会完成,直到你调用sqlTranc.Commit();



因此显而易见的是creditID您尝试更新的内容在表中不可用且无法更新。

在插入完成后移动Commit语句并尝试运行更新查询。
since you use the con.BeginTransaction() before the beginning of statement the real insert will not complete until you make call to sqlTranc.Commit();

so its obvious that the creditID which you are trying to update is not available in the table and it failed to update.
move the Commit statement just after the insert is completed and try running the update query.


这篇关于我无法在C#& SQLSERVER2008的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆