简体   繁体   中英

SQL UPDATE Error in C# Console Application

Using MS Visual Studio 2010, MS Access 2010(database converted to .mdb) and the program is based on C# Console Application.

I'm new to C# and would like some help on the following:

1.I'm struggling with the following an error:

Syntax error in UPDATE statement.

I've tried everything but can't seem to find the problem. I've even looked at possible error outside the UPDATE statement but still no luck. The online help does not seem to help me with my problem and most of the help is regarding a connection with a database server.

for (int w = 0; w <= 9; w++)
{
      string PN = names[w];
      int lvl = Convert.ToInt32(level[w]), dmg = Convert.ToInt32(damage[w]), plek = w+1;
      connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;" + @"Data Source=C:\Users\Home\Desktop\game\game.mdb";
      //Tried this one
      // queryString = "UPDATE Scores SET PlayerName=\""+PN+"\", Level=\"" +lvl+"\", DamageReceived=\""+dmg+"\" WHERE Pos="+ plek;
      //Ended with this one
      queryString = "UPDATE Scores SET PlayerName= @pPN, Level= @plvl, DamageReceived= @pdmg WHERE Pos= @pPlek";

      using (OleDbConnection connection = new OleDbConnection(connectionString))
      using (OleDbCommand command = new OleDbCommand(queryString, connection))
      {
           try
           {
                 command.Parameters.AddWithValue("@pPN", PN);
                 command.Parameters.Add("@plvl", SqlDbType.Int).Value = lvl;
                 command.Parameters.Add("@pdmg", SqlDbType.Int).Value = dmg;
                 command.Parameters.Add("@pPlek", SqlDbType.Int).Value = plek;

                 connection.Open();
                 /*command.Parameters.AddWithValue("@PN", PN);
                 command.Parameters.AddWithValue("@lvl", lvl);
                 command.Parameters.AddWithValue("@dmg", dmg);
                 command.Parameters.AddWithValue("@plek", plek);*/

                 command.ExecuteNonQuery();                                   
           }
           catch (Exception ex)
           {
                 Console.WriteLine(ex.Message);
           }
        }
}

input = Console.ReadLine();

我得到的错误

I would appreciate any help. thanks in advance.

The problem is that OleDbCommand doesn't support named parameters. You have to use the question mark ? instead. Try something like:

UPDATE Scores SET PlayerName= ?, Level= ?, DamageReceived= ? WHERE Pos= ?

Here you will find the full example.

LEVEL is a reserved word in Access SQL. Try using this instead:

queryString = "UPDATE Scores SET PlayerName= @pPN, [Level]= @plvl, DamageReceived= @pdmg WHERE Pos= @pPlek";

Also, as noted in another answer, OleDb ignores parameter names and only pays attention to the order in which they appear in the CommandText, so people usually just use the question mark ( ? ) as the parameter placeholder/name, eg

queryString = "UPDATE Scores SET PlayerName=?, [Level]=?, DamageReceived=? WHERE Pos=?";
// ...
command.Parameters.AddWithValue("?", PN);
command.Parameters.Add("?", SqlDbType.Int).Value = lvl;
command.Parameters.Add("?", SqlDbType.Int).Value = dmg;
command.Parameters.Add("?", SqlDbType.Int).Value = plek;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM