需要帮助来停止使用C#重复数据

时间:2017-04-04 11:06:33

标签: c#

我有一个系统可以将一些重新复制到表中。事情就是当我反复点击更新按钮时,它会将重复的记录插入到我的数据库中。请帮我修理一下。 注意:当我尝试使用表中的数据时,这些重复数据会使程序崩溃。

private void button1_Click(object sender, EventArgs e)
    {

        (sender as Button).Enabled = false;


        string ItemCode;
        string ItemDesc;
        string ItID;
        string InItID;
        decimal EntQty;
        string EntUOM;
        decimal TQty;
        string NewRec;
        bool Prt;


        TQty = 0;

        foreach (DataRow RowVal in dataSetMaster.ITEM_LINE)//
        {
            ItemCode = RowVal["InITCode"].ToString();
            ItemDesc = RowVal["InITDesc"].ToString();
            ItID = RowVal["ItemID"].ToString();
            InItID = RowVal["Ingredient_ItemID"].ToString();
            EntQty = Convert.ToDecimal(RowVal["Entry_Qty"].ToString());
            TQty = Convert.ToDecimal(RowVal["T_Qty"].ToString());
            NewRec = RowVal["NewRec"].ToString();
            EntUOM = RowVal["Entry_UOM"].ToString();
            Prt = Convert.ToBoolean(RowVal["Print"].ToString());

            // MessageBox.Show("Items in the grid " + ItemCode + " == " + ItemDesc+" /"+Convert.ToString(EntQty)+" / "+Convert.ToString(TQty));


            string ComdTextItemCode;
            string ComdTextItemCodeInsert;
            string Itcode;
            Itcode = TextBoxCode.Text.Trim();
            string X1;

            X1 = "TEST";
            sqlConnection1.Open();
            if (NewRec == "True")
            {

                Guid newGuid;
                Guid newGuid2;


                if (Guid.TryParseExact(ItID, "D", out newGuid))
                {
                    Console.WriteLine("Converted {0} to a Guid", ItID);
                }
                else
                {
                    Console.WriteLine("Unable to convert {0} to a Guid",
                                      ItID);
                }

                if (Guid.TryParseExact(InItID, "D", out newGuid2))
                {
                    Console.WriteLine("Converted {0} to a Guid", InItID);
                }
                else
                {
                    Console.WriteLine("Unable to convert {0} to a Guid",
                                      InItID);
                }

                //   MessageBox.Show("Test");


                Guid ItmID = newGuid;
                Guid IngItmID = newGuid2;
                Guid GuItem_LineID = Guid.NewGuid();


                //INSERT INTO ITEM_LINE (Item_LineID,ItemID,Ingredient_ItemID,Entry_Qty,Entry_UOM) VALUES (convert(uniqueidentifier, @Item_LineID),convert(uniqueidentifier, @ItemID),convert(uniqueidentifier, @Ingredient_ItemID),@Entry_Qty,@Entry_UOM)

                ComdTextItemCodeInsert = "INSERT INTO ITEM_LINE (Item_LineID,ItemID,Ingredient_ItemID,Entry_Qty,Entry_UOM,[Print],T_Qty)" +
                "VALUES (convert(uniqueidentifier, @Item_LineID),convert(uniqueidentifier, @ItemID),convert(uniqueidentifier, @Ingredient_ItemID),@Entry_Qty,@Entry_UOM,@Print,@T_Qty)";
                sqlInsertItemDtl.CommandText = ComdTextItemCodeInsert;

                sqlInsertItemDtl.Parameters["@Item_LineID"].Value = GuItem_LineID;
                sqlInsertItemDtl.Parameters["@ItemID"].Value = ItmID;            // roleID;
                sqlInsertItemDtl.Parameters["@Ingredient_ItemID"].Value = IngItmID;
                sqlInsertItemDtl.Parameters["@Entry_Qty"].Value = EntQty;
                sqlInsertItemDtl.Parameters["@Entry_UOM"].Value = EntUOM;
                sqlInsertItemDtl.Parameters["@Print"].Value = Prt;
                sqlInsertItemDtl.Parameters["@T_Qty"].Value = TQty;

                sqlDADetail.InsertCommand.ExecuteNonQuery();
            }
           else
           {
               ComdTextItemCode = "UPDATE ITEM_LINE SET ITEM_LINE.T_Qty=" + @TQty +",ITEM_LINE.[Print]='"+ @Prt +
                  "' WHERE ITEM_LINE.ItemID=convert(uniqueidentifier,'" + ItID + "') AND ITEM_LINE.Ingredient_ItemID=convert(uniqueidentifier,'" + InItID + "')";
               sqlUpdateDtl.CommandText = ComdTextItemCode;
               sqlDADetail.UpdateCommand.ExecuteNonQuery();
            }


            sqlConnection1.Close();


        }

        MessageBox.Show("Material Master updated.");


    }

0 个答案:

没有答案