1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

how do you connect to a microsoft databse using vb

Discussion in 'Visual Basic ( VB )' started by bymbah, Jan 4, 2008.

  1. bymbah

    bymbah New Member

    Joined:
    Dec 12, 2006
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    can anyone help with information on how you can use visual basic to connect to a ms db for the purposes of retrieving and storing data to tables in a ms db.i think that any one who can post sample code will help around the problem
     
  2. KiLLER

    KiLLER New Member

    Joined:
    Mar 2, 2008
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    If you are using VB.NET then

    Code:
    Imports System.Data.OleDb
    Public Class Form1
        Dim da As OleDbDataAdapter
        Dim ds As New DataSet
        Dim con As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source =C:\db1.mdb;") 'you can edit the source as you like....
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            da = New OleDbDataAdapter("Select * from student", con) 'student is table's name
    
            con.Open()
            da.Fill(ds, "student")
        End Sub
        'This is basic, i would advise you to read books for more or Google it. 
    
    If you are using VB6 then it different method for this. You will need to add Reference manually to the Project and it's called ADODB

    Code:
        Public con As New ADODB.Connection 'Database connection string
        Public rsMain As New ADODB.Recordset 'Database record set 
        
        Private Sub ConnectToDB
            con.Open ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source="C:\db1.mdb")
            rsMain.Open "Select * from Table1", con, OpenDynamic, LockOptimistic
            'Now you can use rsMain to retrieve and add data to your database
        End Sub
    
    It seems like you have never done this before so I would advice you to read books and gain knowledge on how Databases work, as it'll be very helpful in future. And don't forget GOOGLE. :)
     
  3. bymbah

    bymbah New Member

    Joined:
    Dec 12, 2006
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Thanx for your contribution kiLLER.
     
  4. Darkness1337

    Darkness1337 New Member

    Joined:
    Mar 15, 2007
    Messages:
    130
    Likes Received:
    1
    Trophy Points:
    0
    you can also use Data Control :]
     

Share This Page