Fiz um código para simplesmente ler um arquivo .mdb, listar suas tabelas e mostrar o conteudo da tabela selecionada em um grid.
Eu consegui fazer o código e até funcionou mas pelo menos duas tabelas geram um erro ao serem selecionadas.
O código está abaixo, a base tem senha mas eu retirei do código.
Obs: Não usei as ferramentas DATA do toolbox. Fiz tudo via código. (porém o .NEt criou os objetos sozinho após o código pronto)
//Form1
Imports System.Data.OleDb
Public Class Form1
Inherits System.Windows.Forms.Form
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim vettab() As String
MSincAccess.TableNames(vettab)
Dim Tab As String
For Each Tab In vettab
LstB.Items.Add(Tab)
Next
End Sub
Private Sub LstB_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LstB.SelectedIndexChanged
Dim Table As String
Table = LstB.Items.Item(LstB.SelectedIndex)
MSincAccess.Read(DgTable, Table)
End Sub
Private Sub Form1_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed
MSincAccess.CloseDB()
End Sub
End Class
//Classe
Imports System.Data.OleDb
Module MSincAccess
Dim OleDbConnection1 As New OleDbConnection
Dim OleDbDataAdapter1 As New OleDbDataAdapter
Public Function TableNames(ByRef vetTables() As String)
Dim schemaTable As DataTable
Dim I As Integer
OpenDb()
schemaTable = OleDbConnection1.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,New Object() {Nothing, Nothing, Nothing, "TABLE"})
For I = 0 To schemaTable.Rows.Count - 1
ReDim Preserve vetTables(I)
vetTables(I) = schemaTable.Rows(I)!TABLE_NAME.ToString
Next I
CloseDB()
End Function
Private Sub OpenDb()
OleDbConnection1.ConnectionString = "Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Registry Path=;Jet OLEDB:Database Locking Mode=0;Jet OLEDB:Database Password=*********;Data Source=C: este.mdb;Password=;Jet OLEDB:Engine Type=4;Jet OLEDB:Global Bulk Transactions=1;Provider=Microsoft.Jet.OLEDB.4.0;Jet OLEDB:System database=;Jet OLEDB:SFP=False;Extended Properties=;Mode=Share Deny None;Jet OLEDB:New Database Password=;Jet OLEDB:Create System Database=False;Jet OLEDB:Don't Copy Locale on Compact=False;Jet OLEDB:Compact Without Replica Repair=False;User ID=Admin;Jet OLEDB:Encrypt Database=False"
OleDbConnection1.Open()
End Sub
Public Sub CloseDB()
OleDbConnection1.Close()
End Sub
Public Function Read(ByRef ObjDataGrid As DataGrid, ByVal TableName As String)
Dim cmd As OleDbCommand = OleDbConnection1.CreateCommand
OpenDb()
cmd.CommandText = "Select * from " & TableName
OleDbDataAdapter1.SelectCommand = cmd
Dim dt As New System.Data.DataTable
OleDbDataAdapter1.Fill(dt)
ObjDataGrid.DataSource = dt
CloseDB()
End Function
End Module
OBS: Reparei que as duas bases que não funcionaram são nativas do Access (não são vinculadas como outras que existem neste Mdb), também reparei que outras tabelas nativas não dão o problema.
Se alguém tiver paciência pra ler isso tudo e ajudar... Eu agradeço muito! :))))
Valeu gente!