コード例 #1
0
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                dbconnection.Open();
                string       query = "select Car_Number from cars where Car_Number='" + txtCarNumber.Text + "'";
                MySqlCommand com   = new MySqlCommand(query, dbconnection);


                if (com.ExecuteScalar() == null)
                {
                    if (txtCarNumber.Text != "")
                    {
                        string qeury = "insert into cars (Car_Number,Car_Capacity,meter_reading,Openning_Account,Car_Value,DepreciationPeriod,PremiumDepreciation)values(@Car_Number,@Car_Capacity,@meter_reading,@Openning_Account,@Car_Value,@DepreciationPeriod,@PremiumDepreciation)";
                        com = new MySqlCommand(qeury, dbconnection);
                        com.Parameters.Add("@Car_Number", MySqlDbType.VarChar, 255);
                        com.Parameters["@Car_Number"].Value = txtCarNumber.Text;
                        com.Parameters.Add("@Car_Capacity", MySqlDbType.Double);
                        com.Parameters["@Car_Capacity"].Value = Convert.ToDouble(txtCarCapacity.Text);
                        com.Parameters.Add("@meter_reading", MySqlDbType.Int32);
                        com.Parameters["@meter_reading"].Value = Convert.ToInt32(txtMeterReading.Text);
                        com.Parameters.Add("@Openning_Account", MySqlDbType.Double);
                        com.Parameters["@Openning_Account"].Value = Convert.ToDouble(txtOpenning_Account.Text);
                        com.Parameters.Add("@Car_Value", MySqlDbType.Double);
                        com.Parameters["@Car_Value"].Value = Convert.ToDouble(txtCarValue.Text);
                        com.Parameters.Add("@DepreciationPeriod", MySqlDbType.Double);
                        com.Parameters["@DepreciationPeriod"].Value = Convert.ToDouble(txtDepreciationPeriod.Text);
                        com.Parameters.Add("@PremiumDepreciation", MySqlDbType.Double);
                        com.Parameters["@PremiumDepreciation"].Value = Convert.ToDouble(txtPremiumDepreciation.Text);

                        com.ExecuteNonQuery();

                        MessageBox.Show("add success");
                        clear();
                        cars.DisplayCars();
                    }
                    else
                    {
                        MessageBox.Show("enter Car Number");
                    }
                }
                else
                {
                    MessageBox.Show("This Car already exist");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            dbconnection.Close();
        }
コード例 #2
0
        private void btnCarUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                dbconnection.Open();

                if (txtCarNumber.Text != "")
                {
                    string       qeury = "update  cars set Car_Number=@Car_Number,Car_Capacity=@Car_Capacity,meter_reading=@meter_reading,Openning_Account=@Openning_Account,Car_Value=@Car_Value,DepreciationPeriod=@DepreciationPeriod,PremiumDepreciation=@PremiumDepreciation where Car_ID=" + row1["car_id"].ToString();
                    MySqlCommand com   = new MySqlCommand(qeury, dbconnection);
                    com.Parameters.Add("@Car_Number", MySqlDbType.VarChar, 255);
                    com.Parameters["@Car_Number"].Value = txtCarNumber.Text;
                    com.Parameters.Add("@Car_Capacity", MySqlDbType.VarChar, 255);
                    com.Parameters["@Car_Capacity"].Value = txtCarCapacity.Text;
                    com.Parameters.Add("@meter_reading", MySqlDbType.Int32);
                    com.Parameters["@meter_reading"].Value = Convert.ToInt32(txtMeterReading.Text);
                    com.Parameters.Add("@Openning_Account", MySqlDbType.Double);
                    com.Parameters["@Openning_Account"].Value = Convert.ToDouble(txtOpenning_Account.Text);
                    com.Parameters.Add("@Car_Value", MySqlDbType.Double);
                    com.Parameters["@Car_Value"].Value = Convert.ToDouble(txtCarValue.Text);
                    com.Parameters.Add("@DepreciationPeriod", MySqlDbType.Double);
                    com.Parameters["@DepreciationPeriod"].Value = Convert.ToDouble(txtDepreciationPeriod.Text);
                    com.Parameters.Add("@PremiumDepreciation", MySqlDbType.Double);
                    com.Parameters["@PremiumDepreciation"].Value = Convert.ToDouble(txtPremiumDepreciation.Text);

                    com.ExecuteNonQuery();
                    cars.DisplayCars();
                    MessageBox.Show("update success");
                }
                else
                {
                    MessageBox.Show("enter Car Number");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            dbconnection.Close();
        }