简体   繁体   中英

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.

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. As this is probably your defining column in your table, you should create a so-called primary key on your car registration column:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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