views:

227

answers:

3

Hey all i am new to ASP.NET and VB.net code behind. I have a classic ASP page that connects to the mySQL server with the following code:

 Set oConnection = Server.CreateObject("ADODB.Connection")
 Set oRecordset = Server.CreateObject("ADODB.Recordset")

 oConnection.Open "DRIVER={MySQL ODBC 3.51 Driver}; SERVER=xxx.com; PORT=3306;      DATABASE=xxx; USER=xxx; PASSWORD=xxx; OPTION=3;"
 sqltemp = "select * from userinfo WHERE emailAddress = '" & theUN & "'"
 oRecordset.Open sqltemp, oConnection,3,3

 if oRecordset.EOF then
 ...

However, i am unable to find anything to connect to mySQL in ASP.NET (VB.NET). I have only found this peice of code that does not seem to work once it gets to the "Dim conn As New OdbcConnection(MyConString)" code:

 Dim MyConString As String = "DRIVER={MySQL ODBC 3.51 Driver};" & _
 "SERVER=xxx.com;" & _
 "DATABASE=xxx;" & _
 "UID=xxx;" & _
 "PASSWORD=xxx;" & _
 "OPTION=3;"

 Dim conn As New OdbcConnection(MyConString)
 conn.Open()

 Dim MyCommand As New OdbcCommand
 MyCommand.Connection = MyConnection
 MyCommand.CommandText = "select * from userinfo WHERE emailAddress = '" & theUN & "'""
 MyCommand.ExecuteNonQuery()
 MyConnection.Close()

I have these import statements also:

 <%@ Import Namespace=System %>
 <%@ Import Namespace=System.IO %>
 <%@ Import Namespace=System.Web %>
 <%@ Import Namespace=System.ServiceProcess %>
 <%@ Import Namespace=Microsoft.Data.Odbc %>
 <%@ Import Namespace=MySql.Data.MySqlClient %>
 <%@ Import Namespace=MySql.Data %>
 <%@ Import Namespace=System.Data %>

The error is as follows:

Compiler Error Message: BC30002: Type 'OdbcConnection' is not defined.

Source Error:

 Line 121:  "OPTION=3;"
 Line 122:  
 Line 123:  Dim conn As New OdbcConnection(MyConString) '<--error line
 Line 124:  conn.Open()
 Line 125:  

So any help would be great! :o)

EDIT: GOT IT WORKING USING THIS WAY

 Dim MyConString As String = "DRIVER={MySQL ODBC 3.51 Driver};" & _
 "SERVER=xxx.com;" & _
 "DATABASE=xxx;" & _
 "UID=xxx;" & _
 "PASSWORD=xxx;" & _
 "OPTION=3;"

 Dim conn As OdbcConnection = New OdbcConnection(MyConString)
 conn.Open()

 Dim MyCommand As New OdbcCommand
 MyCommand.Connection = conn
 'MyCommand.CommandText = "INSERT INTO tablename VALUES("val1","val2","val3")"
 'MyCommand.ExecuteNonQuery()
 conn.Close()

AND WITH THE mysql.data.DLL IN PLAY

 Dim MyConString As String = "DRIVER={MySQL ODBC 3.51 Driver};" & _
    "SERVER=xxx.com;" & _
    "DATABASE=xxx;" & _
    "UID=xxx;" & _
    "PASSWORD=xxx;" & _
    "OPTION=3;"

    Dim conn As New MySqlConnection(MyConString)
    conn.Open()

    Dim MyCommand As New MySqlCommand
    MyCommand.Connection = conn
    'MyCommand.CommandText = "INSERT INTO tablename VALUES("val1","val2","val3")"
    'MyCommand.ExecuteNonQuery()
    conn.Close()

David

A: 

What do you mean by "Does not seem to work?" Do you receive an error message?

Aaron
I would get an error message but its turned off and i do not know how to turn it on... Server Error in '/' Application.Runtime ErrorDescription: An application error occurred on the server. The current custom error settings for this application prevent the details of the application error from being viewed remotely (for security reasons). It could, however, be viewed by browsers running on the local server machine.
StealthRT
A: 

1) Get the newest connector from the MySQL site

2) From what they give you, copy the MySql.Data.dll to your Bin folder for your web app

3) Add this line to your code-behind at the top:

Imports MySql.Data.MySqlClient

4) Use this code, updating the all-caps portions of the connection string:

    Using Con As New MySqlConnection("Database=DB;Server=SERVER-NAME;User Id=USERNAME;Password=PASSWORD;ignore prepare=false")
        Con.Open()
        Using Com As New MySqlCommand("select * from userinfo WHERE emailAddress=?emailAddress", Con)
            Com.CommandType = Data.CommandType.Text
            Com.Parameters.AddWithValue("?emailAddress", theUN)
            Using RDR = Com.ExecuteReader()
                If RDR.HasRows Then
                    Do While RDR.Read
                        'Do stuff here
                        Response.Write(RDR.Item("emailAddress").ToString())
                    Loop
                End If
            End Using
        End Using
        Con.Close()
    End Using

Edit

The ? syntax is to avoid SQL injection, you could concatenate instead, too

Chris Haas
The mySQL server is not on a local server (my own). I wont be able to copy any dll files over. They do, however, have the 5.x version but i was just using code from my classic ASP page that works and it just happens to use 3.x drivers.
StealthRT
If you're running .Net you should be able to just add DLLs to your Bin folder. You don't have to regsvr32 them anymore like you used to do in classic ASP
Chris Haas
Do they not already have that provided since they are running mySQL 5.x?
StealthRT
And i also do not have a BIN folder..
StealthRT
Its just a regular folder, nothing special, you can just make it
Chris Haas
Thanks, Chris. I got it working both ways now :o). Which is better to use? The DLL or the Odbc Connection?
StealthRT
A: 

Try adding:

<%@ Import Namespace=System.Data.Odbc %>
tloflin
Still get the same error: Compiler Error Message: BC30002: Type 'MySqlConnection' is not defined.
StealthRT
Hmm, that is a different error, and "MySqlConnection" is not used anywhere in the code you've showed us. Are you using that type somewhere else? Anyway, it sounds like the MySql library you're referencing either isn't hooked up right or doesn't have the functionality you're trying to use; unfortunately I don't have experience with that particular library. You should be able to find documentation on it at whatever site the DLLs came from, though.
tloflin