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

时间:2010-01-05 01:17:48

标签: c# mysql

HI我收到“并发违规,updatecommand影响了预期的1条记录中的0条。”尝试更新行时出错,奇怪的是我可以一次更新行,下次我在同一行重复这个过程我得到了错误,我已经尝试了endinit和不管怎么说,任何帮助都会受到赞赏!

我正在使用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 个答案:

答案 0 :(得分:1)

我不知道这是否有任何关系,但是:

  

我最终找到了我的问题,这是   我完全没有相关的可靠性   通过测试发现问题   它是SQL Server数据库上的代码   给我另一个错误信息,所以   然后我想出可能是MS Access   他的错误和错误是错误的   问题是别的!

此外,您之前提到了FLOAT issue

  

MySqlCommandBuilder组成了   使用WHERE col =?1 AND的UPDATE命令   col =?2 ...在FLOAT列中你会   永远不会以这种方式找到确切的价值。

哪个可以完美地扩展到double值...当更新尝试查找要更新的值时,问题似乎是不正确的错误消息的组合

您是否可以尝试运行更新命令而不实际更新任何内容?也就是说,不是进行实际更新,而是删除除Update命令之外的所有代码,以便factura对象完全相同,看看是否有效。

答案 1 :(得分:0)

我不确定您是否在数据库中使用float数据类型,但本文可能就是您发生的事情:MySql - Float Issue

答案 2 :(得分:0)

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

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

然后,您需要决定如何处理该问题。如果您只想在wins逻辑中使用last,那么您需要手动更改设计器中的Update SQL(从该表适配器的设计器的属性窗口),以仅在WHERE子句中包含表的主键。更新声明。