2012-08-01 75 views
0

我的問題發生在da.Update(dt)。我收到「OleDbException was unhandled,INSERT INTO語句中的語法錯誤」錯誤。它工作時,我用一個沒有字段名稱和只有10列的基本表,但現在我有25個項目不起作用。Vb.net問題更新數據庫

Dim dt As New DataTable 
    Dim ds As New DataSet 

    con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source= C:\TPComplete.accdb;Persist Security Info=False;" 

    con.Open() 
    MsgBox("here") 
    ds.Tables.Add(dt) 

    Dim da As New OleDbDataAdapter 

    da = New OleDbDataAdapter("SELECT * FROM DReview", con) 

    da.Fill(dt) 



    Dim newRow As DataRow = dt.NewRow 

    newRow.Item("Caller") = Caller 
    newRow.Item("Associate Name") = Associate 
    newRow.Item("Store Number") = "1" 
    newRow.Item("Number of Rings") = Ring 
    newRow.Item("Time on Hold") = HoldTime 
    newRow.Item("Greeting: 3 or fewer rings") = GreetingRings 
    newRow.Item("Greeting: Asked for your name") = GreetingAskName 
    newRow.Item("Greeting: Offered his/her name") = GreetingOfferedName 
    newRow.Item("Greeting: Mentioned TIRE PROS in the greeting") = GreetingTirePros 
    newRow.Item("Greeting: Associated acted like they are glad") = GreetingGlad 
    newRow.Item("Hold for longer than 1 minute") = holdUpdate 
    newRow.Item("Ask for the type of car AND look up the size") = LookupSize 
    newRow.Item("Ask appropriate questions about the type of driving") = DailyDriving 
    newRow.Item("1st Price Mentioned") = SingleTirePrice 
    newRow.Item("1st OTD Price Mentioned") = SingleTireOutDoorPrice 
    newRow.Item("Tire Brand") = TireBrand 
    newRow.Item("Tire Model") = TireModel 
    newRow.Item("Offered several tire choices and prices") = SeveralChoices 
    newRow.Item("Did they offer financing options") = Financing 
    newRow.Item("Mentioned benefits of the location") = Benefits 
    newRow.Item("Appointment") = Appointment 
    newRow.Item("How long does it take to put them on") = InstallTime 
    newRow.Item("Associate Score") = AssociateScore 
    newRow.Item("Time Completed") = hms 
    newRow.Item("Completed Date") = ymd 



    dt.Rows.Add(newRow) 
    Dim cb As New OleDbCommandBuilder(da) 
    cb.GetInsertCommand() 
    da.Update(dt) 
    MsgBox("Saved") 
    con.Close() 
+0

可能重複的[vb.net ...我做錯了與訪問工作](http://stackoverflow.com/questions/11760663/vb-net-what-am-i-doing-wrong-working與訪問) – LarsTech 2012-08-01 16:19:55

+0

很難說如果沒有看到你的表定義。它可以是任何數量的東西,例如'ymd'不是一個日期,'Store Number'不是一個字符串,你試圖分配給一個標識列...我只是猜測,直到我們可以看到表。 – Widor 2012-08-01 16:22:25

+0

@LarsTech:這不是該問題的重複,而是後續。 – RBarryYoung 2012-08-01 16:22:28

回答

0

錯誤「INSERT INTO語句中的語法錯誤」可能意味着問題是,使用OLEDB命令生成器不能正確處理您使用的列名。這些行:

你有「非可變式的」列名,這基本上是任何類型的名字,你不能也作爲變量名使用的
newRow.Item("Store Number") = "1" 
    newRow.Item("Number of Rings") = Ring 
    newRow.Item("Time on Hold") = HoldTime 
    newRow.Item("Greeting: 3 or fewer rings") = GreetingRings 
    newRow.Item("Greeting: Asked for your name") = GreetingAskName 
    newRow.Item("Greeting: Offered his/her name") = GreetingOfferedName 
    newRow.Item("Greeting: Mentioned TIRE PROS in the greeting") = GreetingTirePros 
    newRow.Item("Greeting: Associated acted like they are glad") = GreetingGlad 

顯示。那只是{A-Z,0-9和「_」}。事情就像他們嚴禁可變式的名字不是無效的,但他們有專門引用,就像這樣:

newRow.Item("[Store Number]") = "1" 

不幸的是,一些連接提供商(我認爲的OleDb就是其中之一這種情況下),不能處理,並會取消引用他們,導致生成的SQL命令無效。

最簡單的解決方案是回到列號而不是名稱(可能原因是他們原來的原因)。或者,您可以重命名所有列名稱以刪除所有非變量類型的字符(空格,冒號等)。

+0

好的。那麼我一直在做什麼,並且評論大部分內容,並存儲到一個新的數據庫中,並添加到列以訪問和兩個新變量,並查看失敗發生的位置。現在,現在有錯誤了。 – Jbailey01 2012-08-01 17:44:38