【发布时间】:2018-03-26 11:08:28
【问题描述】:
我的代码有问题,该代码将多个表填充到我的dataset 中。它将我的 数据库 表中包含的所有内容加载到dataset 中的一个表中。我的代码如下所示。如何将这些表从数据库加载到具有相同数量的表和内容的 数据集 中。
Private Sub Filldataset()
Private cnn As OleDbConnection
Private dt As New DataTable
Private da As New OleDbDataAdapter
Private cmd As New OleDbCommand
Private ds As New DataSet
Dim tblrestrictions As String() = New String() {Nothing, Nothing, Nothing, "TABLE"}
Dim userTables As DataTable = Nothing
userTables = cnn.GetSchema("Tables", tblrestrictions)
Dim i As Integer
For i = 1 To userTables.Rows.Count - 1 Step 1
cnn = New OleDbConnection(Str)
cnn.Open()
cmd = cnn.CreateCommand
cmd.CommandText = "select * from" & " " & userTables.Rows(i)(2).ToString
dt.Clear()
da.SelectCommand = cmd
da.Fill(dt)
da.Fill(ds)
Next
cnn.Close()
MessageBox.Show(ds.Tables.Count)
End Sub
【问题讨论】:
-
在调用GetSchema()之前需要打开一个连接。您正在清除和重新填充相同的数据表 (dt)。你不需要 da.Fill(dt)
-
我已经在主类中打开了连接。它遍历数据库并选择每个数据表来填充数据集。但它失败了。你能给出解决方案吗?非常感谢
标签: database vb.net dataset oledbdataadapter