代码之家  ›  专栏  ›  技术社区  ›  Luiscencio

第二次更新行时出现并发错误C#

  •  0
  • Luiscencio  · 技术社区  · 15 年前

    您好,我收到“并发冲突,updateCommand影响了预期1条记录中的0条。”尝试更新行时出错, 奇怪的是,我可以一次更新该行,下次我在同一行上重复该过程时,就会得到错误信息。 ,我已经尝试过endinit和endedit的想法,任何帮助都将不胜感激!

    我正在使用c_和mysql innodb

    string cs = "server=" + fidaConfig.dtBDConfig.Rows[0][2] + ";user id=root;Password=" + fidaConfig.dtBDConfig.Rows[0][4] + ";persist security info=True;database=facturas"; 
    MySql.Data.MySqlClient.MySqlConnection conn = new MySql.Data.MySqlClient.MySqlConnection() { ConnectionString = cs }; 
    switch (tableInvoice) 
    { 
    case "factura_idj": 
        dsLuiscencioTableAdapters.factura_idjTableAdapter idjAdapter = new Control_Administrativo.dsLuiscencioTableAdapters.factura_idjTableAdapter() { Connection = conn }; 
        dsLuiscencio.factura_idjDataTable idj = idjAdapter.GetData(); 
        var facturaidj = (from f in idj where f.no_factura_idj == InvoiceNumber select f).Single(); 
        if (DateTime.Today.Date >= Convert.ToDateTime("01-01-2010") && facturaidj.fecha.Date <= Convert.ToDateTime("01-01-2010")) 
        { 
            var quieresactualizar = MessageBox.Show("Desea Actualizar el total de acuerdo a los nuevos impuestos?", "Reforma Fiscal", MessageBoxButtons.YesNo); 
            if (quieresactualizar == DialogResult.Yes) 
            { 
                switch (facturaidj.opt_iva) 
                { 
            case 1: 
                facturaidj.iva = 0; 
                facturaidj.total = facturaidj.subtotal; 
                break; 
            case 2: 
                facturaidj.iva = facturaidj.subtotal * 0.11; 
                facturaidj.total = facturaidj.subtotal * 1.11; 
                break; 
            case 3: 
                facturaidj.iva = facturaidj.subtotal * 0.16; 
                facturaidj.total = facturaidj.subtotal * 1.16; 
                break; 
            default: 
                break; 
    
                } 
                Number2Letter.Number2Letter n2l = new Number2Letter.Number2Letter(); 
                string totalwithnocents = n2l.Numero2Letra(facturaidj.total.ToString(), 0, 0, "peso", "", 
            Number2Letter.Number2Letter.eSexo.Masculino, 
            Number2Letter.Number2Letter.eSexo.Masculino).ToUpper(); 
    
                string strtotalconivaretenido = Math.Round(facturaidj.total, 2, MidpointRounding.ToEven).ToString("###########.00"); 
                string cents = strtotalconivaretenido.Substring(strtotalconivaretenido.IndexOf(".") + 1); 
    
                facturaidj.total_letra = string.Format(@"{0} {1}/100 {2}", totalwithnocents, cents, facturaidj.tipo_moneda).ToUpper(); 
                idj.EndInit(); 
                idjAdapter.Update(facturaidj);//this runs only the first time on a row, then throws the error 
            } 
        } 
        break; 
        continues......
    
    3 回复  |  直到 15 年前
        1
  •  1
  •   Jorge Córdoba    15 年前

    我不知道这会不会有什么用,除了:

    我终于发现了我的问题,这是 与脑震荡无关,我 通过测试发现了问题 在SQL Server数据库上的代码,它是 给我另一个错误信息,所以 然后我发现可能是MS Access 他的错误是错误的,而且 问题出在别的地方!

    此外,你还有 FLOAT issue 前面提到:

    mysqlcommandbuilder编写 使用where col=更新命令?1和 COL=?2…在浮动列中 永远不要用这种方法找到精确的值。

    它可以完美地扩展到双值…当更新尝试查找要更新的值时,问题似乎是错误消息的组合。

    你能试着运行更新命令而不实际更新任何东西吗?也就是说,不要进行实际的更新,而是删除除update命令之外的所有代码,以便factura对象完全相同,并查看是否有效。

        2
  •  0
  •   CSharpAtl    15 年前

    我不确定你是否在使用 float 数据库中的数据类型,但本文可能是您遇到的问题: MySql - Float Issue

        3
  •  0
  •   Nick    15 年前

    idjadapter.update()后面的SQL是什么?它是如何生成的?在Visual Studio中是否使用了拖放设计器?如果这样做了,那么您想检查自动生成的更新脚本吗?根据我在MS SQL世界中使用设计器代码的经验,它通常会包含一个非常复杂的WHERE语句,其中只有当行的每个旧字段仍然存在时,它才会更新。

    如果您的表恰好包含一个自动生成的时间戳字段或类似的内容,那么您可能会遇到两个用户的问题,在这两个用户的后面可能会更新此字段,因此更新中的位置将失败,从而导致适配器引发此并发异常。

    然后你需要决定如何处理这个问题。如果只想在Wins逻辑中保留最后一个,则需要在设计器中(从该表适配器的设计器的“属性”窗口)手动更改更新SQL,使其仅在UPDATE语句的WHERE子句中包含该表的主键。