ADO.NET错误:在SQL Server上插入重复记录

时间:2014-08-23 01:06:42

标签: sql-server-2008 ado.net

这很奇怪。我有这个POS winform应用程序通过ADO.NET连接到SQL Server 2008 R2数据库。它工作正常,每天保存数千条记录,但我注意到一个奇怪的行为:当客户端机器在保存记录时因任何原因挂起然后重新启动时,下次我的应用程序运行时,它会将以前的数据集重新发送到数据库甚至两次,导致重复的销售条目。我想ADO.NET层保持事务尽管已保存但未保存。

public int UpdateVentas(dsetVentas ds)
    {
        Int32 _newId = 0;
        SqlDataAdapter da = null;
        SqlDataAdapter daDetalle = null;
        SqlDataAdapter daPagos = null;
        using (TransactionScope scope = Utils.GetTransactionScope())
        {
            try
            {
                conn.Open();
                //tran = conn.BeginTransaction();
                // prepare adapters
                da = getDA2updateVentas(conn);
                daDetalle = getDA2updateVentasDetalle(conn);
                daPagos = getDA2updatePagos(conn);
                // prepare table of deleted, added and modified details
                DataTable DeletedDetalle = ds.VentasDetalle.GetChanges(DataRowState.Deleted);
                DataTable AddedDetalle = ds.VentasDetalle.GetChanges(DataRowState.Added);
                DataTable ModifiedDetalle = ds.VentasDetalle.GetChanges(DataRowState.Modified);

                // payments
                DataTable DeletedPagos = ds.Pagos.GetChanges(DataRowState.Deleted);
                DataTable AddedPagos = ds.Pagos.GetChanges(DataRowState.Added);
                DataTable ModifiedPagos = ds.Pagos.GetChanges(DataRowState.Modified);

                // execute in proper order

                // deleted rows
                if ((DeletedDetalle != null))
                {
                    daDetalle.Update(DeletedDetalle);
                }
                if (DeletedPagos != null)
                {
                    daPagos.Update(DeletedPagos);
                }

                // save main record
                // ///////////////////////////////////
                //
                da.Update(ds, "Ventas");
                //
                // ///////////////////////////////////
                // gets new ID
                //dsetVentas.VentasRow r = (dsetVentas.VentasRow)ds.Ventas.Rows[0]; //_newId = r.IdVenta;
                _newId = ds.Ventas[0].IdVenta;
                // updates details & payments
                if ((ModifiedDetalle != null))
                {
                    daDetalle.Update(ModifiedDetalle);
                }
                if (ModifiedPagos != null)
                {
                    daPagos.Update(ModifiedPagos);
                }

                // inserts details & payments
                if ((AddedDetalle != null))
                {
                    foreach (dsetVentas.VentasDetalleRow detalle in AddedDetalle.Rows)
                    {
                        detalle.IdVenta = _newId;
                    }
                    daDetalle.Update(AddedDetalle);
                }

                if (AddedPagos != null)
                {
                    foreach (dsetVentas.PagosRow pago in AddedPagos.Rows)
                    {
                        pago.IdDocumento = _newId;
                    }
                    daPagos.Update(AddedPagos);
                }
                scope.Complete();
                ds.AcceptChanges();
            }
            catch (Exception ex1)
            {
                _newId = 0;
                _ErrorMessage = ex1.ToString();
            }
            finally
            {
                if (conn.State != ConnectionState.Closed)
                {
                    conn.Close();
                }
            }
        }
        da = null;
        return _newId;
    }

0 个答案:

没有答案