2013-03-27 101 views
2

我構建了一個c#應用程序,您可以在其中登錄並註冊一個新帳戶。但是,當我點擊我的按鈕新帳戶,並填寫所需的字段,我會得到以下錯誤:OleDBException錯誤INSERT

vcom.ExecuteNonQuery(); - > OleDBException未處理。 INSERT指令包含語法錯誤。

看到這裏我們下面的代碼:


using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.Data.OleDb; 

namespace Eindopdracht 
{ 
    public partial class maak_account : Form 
    { 

     OleDbConnection vcon = new OleDbConnection(@"provider= microsoft.jet.oledb.4.0;data source=sample.mdb"); 

     public maak_account() 
     { 
      InitializeComponent(); 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      OleDbConnection vcon = new OleDbConnection(@"provider= microsoft.jet.oledb.4.0;data source=sample.mdb"); 
      vcon.Open(); 

      string test = string.Format("insert into inlog (PASSWORD, Username, leeftijd, gewicht) VALUES ('" + textBox2.Text + "','" + textBox1.Text + "','" + textBox3.Text + "','" + textBox4.Text + "')"); 
      OleDbCommand vcom = new OleDbCommand(test, vcon); 
      vcom.ExecuteNonQuery(); 
      MessageBox.Show("Uw gegevens zijn opgeslagen"); 
      vcom.Dispose(); 
     } 
    } 
} 
+1

Golden tip nr 1:使用參數代替字符串concat來創建查詢。 – 2013-03-27 10:57:22

回答

2

的原因語法錯誤是因爲Password,這恰好是列名,是一個保留關鍵字。

insert into inlog ([PASSWORD], Username, leeftijd, gewicht) 

從MS Access文件,

If a reserved word is already in use, you can avoid error messages by surrounding each occurrence of the word with brackets ([ ]). However, the best solution is to change the name to a nonreserved word.

爲了進一步提高代碼,

  • 使用using聲明妥善處置反對
  • 使用try-catch妥善處理異常
  • 參數的值,以避免SQL注入

例如,

string connStr = @"provider= microsoft.jet.oledb.4.0;data source=sample.mdb"; 
string test = "insert into inlog ([PASSWORD], Username, leeftijd, gewicht) VALUES (?, ?, ?, ?)"; 

using(OleDbConnection vcon = new OleDbConnection(connStr)) 
{ 
    using(OleDbCommand vcom = new OleDbCommand(test, vcon)) 
    { 
     vcom.Parameters.AddWithValue("PASSWORD", textBox2.Text); 
     vcom.Parameters.AddWithValue("Username", textBox1.Text); 
     vcom.Parameters.AddWithValue("leeftijd", textBox3.Text); 
     vcom.Parameters.AddWithValue("gewicht", textBox4.Text); 
     try 
     { 
      vcon.Open(); 
      com.ExecuteNonQuery(); 
     } 
     catch(OleDbException ex) 
     { 
      // do something with the exception 
     } 
    } 
} 
0

使用try/catch塊抓住這個例外並看到錯誤ormessage。還要在finally塊中處理你的資源。即使拋出異常,finally塊也會一直執行。

try 
{ 
    OleDbConnection vcon = new OleDbConnection(@"provider= microsoft.jet.oledb.4.0;data source=sample.mdb"); 
    vcon.Open(); 

    string test = string.Format("insert into inlog (PASSWORD, Username, leeftijd, gewicht) VALUES ('" + textBox2.Text + "','" + textBox1.Text + "','" + textBox3.Text + "','" + textBox4.Text + "')"); 
    OleDbCommand vcom = new OleDbCommand(test, vcon); 
    vcom.ExecuteNonQuery(); 
    MessageBox.Show("Uw gegevens zijn opgeslagen"); 
} 
catch(Exception ex) 
{ 
    MessageBox.Show(ex.Message); 
} 
finally 
{ 
    vcom.Dispose(); 
    vcon.Close(); 
    vcon.Dispose(); 
}