ado.net中的交易错误 [英] error in transaction in ado.net

查看:73
本文介绍了ado.net中的交易错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在交易中收到错误...我创建了表Tbl_Invoice(Invoice_Id,Product_Name,Amount,Product_Quantity,Product_Price)和表Tbl_Product(Product_Id,Product_Name,Product_Quantity,Product_Amount)



现在我有一个按钮,我需要通过它来添加文本框(产品名称,数量,产品数量,产品价格)的数据,这些数据由用户运行时填充到网格中,并且我填充了3行网格和当我点击保存按钮

getting an error in transaction ...i have created table Tbl_Invoice (Invoice_Id ,Product_Name,Amount,Product_Quantity,Product_Price) and table Tbl_Product(Product_Id,Product_Name,Product_Quantity,Product_Amount)

now i have one button through which i need to add the data of textboxes(Product Name,Amount,Product Quantity,Product Price) which is filled by user runtime into the grid and i made filled 3 rows of grid and when i click on save button

protected void save_Click(object sender, EventArgs e)
    { SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["test"].ConnectionString);

            con.Open();
            SqlTransaction Trans = con.BeginTransaction();

            SqlCommand com = new SqlCommand();
            com.Connection = con;
            com.Transaction = Trans;

        foreach (GridViewRow g1 in grd.Rows)
        {


           
            try
            {
                if (Convert.ToInt32(g1.Cells[4].Text) > 10)
                {//created exception on qty>10 if filled by user so that it should roll back the whole transaction
                    throw (new Exception("not greater than 10"));
                }
                com.CommandText = "insert into Tbl_Invoice(Product_Name,Amount,Product_Quantity,Product_Price) values ('" + g1.Cells[2].Text + "'," + Convert.ToDouble(g1.Cells[3].Text) + "," + Convert.ToInt32(g1.Cells[4].Text) + "," + Convert.ToDouble(g1.Cells[5].Text) + ")";
                com.CommandText = "update Tbl_Product  set Product_Quantity=Product_Quantity+" + Convert.ToInt32(g1.Cells[4].Text) + ", Product_Amount=Product_Amount+" + Convert.ToDouble(g1.Cells[3].Text) + " where Product_Name='" + g1.Cells[2].Text + "'";
                
                com.ExecuteNonQuery();
               
                Response.Write("both are written to tables");
            }
            catch (Exception ep)
            {
                Trans.Rollback();
                Response.Write(ep.ToString());
                Response.Write("neither are written to tables");
            }
            finally
            {
                Trans.Commit();
                con.Close();
            }   }    }





运行时在网格中添加3行(产品名称,金额,产品数量,产品价格)



第一排衬衫,125,1,125

第二排领带,50,2,25

第三排喘气,1500,12,50



这些是虚拟值我放在这里是格栅

现在按照例外我创建的数量> 10应该回滚整个事务,但它只回滚最后一行而不是整个事务,因为根据酸属性应该执行所有查询或不执行查询。它影响了Tbl_Invoice和Tbl_Product

前两行是不希望..我错过了什么或写错了

请告诉,问候



runtime 3 rows were added in grid repective (Product Name,Amount,Product Quantity,Product Price)

first row Shirt,125,1,125
second row tie,50,2,25
third row pant,1500,12,50

these are dummy values i put here which is in grid
now as per exception i created qty>10 should roll back whole transaction but it only roll back last row not the whole transaction as per acid properties either should execute all queries or no query .It has affected Tbl_Invoice and Tbl_Product
with first 2 rows which is not desired ..what i missed or written wrong
pls tell,regards

推荐答案

您可以在交易期间设置保存点,如果是失败,你可以回滚到该保存点。有关更多信息,请参阅保存交易 [ ^ ]



好好运,

OI
You can set a savepoint during the transaction and if it fails you can rollback to that save point. FOr more info you should look into SAVE TRANSACTION[^]

Good luck,
OI


只执行更新语句。

你需要

Only the update statement is executed.
You need
com.CommandText = "insert into..."
com.ExecuteNonQuery();
com.CommandText = "update..."
com.ExecuteNonQuery();



此外,你在finally块中有Trans.Commit(),可以在Trans.Rollback()之后调用。




我没有注意到有一个循环。尝试类似这样的事情......


Also, you have Trans.Commit() in finally block, which may be called after Trans.Rollback().


I havent noticed there is a loop. Try something like this..

try
{

  foreach...
  {
  ...
  com.CommandText = "insert into..."
  com.ExecuteNonQuery();
  com.CommandText = "update..."
  com.ExecuteNonQuery();
  }

  Trans.Commit()
}
catch ...
{
  Trans.Rolback()
}


这篇关于ado.net中的交易错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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