简体   繁体   English

C#数据库接口检查SQL重复项

[英]C# Database Interface Checking SQL Duplicates

Ok, im buiding a stock system for a car dealer which has the abillity to add new cars to the system, Now i need to add a feature if the dealer types in a car REG which is already in the database it would bring up an error message box. 好的,我为汽车经销商建立了一个库存系统,该系统具有向系统中添加新汽车的能力,现在,我需要添加一个功能,如果经销商输入数据库中已经存在的汽车REG,它将产生一个错误消息框。

How would i do this? 我该怎么办?

Heres the code: 这是代码:

MySqlCommand cmd = new MySqlCommand();

            String carManufacture = textBoxCARManufac.Text.ToUpper();
            String carModel = textBoxCARModel.Text.ToUpper();
            String carColour = textBoxCarBodColo.Text.ToUpper();
            int carMileage = int.Parse(textBoxCArMillea.Text);
            int carYearReg = int.Parse(textBoxCARREGYear.Text);
            String carReg;
            String fuel = textBoxCARFuel.Text.ToUpper();
            int carEng = int.Parse(textBoxCarEngSize.Text);
            float carprice = float.Parse(textBoxCARPrice.Text);
            float carTaxcost = float.Parse(textBoxCArTax.Text);

            carReg = Regex.Replace(textBox1CARregplate.Text, " ", "").ToUpper();
            try
            {
                cmd.CommandText = "INSERT INTO stockList (Manufacturer,CarModel,BodyColour,AquiredMileage,RegistartionYear,CarRegistration,FuelType,EngineSize,Price,CarTaxCost12Months) VALUES (@Manufacturer, @carModel, @carColour, @carMileage, @carYearReg, @carReg, @fuel, @carEng, @carPrice, @carTaxCost)";

                cmd = SDS1.Prepare(cmd);

                cmd.Parameters.AddWithValue("@Manufacturer", carManufacture);
                cmd.Parameters.AddWithValue("@carModel" , carModel);
                cmd.Parameters.AddWithValue("@carColour" , carColour);
                cmd.Parameters.AddWithValue("@carMileage" , carMileage);
                cmd.Parameters.AddWithValue("@carYearReg" , carYearReg);
                cmd.Parameters.AddWithValue("@carReg" , carReg);
                cmd.Parameters.AddWithValue("@fuel" , fuel);
                cmd.Parameters.AddWithValue("@carEng" , carEng);
                cmd.Parameters.AddWithValue("@carPrice" , carprice);
                cmd.Parameters.AddWithValue("@carTaxCost", carTaxcost);


                SDS1.Query(cmd);
            }
            catch (Exception ex)
            {
                Console.WriteLine("ERROR: " + ex);
            }



        }

I'd suggest you first make sure that no two identical car registration are in the database. 我建议您首先确保数据库中没有两个相同的汽车注册。

I assume, a car registration is a unique key in your scenario. 我认为,在您的情况下,汽车登记是唯一的关键。 You can create a unique key constraint on your table. 您可以在表上创建unique key约束。 As this is probably your defining column in your table, you should create a so-called primary key on your car registration column: 由于这可能是表中的定义列,因此您应该在汽车注册列上创建一个所谓的primary key

ALTER TABLE stockList
ADD CONSTRAINT PK_stockList_CarRegistration PRIMARY KEY (CarRegistration)

Now, if you insert the same key for a second time, the database will throw an exception. 现在,如果您第二次插入相同的密钥,则数据库将引发异常。

You need to catch this exception and tell the user nicely what he did wrong. 您需要捕获此异常,并很好地告诉用户他做错了什么。

If you want a comfortable UI, you might want to load all existing car registrations from the database and tell him that this one alredy exists before the user types in all the other fields. 如果您想要一个舒适的用户界面,则可能希望从数据库中加载所有现有的汽车注册信息,并告诉他在用户在所有其他字段中键入之前,此提示已存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM