This question already has answers here:
ASP.NET ODBC Query with parameters
                                
                                    (4个答案)
                                
                        
                                去年关闭。
            
                    
我使用该代码已经很长时间了,没有问题。但是,现在当我尝试插入数据库时​​,出现此错误


  错误[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();
   }
}

最佳答案

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

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


代替

关于c# - 从C#插入数据库,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48306120/

10-13 05:28