Results 1 to 13 of 13

Thread: [Resolved] Open Database

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2006
    Location
    Osaka
    Posts
    200

    Resolved [Resolved] Open Database

    I want to open mysql database hosted on server from vb program on desktop.How to accomplish it ?
    Thank You.
    Last edited by Hack; Jan 17th, 2006 at 11:16 AM.

  2. #2
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: Open Database

    www.connectionstrings.com will be able to help you there. or you can search the forum for MySQL and youll get many responses to possible resolution

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jan 2006
    Location
    Osaka
    Posts
    200

    Re: Open Database

    Error i am getting on command
    cn.Open

    Data Source Name not found and no default driver specified.


    VB Code:
    1. Option Explicit
    2. Private cn As ADODB.Connection
    3. Private rs As ADODB.Recordset
    4.  
    5.  
    6. Private Sub Form_Load()
    7. Me.MousePointer = 11 'this makes the mouse pointer the hourglass
    8.  
    9.     Set cn = New ADODB.Connection 'we've declared it as a ADODB connection lets set it.
    10.     cn.ConnectionString = "Driver={mySQL};Server=data.domain.com;Port=3306;Option=131072;Stmt=;Database=my-database;Uid=username;Pwd=password;"
    11.     cn.Open
    12.     Set rs = New ADODB.Recordset 'as we did with the connection
    13.     rs.Open "tbl_master", cn, adOpenKeyset, adLockPessimistic, adCmdTable 'opening the recordset explained in the notes
    14.        
    15. rs.MoveFirst 'moves to the first record
    16. Do Until rs.EOF = True 'this is the Loop to add items to the combo box
    17. Combo1.AddItem rs.Fields("field1") 'this adds items from field1 into the combo box
    18. rs.MoveNext 'moves next record
    19. Loop
    20. rs.MoveFirst
    21. fillfields 'i’ll explain this later on.
    22.  
    23. Me.MousePointer = 0 'sets the mouse pointer to the normal arrow
    24.  
    25. End Sub
    26. Public Sub fillfields()
    27.     If Not (rs.BOF = True Or rs.EOF = True) Then 'Checks if we are at the first or last record. This is use a lot.
    28.         Text1.Text = rs.Fields("Field2") 'text1 = field2 and display that data
    29.       Text2.Text = rs.Fields("Field3") 'as above
    30.   Combo1.Text = rs.Fields("Field1") 'as above
    31.              Else
    32.              MsgBox "Either you are at the first record or the last record.", vbExclamation, "Cannot Move"
    33.     End If
    34. End Sub

    I have used reference to Microsoft ActiveX Object 2.5 Library
    and Connection String used from above mentioned site.
    Thank You.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jan 2006
    Location
    Osaka
    Posts
    200

    Re: Open Database

    I am getting error
    [MySQL][ODBC 3.51 Driver]Can't connect to MySQL server on xxx.xx.Xxx.xxx' (10065)
    When trying to set DSN from Adminstrative Tools in CP.

    I am using correct host ip where my site is hosted.(share hosted).

  5. #5
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: Open Database

    you would do something like this:
    VB Code:
    1. Dim SQL As String
    2. Dim conn As ADODB.Connection
    3. Set conn = New ADODB.Connection
    4.  
    5. Dim rs As ADODB.Recordset
    6. Set rs = New ADODB.Recordset
    7.  
    8. conn.CursorLocation = adUseClient
    9. conn.ConnectionString = "DRIVER={MySQL ODBC 3.51 Driver};" _
    10. & "SERVER=[IP_ADDRESS_OF_SERVER_HERE;" _
    11. & "DATABASE=[DATABASE_NAME];" _
    12. & "UID=[DATABASE_USERNAME];" _
    13. & "PWD=[DATABASE_PASSWORD];" _
    14. & "OPTION=" & 1 + 2 + 8 + 32 + 2048 + 16384
    15.  
    16. conn.Open conn.ConnectionString
    17.  
    18. 'THIS IS AN EXAMPLE SQL STATEMENT
    19. SQL = "SELECT * FROM My_Table WHERE Column_name = '" & txtBox.Text & "' "
    20.  
    21. rs.Open SQL, conn, adOpenStatic, adLockReadOnly
    22.  
    23. 'DO WHAT YOU NEED TO AFTER YOU HAVE MADE A REQUEST TO
    24. 'MYSQL SERVER USING AN SQL STATEMENT
    25.  
    26. If rs.State = adStateOpen Then
    27.    rs.Close
    28. End If
    29. Set rs = Nothing
    30.  
    31. conn.Close
    32. Set conn = Nothing

    You will also need to go to www.MySQL.com and download the drivers for "MySQL ODBC 3.51 Driver" and install them on your machine. If you dont have the drivers, you wont get anywhere. its like trying to go through a locked door with no key.

  6. #6
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Open Database

    You need the ODBC driver for MySQL, download it from their site...
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  7. #7

  8. #8
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Open Database

    I'm a minute too late!
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  9. #9
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: Open Database

    Quote Originally Posted by slice
    Error i am getting on command
    cn.Open

    Data Source Name not found and no default driver specified.


    VB Code:
    1. Option Explicit
    2. Private cn As ADODB.Connection
    3. Private rs As ADODB.Recordset
    4.  
    5.  
    6. Private Sub Form_Load()
    7. Me.MousePointer = 11 'this makes the mouse pointer the hourglass
    8.  
    9.     Set cn = New ADODB.Connection 'we've declared it as a ADODB connection lets set it.
    10.     cn.ConnectionString = "Driver={mySQL};Server=data.domain.com;Port=3306;Option=131072;Stmt=;Database=my-database;Uid=username;Pwd=password;"
    11.     cn.Open
    12.     Set rs = New ADODB.Recordset 'as we did with the connection
    13.     rs.Open "tbl_master", cn, adOpenKeyset, adLockPessimistic, adCmdTable 'opening the recordset explained in the notes
    14.        
    15. rs.MoveFirst 'moves to the first record
    16. Do Until rs.EOF = True 'this is the Loop to add items to the combo box
    17. Combo1.AddItem rs.Fields("field1") 'this adds items from field1 into the combo box
    18. rs.MoveNext 'moves next record
    19. Loop
    20. rs.MoveFirst
    21. fillfields 'i’ll explain this later on.
    22.  
    23. Me.MousePointer = 0 'sets the mouse pointer to the normal arrow
    24.  
    25. End Sub
    26. Public Sub fillfields()
    27.     If Not (rs.BOF = True Or rs.EOF = True) Then 'Checks if we are at the first or last record. This is use a lot.
    28.         Text1.Text = rs.Fields("Field2") 'text1 = field2 and display that data
    29.       Text2.Text = rs.Fields("Field3") 'as above
    30.   Combo1.Text = rs.Fields("Field1") 'as above
    31.              Else
    32.              MsgBox "Either you are at the first record or the last record.", vbExclamation, "Cannot Move"
    33.     End If
    34. End Sub

    I have used reference to Microsoft ActiveX Object 2.5 Library
    and Connection String used from above mentioned site.
    Thank You.
    You opened the connection but you didnt close it when you were done with it. this is always a bad idea to keep the connection open when not in use

  10. #10

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Jan 2006
    Location
    Osaka
    Posts
    200

    Re: Open Database

    I am getting error
    [MySQL][ODBC 3.51 Driver]Can't connect to MySQL server on xxx.xx.Xxx.xxx' (10065)
    When trying to set DSN from Adminstrative Tools in CP.

    I am using correct host ip where my site is hosted.(share hosted).
    Last edited by slice; Jan 16th, 2006 at 06:43 AM.

  12. #12
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Open Database

    Yep, you said that before.

    Have you tried what was suggested in post #5 ? (especially the bit at the bottom)

  13. #13
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: Open Database

    Quote Originally Posted by slice
    I am getting error
    [MySQL][ODBC 3.51 Driver]Can't connect to MySQL server on xxx.xx.Xxx.xxx' (10065)
    When trying to set DSN from Adminstrative Tools in CP.

    I am using correct host ip where my site is hosted.(share hosted).
    this is the thing, you wont need to use odbcad32.exe to set up anything to your mysql because the connection string in your app would take care of all of that.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width