开发者

VB.net (aspx) mysql connection

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=example.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=example.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 %&g开发者_高级运维t;
 <%@ 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=example.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=example.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


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


Try adding:

<%@ Import Namespace=System.Data.Odbc %>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜