在VB.NET中设置DataSource

时间:2013-03-06 20:42:26

标签: .net database vb.net datasource

Dim inc As Integer
Dim MaxRows As Integer
Dim con As New OleDb.OleDbConnection
Dim dbProvider As String
Dim dbSource As String
Dim dA As OleDb.OleDbDataAdapter
Dim dS As New DataSet
Dim SQL As String

Private Sub AbrirToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles AbrirToolStripMenuItem.Click
    Dim strFileName As String
    Dim ClikedOk As Integer

    OpenFD.InitialDirectory = "C:\"
    OpenFD.Title = "Ubica la base de datos"
    OpenFD.Filter = "Agenda|Agenda.mdb"
    OpenFD.ShowDialog()

    If ClikedOk = DialogResult.OK Then
        strFileName = OpenFD.FileName
        dbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
        dbSource = "Data Source = Agenda.mdb"

        con.ConnectionString = dbProvider & dbSource
        con.Open()
        SQL = "SELECT * FROM Contactos"
        dA = New OleDb.OleDbDataAdapter(SQL, con)
        dA.Fill(dS, "Agenda")

        con.Close()
        MaxRows = dS.Tables("Agenda").Rows.Count
        inc = -1

    End If
End Sub

我想做的是:
用户应按菜单项并选择数据库文件,然后数据源将定向到用户指向数据库文件的位置。 我应该在“dbSource = Data Source = ...”这一行中加入什么? Access Data Base是.mdb,因为我在使用.accdb

时遇到了麻烦

2 个答案:

答案 0 :(得分:1)

更改此行:

dbSource = "Data Source = Agenda.mdb"

到此:

dbSource = "Data Source=""" & OpenFD.FileName & """"

,这会将Data Source指向用户选择的文件。

答案 1 :(得分:0)

那你在那里遇到了一些麻烦。 Jet drivers are 32 bit and need to be updated if you want to use jet to access databases in 64 bit windows

您还可以将应用配置为在WoW64模式下运行,以使用32位驱动程序。

如果您已经完成了制作喷气式飞机的工作,那么您可以这样做:

dbSource = "Data Source=" & OpenFD.FileName;

如果你不想跳过使Jet工作的箍,then you can use the .Net libraries to connect改变:

dbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
dbSource = "Data Source = Agenda.mdb"

dbProvider = "Driver={Microsoft Access Driver (*.mdb)};"
dbSource = "Dbq=" & "Agenda.mdb;"

祝你好运。