C# 不执行第二个查询?

C# 不执行第二个查询?,c#,sql,database,visual-studio,ms-access,C#,Sql,Database,Visual Studio,Ms Access,单击按钮时,只执行第一个查询。第二个插入afspraken(behandeling)不会执行。有人知道为什么吗 private void button1_Click(object sender, EventArgs e) { string insertStatement = "INSERT INTO Afspraken (Afspraakdatum) VALUES ('" + textBox23.Text + "');"; string insertStatement1 = "I

单击按钮时,只执行第一个查询。第二个
插入afspraken(behandeling)
不会执行。有人知道为什么吗

private void button1_Click(object sender, EventArgs e)
{
    string insertStatement = "INSERT INTO Afspraken (Afspraakdatum) VALUES ('" + textBox23.Text + "');";
    string insertStatement1 = "INSERT INTO Afspraken (Behandeling) VALUES ('" + textBox21.Text + "');";
    OleDbCommand insertCommand = new OleDbCommand(insertStatement, connection);
    OleDbCommand insertCommand1 = new OleDbCommand(insertStatement1, connection);
    connection.Open();
    try
    {
        int count = insertCommand.ExecuteNonQuery();
    }
    catch (OleDbException ex)
    {

    }
    finally
    {
        connection.Close();
        textBox23.Clear();
        textBox21.Clear();
        MessageBox.Show("Uw afspraak is gemaakt!");
    }
}

您正在创建
insertCommand1
,但从未执行它。您只在执行
insertCommand
(在
try
块内的单行中)。

您正在创建
insertCommand1
,但您从未执行它。您只执行了
insertCommand
(在
try
块内的一行中)。

正如我在您对另一个答案的评论中看到的,我相信此解决方案最适合您:

string insertStatement = "INSERT INTO Afspraken (Afspraakdatum, Behandeling) VALUES ('" + textBox23.Text + "', '" + textBox21.Text + "');");
OleDbCommand insertCommand = new OleDbCommand(insertStatement, connection);

但请记住,此代码是不安全的,您应该使用预先准备好的语句。

正如我在您对另一个答案的评论中所看到的,我相信此解决方案最适合您:

string insertStatement = "INSERT INTO Afspraken (Afspraakdatum, Behandeling) VALUES ('" + textBox23.Text + "', '" + textBox21.Text + "');");
OleDbCommand insertCommand = new OleDbCommand(insertStatement, connection);

但是请记住,此代码是不安全的,您应该使用预先准备好的语句。

谢谢。忽略了它。但是现在发生了什么:它们被分别放在acces数据库文件中。命令和命令1需要放在数据库的同一行上。比如:约会治疗谢谢。忽略了它。但是现在发生了什么:它们被分别放在acces数据库文件中。命令和命令1需要放在数据库的同一行上。Like:Date-treatment谢谢您的回答,但您建议的代码会导致以下错误:查询表达式“30-01-2016,很少;”中的语法错误(缺少运算符)。请注意,“30-01-2016”和“很少”“是文本框中的值,我已更改了答案,在第一个值后缺少一个撇号。立即检查谢谢您的回答,但您建议的代码会导致以下错误:查询表达式“30-01-2016,少数;”中的语法错误(缺少运算符)。请注意,“30-01-2016”和“少数”是文本框中的我的值,我已更改了我的答案,我在第一个值后缺少撇号。请立即检查