我有一个数据库编辑程序,需要让用户能够编辑现有查询的定义。更改似乎适当地应用于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
我忽略了什么吗?为什么这些变化没有反映在数据库中?
答案 0 :(得分:2)
DataTable是一个断开连接的对象,与数据库连接无关。无论你做了什么改变 - 只留在记忆中。您需要编写其他逻辑来将数据写回数据库。