2013-06-05 49 views
0

運行此代碼時出現錯誤。該錯誤存在於我正在使用「insert into」語句的位置。我不明白是什麼問題。插入語句時出現語法錯誤(帶MS Access數據庫的MS.net代碼)

錯誤:語法錯誤INSERT INTO語句中

請告訴我哪裏是在給定的代碼問題

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

namespace GTC 
{ 
    public partial class Adminhome : Form 
    { 
     public Adminhome() 
     { 
      InitializeComponent(); 
     } 

     OleDbConnection cn = new OleDbConnection("provider=microsoft.ace.oledb.12.0;data source=|DataDirectory|/GTC.accdb;"); 




     void clear() 
     { 
      textBox1.Text = ""; 
      comboBox1.Text = "-Select-"; 
      textBox2.Text = ""; 
      textBox3.Text = ""; 
      textBox4.Text = ""; 
      textBox5.Text = ""; 
     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 
      cn.Open(); 
      // TODO: This line of code loads data into the 'GTCDataSet2.Society' table. You can move, or remove it, as needed. 
      this.SocietyTableAdapter.Fill(this.GTCDataSet2.Society); 
      // TODO: This line of code loads data into the 'GTCDataSet2.User' table. You can move, or remove it, as needed. 
      this.UserTableAdapter.Fill(this.GTCDataSet2.User); 
      this.reportViewer1.RefreshReport(); 
      this.reportViewer2.RefreshReport(); 
     } 

     private void button2_Click(object sender, EventArgs e) 
     { 
      try 
      { 
       String q = String.Format("insert into Society values('{0}')", textBox5.Text); 
       OleDbCommand cmd = new OleDbCommand(q, cn); 
       cmd.ExecuteNonQuery(); 
       label8.Text = "Society Added..."; 
       clear(); 
      } 
      catch (Exception err) 
      { 
       label8.Text = err.Message; 
      } 
     } 

     private void button1_Click_1(object sender, EventArgs e) 
     { 
      try 
      { 
       String q = String.Format("insert into User values('{0}','{1}','{2}','{3}','{4}')", textBox1.Text, comboBox1.Text, textBox2.Text, textBox3.Text, textBox4.Text); 
       OleDbCommand cmd = new OleDbCommand(q, cn); 
       cmd.ExecuteNonQuery(); 
       label6.Text = "User Added..."; 
       clear(); 
      } 
      catch (Exception err) 
      { 
       label6.Text = err.Message; 
      } 
     } 
    } 
} 
+0

你有兩個在代碼中插入INSERT語句,單擊按鈕1或按鈕2時是否發生錯誤?我建議在格式化語句之後顯示字符串Q的內容,以查看顯示內容。我懷疑這是一個引用問題,但可以更好地說明我們是否知道該聲明實際上是什麼樣子。 – Sparky

回答

0

我沒有那麼肯定,但我知道在VB .. SQL插入語法是

INSERT INTO tablename (field1, field2, ...) VALUES (val1, val2, ...) 

我使用的不是你的風格,但也許你已經錯過那些字段列表..

+0

如果您爲表中的每個字段提供值,則字段列表是可選的... – Sparky

相關問題