计算 vb.Net 中 MS Access 数据库中所有表中的所有记录
count all records in all tables in MS access database in vb.Net
我在 MS Access 中有一个数据库,其中有 4 个表。我想使用 vb.Net 计算所有表的所有记录。有没有oledbcommand命令统计数据库所有表的所有记录?
con1.Open()
Dim Restrictions() As String = {Nothing, Nothing, "Table1", Nothing}
Dim CollectionName As String = "Columns"
Dim dt As DataTable = con1.GetSchema(CollectionName, Restrictions)
For Each TableRow As DataRow In dt.Rows
Console.WriteLine(TableRow.Item("COLUMN_NAME").ToString)
Next
con1.Close()
我创建了 2 个函数。第一个通过在连接上调用 GetSchema
获取 table 名称。我通过检查名称是否以 MS 开头排除了系统 tables。我还通过仅添加 TABLE_TYPE = TABLE.
排除了视图
第二个函数获取 table 个名称的列表并获取每个 table 中的计数。请注意 table 名称周围的括号,以防名称包含 space.
Private cs As String = My.Settings.AccessAddressConnection
Private Function GetTableNames() As List(Of String)
Dim TableNames As New List(Of String)
Dim dt As New DataTable
Using cn As New OleDbConnection(cs)
cn.Open()
dt = cn.GetSchema("Tables")
End Using
For Each row As DataRow In dt.Rows
If Not row("TABLE_NAME").ToString.StartsWith("MS") AndAlso row("TABLE_TYPE").ToString = "TABLE" Then
TableNames.Add(row("TABLE_NAME").ToString)
End If
Next
Return TableNames
End Function
Private Function GetTotalRecords(lst As List(Of String)) As Integer
Dim counts As Integer
Using cn As New OleDbConnection(cs),
cmd As New OleDbCommand()
cmd.Connection = cn
cn.Open()
For Each TName In lst
cmd.CommandText = $"Select Count(*) From [{TName}];"
counts += (CInt(cmd.ExecuteScalar))
Next
End Using
Return counts
End Function
Private Sub Button1_Click() Handles Button1.Click
Dim lst = GetTableNames()
Dim Total = GetTotalRecords(lst)
MessageBox.Show(Total.ToString)
End Sub
我在 MS Access 中有一个数据库,其中有 4 个表。我想使用 vb.Net 计算所有表的所有记录。有没有oledbcommand命令统计数据库所有表的所有记录?
con1.Open()
Dim Restrictions() As String = {Nothing, Nothing, "Table1", Nothing}
Dim CollectionName As String = "Columns"
Dim dt As DataTable = con1.GetSchema(CollectionName, Restrictions)
For Each TableRow As DataRow In dt.Rows
Console.WriteLine(TableRow.Item("COLUMN_NAME").ToString)
Next
con1.Close()
我创建了 2 个函数。第一个通过在连接上调用 GetSchema
获取 table 名称。我通过检查名称是否以 MS 开头排除了系统 tables。我还通过仅添加 TABLE_TYPE = TABLE.
第二个函数获取 table 个名称的列表并获取每个 table 中的计数。请注意 table 名称周围的括号,以防名称包含 space.
Private cs As String = My.Settings.AccessAddressConnection
Private Function GetTableNames() As List(Of String)
Dim TableNames As New List(Of String)
Dim dt As New DataTable
Using cn As New OleDbConnection(cs)
cn.Open()
dt = cn.GetSchema("Tables")
End Using
For Each row As DataRow In dt.Rows
If Not row("TABLE_NAME").ToString.StartsWith("MS") AndAlso row("TABLE_TYPE").ToString = "TABLE" Then
TableNames.Add(row("TABLE_NAME").ToString)
End If
Next
Return TableNames
End Function
Private Function GetTotalRecords(lst As List(Of String)) As Integer
Dim counts As Integer
Using cn As New OleDbConnection(cs),
cmd As New OleDbCommand()
cmd.Connection = cn
cn.Open()
For Each TName In lst
cmd.CommandText = $"Select Count(*) From [{TName}];"
counts += (CInt(cmd.ExecuteScalar))
Next
End Using
Return counts
End Function
Private Sub Button1_Click() Handles Button1.Click
Dim lst = GetTableNames()
Dim Total = GetTotalRecords(lst)
MessageBox.Show(Total.ToString)
End Sub