Results 1 to 8 of 8

Thread: Connecting to MySQL DB with ADO.net

  1. #1

    Thread Starter
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336

    Connecting to MySQL DB with ADO.net

    I have a MySQL Database on a remote server that i would like to connect to with ADO.net... i have never used ADO.net so how would i go about this?

    Thank you!
    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


  2. #2
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803
    Well, I only know how to do it with ODBC

    To make it work for .NET you need to install the ODBC .NET Data Provider, MySQL ODBC drivers, and I see you already have MySQL

    Here's a link I found on the net that helped me do it...
    http://myhome.spu.edu/tbuiten/4800/setup.asp (first 5 steps)

    Then when developing, in you ASP .NET program you need to add a reference to the Microsoft.Data.Odbc.dll and Import it in the pages you use the DB (Imports Microsoft.Data.Odbc)

    And here's some code I use in Global.aspx to connect with MySQL, just to get you started:
    VB Code:
    1. Try
    2.             If Application("Connection") Is Nothing Then
    3.                 Dim mySQLConnStr As String = "Driver={MySQL};" & _
    4.                                             "Server=localhost;" & _
    5.                                             "Port=3306;" & _
    6.                                             "Option=131072;" & _
    7.                                             "Stmt=;" & _
    8.                                             "Database=the_db_in_MySQL_Admin;" & _
    9.                                             "Uid=your_user_name;" & _
    10.                                             "Pwd=your_password;"
    11.  
    12.                 Dim cn As OdbcConnection = New OdbcConnection(mySQLConnStr)
    13.                 Application("Connection") = cn
    14.                 cn.Open()
    15.  
    16.                 Dim cmd As New OdbcCommand("SELECT Value FROM t_variables WHERE VarName = 'Users';", cn)
    17.                 Dim ret As Object = cmd.ExecuteScalar()
    18.  
    19.                 If Not IsDBNull(ret) Then Application("Visitors") = CInt(Val(ret))
    20.             End If
    21.         Catch ex As OdbcException
    22.             Application("Connection") = Nothing
    23.             Application("ConnectionError") = ex.Message
    24.         End Try

    And since you use a remote server, I think you just have to change the Server param in the connection string to your server name/ip

    Hope this helps

  3. #3

    Thread Starter
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336
    Hi and thanks. I installed everything the page said but when i try to run your code it says

    type OdbcConnection not defined

    it saus that anywhere there is an Odbc refrence...

    I copied your code exactly...
    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


  4. #4
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Unless you are using VS.NET 2003 then you have to install the .NET ODBC drivers. Did you do that? You also need the following Imports: Imports Microsoft.Data.ODBC

  5. #5

    Thread Starter
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336
    Yah i installed them... i got the file from here http://download.microsoft.com/downlo...S/odbc_net.msi

    The imports statement says Namspace or type ODBC for the imports Microsoft.Data.ODBC cannot br found.


    hmmm..
    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


  6. #6
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803
    You also have to add a reference to it, like in the picture below, right click on the References and choose "Add Reference..." then select "Microsoft.Data.Odbc.dll"


  7. #7

    Thread Starter
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336
    You do?? Damn i thought thats what Import was for....
    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


  8. #8
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803
    The Reference thing in .NET is same as the reference in VB6, it adds a reference to the whole project, but in .NET you also have to tell it in what page you want to make the reference avalilable, then you use Imports in that page...

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