2012-09-26 67 views
1

heyguys,錯誤添加記錄訪問從C#

IM全新這個......我想從一個winform添加我的記錄,這已經是一個數據源訪問數據庫,但在錯誤的位置結束了它說:

OleDBException未處理,並且它說「在 INSERT INTO語句中的語法錯誤。

表示該行:

da.Update(ds1, "Table1"); 

我的編碼是:

 int MaxRows = 0; 
     int inc = 0; 

     private void Form2_Load(object sender, EventArgs e) 
     { 
      con = new System.Data.OleDb.OleDbConnection(); 
      ds1 = new DataSet(); 

      con.ConnectionString = " Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:/Documents and Settings/user/My Documents/anchu.accdb"; 
      string sql = "SELECT * From Table1"; 
      da = new System.Data.OleDb.OleDbDataAdapter(sql, con); 

      con.Open(); 

      da.Fill(ds1, "Table1"); 
      //NavigateRecords(); 

      con.Close(); 
      //con.Dispose(); 
     } 
     /*private void NavigateRecords() 
     { 
      DataRow drow = ds1.Tables["Table1"].Rows[0]; 

      textBox1.Text = drow.ItemArray.GetValue(0).ToString(); 
      textBox2.Text = drow.ItemArray.GetValue(1).ToString(); 
      textBox3.Text = drow.ItemArray.GetValue(2).ToString(); 
      textBox4.Text = drow.ItemArray.GetValue(3).ToString(); 





     }*/ 


     private void groupBox1_Enter(object sender, EventArgs e) 
     { 

     } 

     private void button2_Click(object sender, EventArgs e) 
     { 
      textBox1.Clear(); 
      textBox2.Clear(); 
      textBox3.Clear(); 
      textBox4.Clear(); 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      System.Data.OleDb.OleDbCommandBuilder cb; 
      cb = new System.Data.OleDb.OleDbCommandBuilder(da); 

      DataRow drow = ds1.Tables["Table1"].NewRow(); 
      drow[0] = textBox1.Text; 
      drow[1] = textBox2.Text; 
      drow[2] = textBox3.Text; 
      drow[3] = textBox4.Text; 

      ds1.Tables["Table1"].Rows.Add(drow); 

      MaxRows = MaxRows + 1; 
      inc = MaxRows - 1; 

      da.Update(ds1, "Table1"); 

      MessageBox.Show("Entry Added"); 


     } 
    } 
} 

回答

2

可否請你看看錶結構。 看來,表字段不匹配。例如:如果您有一個自動列,並且向該列添加值,則會引發異常。 此外,即使表格列匹配,也檢查數據類型和最大長度。

希望這個解決。

歡呼

+0

你可以發佈你正在使用table1的表結構嗎? –

+0

是啊當然...我應該如何發佈它? – Anirudh

+0

我貼了表格結構sandip – Anirudh

0

OleDbCommandBuilder是不是在爲具有比PSQL或TSQL不同的語法要求數據庫系統,如Access建設語句很大。在生成CommandBuilder後,我會添加一個斷點,並查看它生成的SQL以查看它是否是Access的正確語法。

您可能更適合自己構建INSERT語句,而不是假設命令生成器會爲您正確構建INSERT語句。