Vb.net问题更新数据库

时间:2012-08-01 16:17:01

标签: database vb.net

我的问题发生在da.Update(dt)。我收到“OleDbException未处理,INSERT INTO语句中的语法错误”错误。当我使用没有字段名称且只有10列的基本表时,它工作正常,但现在我有25个项目它不起作用。

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()

1 个答案:

答案 0 :(得分: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命令无效。 / p>

最简单的解决方案是返回列号而不是名称(可能是为什么他们原来的那个)。或者,您可以重命名所有列名称以删除所有非变量类型的字符(空格,冒号等)。