【问题标题】:Data Adapter Update command causing exception数据适配器更新命令导致异常
【发布时间】:2016-10-16 07:12:17
【问题描述】:

请注意 ----- 这不再适用于我。我通过将以下两行添加到我的 form.load 来修复它。

    cb.QuotePrefix = "["
    cb.QuoteSuffix = "]"

它已经为我解决了这个问题,我希望它也能为你解决问题。

好的,我在 VB.net 中有一个应用程序,用作我工作公司的培训师的调度程序。在编辑模式下(因此可以对存储信息的数据库进行更新)有许多按钮,添加和删除培训师,或者在表格中添加星期。

导致我出现问题的按钮是保存/更新按钮。它包含三组命令,一组用于更新添加的列,一组用于删除的列,第三组简单地更新其他修改的数据。我知道它可能会更有效率,但哦,好吧,我稍后再谈。

问题是,最后一段代码包含“da.update(ds)”,它是更新数据源的数据适配器。虽然这个命令在我们连接到我们的 SQL 服务器的其他应用程序中运行良好,但它在这里引起了问题。

第一个单元格的值为空的任何列都会导致异常提示

“'列标题的前两个字符'附近的语法不正确。”

现在,我认为这个问题源于 - 由于异常 - 我为列使用了一组不正确的名称,这些名称是每周星期一的日期(例如 01/02/2016)所以它'd 在 '01' 附近显示不正确的语法。在这种情况下。

但是,更改命名约定并没有像异常所暗示的那样解决此问题,并且它仅发生在 FIRST 值为 null 的列上 - 这意味着第一位培训师本周没有任何计划。

任何人有任何想法来解决这个问题,基本上不涉及在这个相当大的桌子上填充每个空值?我知道这会奏效,但这会非常耗时,如果没有其他解决方案出现,我愿意这样做。

我在互联网上四处查看,但没有找到与我们的确切问题相关的解决方案,因此非常感谢您的帮助。

如果它很重要 - 这是导致问题的功能。

Dim da As OleDbDataAdapter 'The datasets and adapters variables.
Dim da2 As OleDbDataAdapter
Public ds As DataSet = New DataSet
Public ds2 As DataSet = New DataSet


    'Connection String. Connects to the server and finds the database and table listed.
    cs = "Provider=SQLOLEDB;"
    cs &= "Server=SOFWAREDEVSQLSE\SQLEXPRESS;"
    cs &= "Database=MTS2;"
    cs &= "User Id=;" 'You don't need to see that to be fair.
    cs &= "Password=;" 'You don't need to see that to be fair.
    sql = "Select * FROM MTS2;"

    'Runs the string. Flares up a timed out error if connection could not be established.
    Try
        da = New OleDbDataAdapter(sql, cs)
        da.Fill(ds)
        da2 = New OleDbDataAdapter(sql, cs)
        da2.Fill(ds2)
    Catch ex As Exception
        MsgBox("Connection failed. Please ensure you have a suitable connection to the Training network. Otherwise, refer to helpdesk support for further assistance.")
        Me.Close()
    End Try
    dgvSchedule.DataSource = ds.Tables(0)


Private Function save()

    'Try
    ''This section reads the SQL server for column names, and adds any that are listed in the DGV, but not the database. I know its a little messy but itll do.
    Dim columnnum As Integer = -1
    Dim columname As String
    For Each column In ds.Tables(0).Columns
        columnnum = columnnum + 1
        columname = dgvSchedule.Columns(columnnum).HeaderText
        If Not ds2.Tables(0).Columns.Contains(columname) Then
            Dim SqlAddCol As String = "ALTER TABLE MTS2 ADD [" & columname.Trim() & "] nvarchar(255)"
            Using con As New OleDbConnection(cs)
                Using cmd As New OleDbCommand(SqlAddCol, con)
                    con.Open()
                    cmd.ExecuteNonQuery()
                End Using
            End Using

        End If
    Next

    columnnum = -1
    For Each column In ds2.Tables(0).Columns
        columnnum = columnnum + 1
        columname = ds2.Tables(0).Columns(columnnum).ColumnName
        If Not ds.Tables(0).Columns.Contains(columname) Then
            Dim SqlDelCol As String = "ALTER TABLE MTS2 DROP COLUMN [" & columname.Trim() & "]"
            Using con As New OleDbConnection(cs)
                Using cmd As New OleDbCommand(SqlDelCol, con)
                    con.Open()
                    cmd.ExecuteNonQuery()
                End Using
            End Using
        End If
    Next

    ds2.Tables.Clear()
    da2 = New OleDbDataAdapter(sql, cs)
    da2.Fill(ds2)

    da.Update(ds) ''''' The exception is thrown here. " Incorrect syntax near '01'."
    DataTableColours()
    MessageBox.Show("Data saved successfully. New weeks and trainers added and deleted. Changed values updated.")
    'Catch
    '   MessageBox.Show("Data failed to update properly. Please ensure you are connected to the Baltic network and try again. If the problem persists, seek IT support.")
    'End Try

End Function

该函数将数据网格视图 (DGVSchedule) 中的值保存到服务器,方法是获取 DS 中的当前列及其原始列(在 DS2 中)然后运行 ​​Sql 查询以添加或删除任何列不匹配.然后更新 DS2 以使用与 DS 相同的值。最后,调用 DA.update(DS),它将所有其他修改的值更新到 SQL 服务器中——理论上。相反,它导致了我们的特殊异常。

任何帮助将不胜感激,谢谢。

【问题讨论】:

  • da 和 ds 在此代码中声明的位置 da.Update(ds) ,请显示完整代码以获得更好的帮助
  • @NazirUllah 为您整理好了。如果你想看看,它现在就在那里
  • 在更新方法调用之前通过调试器检查 ds 中存在哪些数据
  • @NazirUllah 我该怎么做?我会在 DA.update(Ds) 命令之前暂停一下吗?老实说,我仍然是这种事情的实习生。我不知道该怎么做>.

标签: sql-server vb.net oledb dataadapter


【解决方案1】:

我已经解决了我遇到的问题。我通过将以下两行添加到我的 form.load 来修复它。

    cb.QuotePrefix = "["
    cb.QuoteSuffix = "]"

基本上,我现在感觉自己像个笨蛋,但这解决了问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-27
    • 1970-01-01
    • 2015-04-03
    • 1970-01-01
    • 1970-01-01
    • 2015-11-26
    相关资源
    最近更新 更多