示例#1
0
        private void BtnInsert_Click(object sender, EventArgs e)
        {
            int             tempstaffid = 0;
            int             tempname    = 0;
            MySqlConnection conn        = DBUtils.GetDBConnection();

            conn.Open();
            MemoryStream ms = new MemoryStream();

            pictureBox1.Image.Save(ms, pictureBox1.Image.RawFormat);
            byte[]       img     = ms.ToArray();
            MySqlCommand command = new MySqlCommand("INSERT INTO Products (pid, pname, typename, brandname, wholeprice, retailprice, qty, image, dateadd) VALUES (@pid, @pname, @typename, @brandname, @wholeprice, @retailprice, @qty, @image, @dateadd)", conn);

            command.Parameters.Add("@pid", MySqlDbType.VarChar).Value   = txtProductid.Text;
            command.Parameters.Add("@pname", MySqlDbType.VarChar).Value = txtProductname.Text;
            Object typecb = comboType.SelectedItem;

            command.Parameters.Add("@typename", MySqlDbType.VarChar).Value = typecb.ToString();
            Object brandcb = comboBrand.SelectedItem;

            command.Parameters.Add("@brandname", MySqlDbType.VarChar).Value = brandcb.ToString();
            command.Parameters.Add("@wholeprice", MySqlDbType.Int32).Value  = txtWholeprice.Text;
            command.Parameters.Add("@retailprice", MySqlDbType.Int32).Value = txtRetailprice.Text;
            command.Parameters.Add("@qty", MySqlDbType.Int32).Value         = txtQty.Text;
            command.Parameters.Add("@dateadd", MySqlDbType.DateTime).Value  = DateTime.Now;
            command.Parameters.Add("@image", MySqlDbType.Blob).Value        = img;
            using (var cmd = new MySqlCommand("Select pid from Products WHERE pid = '" + txtProductid.Text + "'", conn))
            {
                string reid = Convert.ToString(cmd.ExecuteScalar());
                resultpname.Text = reid;
                if (resultpname.Text.Length > 0)
                {
                    Alert.ThatShow("Products ID already exists", Alert.AlertType.warning);
                }
                else
                {
                    tempstaffid = 1;
                }
            }
            using (var cmd1 = new MySqlCommand("Select pname from Products WHERE pid = '" + txtProductname.Text + "'", conn))
            {
                string repname = Convert.ToString(cmd1.ExecuteScalar());
                resultpid.Text = repname;
                if (resultpid.Text.Length > 0)
                {
                    Alert.ThatShow("Product Name already exists", Alert.AlertType.warning);
                }
                else
                {
                    tempname = 1;
                }
            }
            if (string.IsNullOrEmpty(txtProductid.Text) || txtProductid.Text == "Product ID" || string.IsNullOrEmpty(txtProductname.Text) || txtProductname.Text == "Product Name" || comboType.SelectedIndex == 0 || comboBrand.SelectedIndex == 0 || string.IsNullOrEmpty(txtWholeprice.Text) || txtWholeprice.Text == "Wholesale Price" || string.IsNullOrEmpty(txtRetailprice.Text) || txtRetailprice.Text == "Retail Price" || string.IsNullOrEmpty(txtQty.Text) || txtQty.Text == "Qty.")
            {
                Alert.ThatShow("Don't leave blank", Alert.AlertType.error);
            }
            else if (tempstaffid == 1 && tempname == 1)
            {
                ExecMyQuery(command, "Inserted Successfuly");
            }

            conn.Close();
        }