c#同时执行2个命令

时间:2018-03-04 06:29:44

标签: c#

我正在尝试将数据同时放入我的数据库,但是在不同的表中,我试图将查询压缩成一行,但它似乎不起作用。

private void btnBorrows_Click(object sender, EventArgs e)
{
    string connectionString = @"Provider=Microsoft Office 12.0 Access Database Engine OLE DB Provider;" + @"Data source= C:\Users\Administrator\Desktop\dtbase\Database1.accdb";

    string queryString = "SELECT Availability FROM Books WHERE ID = " + txtbxBookId.Text + "";
    string input1 = "insert into AuditTrail (MemberID, MemberName, BookID, BookTitle, DateBorrowed, ReturnDate, Status) values ('" + txtbxMId.Text + "', '" + txtbxMN.Text + "', '" + txtbxBookId.Text + "', '" + txtbxBookTitle.Text + "', '" + txtbxDateNow.Text + "', '" + txtbxReturn.Text + "', '" + txtbxStatus.Text + "')";
    string input2 = "update Books set Availability = '" + txtbxStatus.Text + "' where ID = " + txtbxBookId.Text + " ";

    try
    {
        OleDbConnection connection = new OleDbConnection(connectionString)
        OleDbCommand command = new OleDbCommand(queryString, connection);
        connection.Open();
        command.Connection = connection;
        command.CommandText = queryString;
        command.CommandText = input1;

        command.Connection = connection;
        command.CommandText = queryString;
        command.CommandText = input2;

        command.ExecuteNonQuery();
        MessageBox.Show("Borrowed!");
        return;
    }
    catch(Exception ex)
    {

    }

}

我不知道怎么做的同时,我不知道逻辑。但程序只是跳转到“input2”并忽略“input1”。如果我删除其中一个,该程序将运作良好。

1 个答案:

答案 0 :(得分:0)

在执行命令

之前,您正在覆盖command.CommandText
command.Connection = connection;
command.CommandText = input1;
command.ExecuteNonQuery();
command.CommandText = input2;
command.ExecuteNonQuery();