locked
How to Connect a DataBase like ADODB (Visual Basic 6.0)

    Question

  • Hi...

    I'm new on Visual Basic 2005, and I don't know how to connect a DataBase to my Application...

    In Visual Basic 6.0, I connect a DataBase with this code:

    Dim Base As New ADODB.Connection
    Dim Reg As New ADODB.Recordset
    Dim Sql As String

    Base.CursorLocation = adUseClient
    Base.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "/BaseDat/BaseDat.mdb;Persist Security Info=False"

    Sql = "Select * from Table"
    Reg.Open Sql, Base, adOpenDynamic, adLockOptimistic

    Now... my question is: How can I do this on Visual Basic 2005 Express?

    Please help me...

    Regards...

    Wednesday, February 14, 2007 12:29 AM

Answers

  • MetalTux,

    1. Connect to the Access database by Data Source Configuration wizard. Take an example as DataGridView control on a form. In the DataSource property, click Add Project Data Source, choose Database and New Connection and the Microsoft Access Database File. The default database of the wizard is SQL Server database, you have to change the defaut to Access file. After you text the connection correctly, just choose the columns in the table you want to show in the DataGridView control.

    2. Connect to Access database by code:

            Dim conn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Clients.mdb")

            Try

                conn.Open()

                Dim cmd As New OleDbCommand

                cmd.Connection = conn

                cmd.CommandType = CommandType.Text

                cmd.CommandText = "Select distinct ClientName from OrderClient"

                Dim dr As New OleDbDataAdapter(cmd)

                dr.Fill(dt)

                Me.btnFillListBox.Enabled = True

                conn.Close()

            Catch ex As Exception

                MsgBox(ex.Message)

            End Try

    If you don't understand some of the objects or methods such as OleDbConnection, OleDbDataAdapter.Fill, please take a look up in MSDN on the related instructions.

    http://msdn2.microsoft.com/en-us/default.aspx

     

    Wednesday, February 21, 2007 3:16 AM