2013-02-12 8 views
8

Ho un database (io uso MS ACCESS) Ho questo codice inserto, posso leggere i dati, ma ha ricevuto l'errore durante la scrittura, seguo istruzioni, ma non ha funzionato qui è il mio codiceOleDB errore di comando INSERT

OleDbConnection con = new OleDbConnection(@" provider=Microsoft.ace.Oledb.12.0; data source=\\sisc-erelim\4_Printing\VTDB\DB\VirginiTEADB2.accdb; Persist Security Info=False"); 

private void button1_Click(object sender, EventArgs e) 
     { 
      try 
      { 
       OleDbCommand cmd = new OleDbCommand(); 
       cmd.CommandType = CommandType.Text; 
       cmd.CommandText = "INSERT INTO Accountstbl (Username, Password)" + "VALUES ('" + textBox1.Text + "','" + textBox2.Text + "')"; 
       cmd.Parameters.AddWithValue("@Username", textBox1.Text); 
       cmd.Parameters.AddWithValue("@Password", textBox2.Text); 
       cmd.Connection = con; 
       con.Open(); 
       cmd.ExecuteNonQuery(); 
      } 
      catch (Exception ex) 
      { 
       textBox1.Text = ex.ToString(); 
      } 

E ho sempre ottenuto questo errore,

System.Data.OleDb.OleDbException: Syntax error in INSERT INTO statement. 
    at System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr) 
    at System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult) 
    at System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult) 
    at System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult) 
    at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method) 
    at System.Data.OleDb.OleDbCommand.ExecuteNonQuery() 
    at VirginiTEAcorp.Form3.button1_Click(Object sender, EventArgs e) in C:\Documents and Settings\12-014s\My Documents\applications\Database\WindowsFormsApplication1\Form3.cs:line 34 
+1

Quando esegui il debug, qual è il valore di 'cmd.CommandText' prima di chiamare' ExecuteNonQuery() '? – Arran

risposta

9

Sono stati definiti i parametri @Username e @Password ma non sono mai stati utilizzati nel comando sql.

Che ne dici?

cmd.CommandText = "INSERT INTO Accountstbl (Username, [Password]) VALUES (@Username, @Password)"; 
cmd.Parameters.AddWithValue("@Username", textBox1.Text); 
cmd.Parameters.AddWithValue("@Password", textBox2.Text); 
cmd.Connection = con; 
con.Open(); 
cmd.ExecuteNonQuery(); 

Inoltre si dovrebbe usare Password tra parentesi quadre sul comando SQL come [Password] perché è un reserved keyword in MS Access.

Se ciò non si verifica può causare un errore simile;

Sintassi non corretta in prossimità della parola chiave "Password"

+0

grazie, funziona :) – Pyromancer

+1

@AlfredSanz Buono a sentirlo. –

1

cercare di cambiare questo modo

cmd.CommandText = "INSERT INTO Accountstbl (Username, Password) VALUES (@Username,@Password)"; 
0

Uno spazio è richiesto nella vostra dichiarazione di inserimento:

ssword)" + "VALUES ('" 

ad esempio nell'istruzione voi hanno concatenato senza consentire per uno spazio.

dovrebbe essere:

ssword)" + " VALUES ('" 

per esempio.

0

Si può avere questo:

cmd.CommandText = "INSERT INTO Accountstbl (Username, [Password]) VALUES (@Username,@Password)"; 
      cmd.Parameters.AddWithValue("@Username", textBox1.Text); 
      cmd.Parameters.AddWithValue("@Password", textBox2.Text); 

spero che questo funzionerà.

Cheer-up.