【问题标题】:How to submit all MySQL command only by one time Clicking on Button Event in C# Winforms如何在C# Winforms中通过一次点击按钮事件提交所有的MySQL命令
【发布时间】:2022-12-08 02:22:16
【问题描述】:

亲爱的人们,

  1. btnSubmit_Click 无法处理代码并在 MySql 语法错误中显示错误。

  2. 我只想将文本框输入插入数据库,同时从数据库中选择 Cus_ID 并重新插入到 Orders 表中。

  3. 最后显示订单ID

    使用系统; 使用 System.Collections.Generic; 使用 System.ComponentModel; 使用系统数据; 使用系统绘图; 使用 System.Linq; 使用系统文本; 使用 System.Threading.Tasks; 使用 System.Windows.Forms; 使用 MySql.Data.MySqlClient;

    命名空间项目 { 公共部分类 CoffeeMania:表格 { 公共 CoffeeMania() { 初始化组件(); }

         private void CoffeeMania_Load(object sender, EventArgs e)
         {
             cmbBranchArea.Items.Add("PJ");
             cmbBranchArea.Items.Add("KL");
    
             cmbCusGender.Items.Add("Male");
             cmbCusGender.Items.Add("Female");
         }
    
         private void btnSubmit_Click(object sender, EventArgs e)
         {
             string dburl = System.Configuration.ConfigurationManager.ConnectionStrings["dburl"].ConnectionString;
             MySqlConnection conn = new MySqlConnection(dburl);
    
             int qty = int.Parse(txtAmericano.Text + txtCappuccino.Text + txtMocha.Text + txtEspresso.Text + txtLatte.Text);
    
             string sql = "INSERT INTO customer(Cus_Name, Cus_Gender, Cus_Phone) VALUES('"+txtCusName+"', '"+cmbCusGender.SelectedItem+"', '"+txtCusPhone.Text+"';)";
    
             string sql2 = "select Cus_ID from customer where Cus_Name='" + txtCusName.Text + "' and Cus_Gender='" + cmbCusGender.SelectedItem + "' and Cus_Phone='" + txtCusPhone.Text + "'; ";
    
             try
             {
    
                 conn.Open();
                 MySqlCommand comm = new MySqlCommand(sql, conn);
                 int record = Convert.ToInt32(comm.ExecuteNonQuery());
                 Console.WriteLine(record);
    
                 MySqlCommand comm2 = new MySqlCommand(sql2, conn);
                 MySqlDataReader reader2 = comm2.ExecuteReader();
                 if(reader2.Read())
                 {
                     int CusID = Convert.ToInt32(reader2["Cus_ID"]);
    
                     string sql3 = "INSERT INTO orders(Orders_Qty, Cus_ID, Branch_ID) VALUES('" + qty + "', '" + CusID + "', '" + cmbBranchArea.SelectedItem + "';)";
    
                     MySqlCommand comm3 = new MySqlCommand(sql3, conn);
                     int record3 = Convert.ToInt32(comm3.ExecuteNonQuery());
                     Console.WriteLine(record3);
    
                     string sql4 = "select Orders_ID from orders where Cus_ID='" + CusID + "';";
                     MySqlCommand comm4 = new MySqlCommand(sql4, conn);
                     MySqlDataReader reader4 = comm4.ExecuteReader();
                     if(reader4.Read())
                     {
                         MessageBox.Show("Your Order ID is: " + reader4["Orders_ID"]);
                     }
    
                 }
    
                 lblShowVoteResult.Text = "Successfully submitted";
    
                 comm.Dispose();
                 conn.Close();
             }
    
             catch(MySqlException ex)
             {
                 MessageBox.Show(ex.Message);
             }
         }
     }
    

    }

【问题讨论】:

    标签: c# mysql winforms mysqlcommand


    【解决方案1】:

    您得到语法错误是因为您在“)”之前插入了分号。

    例如改变:

    string sql3 = "INSERT INTO orders(Orders_Qty, Cus_ID, Branch_ID) VALUES('" + qty + "', '" + CusID + "', '" + cmbBranchArea.SelectedItem + "';)";

    string sql3 = "INSERT INTO orders(Orders_Qty, Cus_ID, Branch_ID) VALUES('" + qty + "', '" + CusID + "', '" + cmbBranchArea.SelectedItem + "');";

    和其他地方。

    【讨论】:

      猜你喜欢
      • 2014-12-02
      • 1970-01-01
      • 1970-01-01
      • 2022-11-30
      • 1970-01-01
      • 1970-01-01
      • 2012-07-26
      • 2013-03-24
      • 2019-07-05
      相关资源
      最近更新 更多