【问题标题】:Inserting into database from c# [duplicate]从c#插入数据库[重复]
【发布时间】:2018-01-17 16:47:57
【问题描述】:

我用这个代码这么久没有问题。但是,现在当我尝试插入数据库时​​,我收到了这个错误

错误 [07002] [MICROSOFT] ODBC MICROSOFT ACCESS DRIVER] 预计参数太少 2。

谁能告诉我我做错了什么?

if (txtFullName.Text == "" || txtPostcode.Text == "")
{
    MessageBox.Show("Please enter appropriate details in order to proceed", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else if (txtFullName.Text != "" && txtFullName.Text != "")
{
    OdbcCommand cmd = new OdbcCommand("INSERT INTO [Entry] ([FullName], Postcode) Values(@A, @B)", ConnectDb);
    cmd.Parameters.AddWithValue("@A", "HI");
    cmd.Parameters.AddWithValue("@B", "BY");

    ConnectDb.Open();

    try
    {
        int res = cmd.ExecuteNonQuery();

        if (res > 0)
        {
           DialogResult dialogResult = MessageBox.Show("New Allocator saved! would you like to exit? ", "Saved", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

           if (dialogResult == DialogResult.Yes)
           {
               //BackToAdminMainMenu();
           }

           if (dialogResult == DialogResult.No)
           {
               //Clear();
           }
       }
   }
   catch (Exception err)
   {
       MessageBox.Show("A database error has occurred: " + Environment.NewLine + err.Message);
   }
   finally
   {
       ConnectDb.Close();
   }
}

【问题讨论】:

    标签: c# winforms ms-access odbc sql-insert


    【解决方案1】:

    ODBC 不支持命名参数。尝试使用:

    "INSERT INTO [Entry] ([FullName], Postcode) Values(?, ?)"
    

    改为

    【讨论】:

    • 现在可以了,谢谢
    猜你喜欢
    • 2021-10-27
    • 1970-01-01
    • 2020-04-17
    • 2018-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-04
    • 1970-01-01
    相关资源
    最近更新 更多