如何单击一次按钮执行2个Sql语句

时间:2011-09-13 03:06:17

标签: c# database visual-studio-2010 insert

我已设法使用wamp运行此查询。

INSERT INTO guest (guestno,familyname)
    VALUES(NULL,'Damn');      

INSERT INTO reservation (reservationno, guestno)
    VALUES(NUll,LAST_INSERT_ID())

但是,如果我单独执行这两个插入语句,我将有一个外键约束。 我认为他们俩都需要同时执行。

我的问题是:

  1. 如何将其合并到我的c#winform代码中?
  2. 一个按钮上是否可以有2个插入语句?
  3. 当用户按下“添加预约”时,我希望执行两个MySQl查询。

    这是我的插入声明:

    private void button7_Click(object sender, EventArgs e)
            {
        string connectionString =
                "Server=localhost;" +
            "Database=sad;" +
            "User ID=root;" +
            "Password=root;" +
            "Pooling=false";
    
            IDbConnection dbcon;
            dbcon = new MySqlConnection(connectionString);
            dbcon.Open();
    
            IDbCommand dbcmd = dbcon.CreateCommand();
            string sql = "<insert statement>";
            dbcmd.CommandText = sql;
            IDataReader reader = dbcmd.ExecuteReader();
            reader.Read(); 
    

    }

    更新版本(不工作)

    string connectionString =
             "Server=localhost;" +
             "Database=sad;" +
         "User ID=root;" +
         "Password=root;" +
         "Pooling=false";
        Form3 f3 = new Form3();
    
            IDbConnection dbcon;
            dbcon = new MySqlConnection(connectionString);
            dbcon.Open();
            IDbCommand dbcmd = dbcon.CreateCommand();
            string sql = "insert into guest (guestno, familyname) values (null, '" + textBox6.Text + "'); insert into reservation (reservationno, guestno) values (null, LAST_INSERT_ID())";
            dbcmd.CommandText = sql;
    
            IDataReader reader = dbcmd.ExecuteReader();
            reader.Read();
            MessageBox.Show("Added Guest Reservation Successfully");
            f3.guestList();
            f3.reservationList();
    

    更新了第3号(仍然无效)

    string connectionString =
                 "Server=localhost;" +
                 "Database=sad;" +
                 "User ID=root;" +
                 "Password=root;" +
                 "Pooling=false";
    
                IDbConnection dbcon;
                dbcon = new MySqlConnection(connectionString);
                dbcon.Open();
                IDbCommand dbcmd = dbcon.CreateCommand();
                dbcmd = new MySqlCommand("CreateGuestAndReservation", dbcon);
                dbcmd.CommandType = CommandType.StoredProcedure;
                dbcmd.Parameters.AddWithValue("familyName", "foo");
                dbcmd.ExecuteNonQuery();
    enter code here
    

4 个答案:

答案 0 :(得分:1)

您不能在给定的MySqlCommand上执行多个语句。

你最好的选择(可维护性,性能,可读性)是:

  • 为您的2条SQL语句创建一个MySQL存储过程。
  • 使用ExecuteNonQuery()
  • 调用您的存储过程
DELIMITER //
CREATE PROCEDURE CreateGuestAndReservation
(
   IN familyName VARCHAR(255)
)
BEGIN
    insert into guest (guestno, familyname) 
     values (null, familyName); 

    insert into reservation (reservationno, guestno) 
    values (null, LAST_INSERT_ID());

END//
DELIMITER ;

从你的WinForms代码中调用它:

dbcon.Open();
cmd = new MySqlCommand("CreateGuestAndReservation", dbcon);
cmd.CommandType = CommandType.StoredProcedure;
//cmd.Parameters.AddWithValue("?familyName", "foo");
cmd.Parameters.Add("?familyName", MySqlDbType.VarChar,255).Value = "foo";
cmd.ExecuteNonQuery();

答案 1 :(得分:0)

下面的代码应该有效,但我怀疑你可能已经尝试过,因为你正在寻求帮助?

string sql = "INSERT INTO guest (guestno,familyname) VALUES(NULL,'Damn'); INSERT INTO reservation (reservationno, guestno) VALUES(NUll,LAST_INSERT_ID())";

如果您需要参数,请尝试以下方法:

string sql = "INSERT INTO guest (guestno,familyname) VALUES(NULL,?familyName); INSERT INTO reservation (reservationno, guestno) VALUES(NUll,LAST_INSERT_ID())";
...
dbcmd.Parameters.Add("@familyName", MySqlDbType.VarChar, 80).Value = _familyName;

编辑:您可能需要运行2个插入命令。请参阅here

答案 2 :(得分:0)

我建议有一种方法来获取ID,而不是依赖于自动id生成,如mysql和sql server的自动增量,这是非常有限的。如果你使用HILO id生成器,你首先获得id,然后在单个事务中执行几个插入没问题,因为你事先知道你的父id。

它不会解决您当前的问题,但它将来会对您的应用程序有所帮​​助,特别是如果经常发生像数据这样的父子项目。

答案 3 :(得分:0)

试试这个,它会起作用:

    private void button56_Click(object sender, EventArgs e) {
        con.Open();
        SqlCommand cmd = new SqlCommand("insert into stholidays values('" + dateTimePicker12.Text + "','" + dateTimePicker20.Text + "','" + dateTimePicker13.Text + "','" + mbk + "','" + dateTimePicker14.Text + "','" + dateTimePicker15.Text + "','" + lt + "','" + dateTimePicker16.Text + "','" + dateTimePicker17.Text + "','" + ebk + "','" + dateTimePicker18.Text + "','" + dateTimePicker19.Text + "','" + textBox105.Text + "','" + textBox106.Text + "','" + textBox107.Text + "','" + dd + "','" + textBox104.Text + "')", con);
        SqlCommand cmd1 = new SqlCommand("insert into holidays values('" + dd + "','" + ms + "','" + day + "','" + textBox104.Text + "')", con);
        cmd.ExecuteNonQuery();
        cmd1.ExecuteNonQuery();
        con.Close();    
    }