Ejemplo n.º 1
0
        private void button1_Click(object sender, EventArgs e)
        {
            //MessageBox.Show("Item has been added to the database!!");

            string vid   = textBox1.Text;
            double price = 0;
            string model = "";

            try
            {
                int count = 0;

                //This is my connection string i have assigned the database file address path
                string MyConnection2 = "datasource=localhost;port=3306;username=root;password=aman";
                //This is my insert query in which i am taking input from the user through windows forms
                string Query = "(SELECT V_MODEL,V_PRICE FROM automobile.vehicles where V_ID ='" + vid + "');";// and Password = '******')";
                //This is  MySqlConnection here i have created the object and pass my connection string.
                MySqlConnection MyConn2 = new MySqlConnection(MyConnection2);
                //This is command class which will handle the query and connection object.
                MySqlCommand    MyCommand2 = new MySqlCommand(Query, MyConn2);
                MySqlDataReader MyReader2;
                MyConn2.Open();
                MyReader2 = MyCommand2.ExecuteReader();     // Here our query will be executed and data saved into the database.
                //MessageBox.Show("Save Data");
                while (MyReader2.Read())
                {
                    model = MyReader2.GetString("V_MODEL");
                    price = MyReader2.GetDouble("V_PRICE");
                }


                //textBox2.Text = count.ToString();
                MyConn2.Close();
            }
            catch (Exception ex)
            {
            }



            try
            {
                //This is my connection string i have assigned the database file address path
                string MyConnection2 = "datasource=localhost;port=3306;username=root;password=aman";
                //This is my insert query in which i am taking input from the user through windows forms
                string a     = "SA4";
                string show  = "S1";
                string Query = "insert into automobile.sales values('" + a + "','" + vid + "','" + Form2.id + "','" + show + "'," + price + ",'" + model + "');";
                //This is  MySqlConnection here i have created the object and pass my connection string.
                MySqlConnection MyConn2 = new MySqlConnection(MyConnection2);
                //This is command class which will handle the query and connection object.
                MySqlCommand    MyCommand2 = new MySqlCommand(Query, MyConn2);
                MySqlDataReader MyReader2;
                MyConn2.Open();
                MyReader2 = MyCommand2.ExecuteReader();     // Here our query will be executed and data saved into the database.
                MessageBox.Show("Save Data");
                while (MyReader2.Read())
                {
                }
                MyConn2.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }