-1

大家好,我是 asp.net 的新手,我想从数据库中选择数据并将数据存储在 VB 而非 C# 中的 DataTable 中,但我似乎无法理解如何连接。谁能帮我尝试连接到访问数据库?我很迷茫,已经好几天了。如果不是没关系。

感谢您的阅读。

布伦特

Public Function CheckUser(ByVal p_strUserNAME As String, ByVal p_Password As String) As Boolean
    Dim blnAdminUser As Boolean = False

    Dim SQLQuery As String = "SELECT Username, Password FROM HomelessUsers WHERE Username = " & p_strUserNAME & " AND Password = " & p_Password

    Dim MDBConnectionString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=F:\HomelessCapstone\HomelessCapstone\HomelessCapstone\APP_Data\Homeless.accdb;Persist Security Info=True"
    Dim ds As New DataSet
    Dim cnn As OleDbConnection = New OleDbConnection(MDBConnectionString)

    cnn.Open()

    Dim cmd As New OleDbCommand(SQLQuery, cnn)
    Dim da As New OleDbDataAdapter(cmd)
    da.Fill(ds, "HomelessUsers")
    cnn.Close()


    'Dim DatatableTest As DataTable = ds.Tables("HomelessUsers")
    'Dim Row As DataRow = Nothing
    'Dim Item(2) As String


    'For Each Row In DatatableTest.Rows
    'item()
    ' Next



    Return blnAdminUser
End Function
4

1 回答 1

0

Basically, there are two ways you can go about it. One is to use ADO (access database objects) which has an intiutive wizard that will help you connect your access database to your program and set datasource for your controls, Or you can use System.Data.OLEDb (I prefer this as it gives me more control over my system)

Dim accessconn As New  _
System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data source=" & pathofAccessFile)
    Try
        accessconn.Open()
        MsgBox("Access succesfully connected")
        accessconn.Close()
        Return 0
    Catch ex As Exception
        accessconn.Close()
        MsgBox("There is something wrong with the path provided.")
    Return 1


    End Try

You will need an understanding of using OLEDb classes though, so here is something to help you out. http://msdn.microsoft.com/en-us/library/System.Data.OleDb(v=vs.110).aspx

Take note of OLEDbCommand(Execute SQL strings), OLEDbDataAdapter(Translate data from DB to Datagridview) and OLEDbDataReader(I use this for selecting data from the Database).

Perhaps to help you more with understanding your problem, here is an example of how I use DataReader Class:

  Public Sub AccesstoGridd(ByVal utos As String, ByVal pathh As String, ByVal gridd As DataGridView, ByVal ngalan As String)
    Dim accessconn As New System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data source=" & pathh)
    Try


        Dim sqlutos As New System.Data.OleDb.OleDbCommand(utos, accessconn)
        Dim idcounter As Integer = 0

        accessconn.Open()
        Dim reader As System.Data.OleDb.OleDbDataReader
        reader = sqlutos.ExecuteReader
        While reader.Read
            idcounter = idcounter + 1
            gridd.Rows.Add(idcounter, reader(0), reader(1), reader(2), reader(3), reader(4), reader(5), ngalan)
        End While
        reader.Close()


        accessconn.Close()
    Catch ex As Exception
        accessconn.Close()
        MsgBox("There is something wrong with the Access file selected." & vbNewLine & ex.ToString, MsgBoxStyle.Exclamation, "MDB file unrecognized.")
    End Try

End Sub
于 2013-10-28T23:49:18.220 回答