0

我有一个数据库编辑程序,需要让用户能够编辑现有查询的定义。更改似乎已适当地应用于 DataTable,但实际上并未保存。

' schemaTable will be set to one of the following values before calling this method
schemaTable = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Views, Nothing)
or
schemaTable = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Procedures, Nothing)

Public Sub UpdateQueryDefinition(ByRef schemaTable As DataTable, _
                                 ByRef SQL As String, _
                                 ByVal QName As String, _
                                 ByVal View As Boolean)

Dim existingqueryname As String
Dim existingquerydefinition As String
Dim qrydefinitionindex As Integer

For each row As DataRow In schemaTable.Rows

     existingqueryname = row.ItemArray(2).ToString.ToUpper
     If View Then
        ' Column index for query definition is 3 for Views
        qrydefinitionindex = 3
     Else
        ' Not a view, then we're a procedure.  The Definition is column index 4
        qrydefinitionindex = 4
     End If
     existingquerydefinition = row.ItemArray(qrydefinitionindex).ToString.ToUpper

     ' See if the current query in schemaTable is the query we want to modify
     If existingqueryname = QName.ToUpper Then

        SQL = ' User input retrieved and set here

        ' Set query definition in schemaTable = to user input of new query
        row.Item(qrydefinitionindex) = SQL
        row.AcceptChanges()

' At this point, if you reference row.Item(qrydefinitionindex) it will indeed = SQL
' but the query is not actually modified in the .mdb

        Exit For
     End If

  Next

End Sub

我忽略了什么吗?为什么更改没有反映在数据库中?

4

1 回答 1

2

DataTable 是一个断开连接的对象,独立于数据库连接。无论你对它做什么改变——只留在记忆中。您需要编写额外的逻辑来将数据写回数据库。

于 2013-06-06T19:37:01.293 回答