NET和VB.net代码落后。我有一个经典的ASP页面,使用以下代码连接到mySQL服务器:
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
...
但是,我无法在ASP.NET(VB.NET)中找到任何连接到mySQL的东西。我只发现了一些代码,一旦它到达“Dim conn As New OdbcConnection(MyConString)”代码似乎不起作用:
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()
我也有这些导入声明:
<%@ 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 %>
错误如下:
编译器错误消息:BC30002:未定义类型“OdbcConnection”。
来源错误:
Line 121: "OPTION=3;"
Line 122:
Line 123: Dim conn As New OdbcConnection(MyConString) '<--error line
Line 124: conn.Open()
Line 125:
所以任何帮助都会很棒! :O)
编辑:按照这种方式工作
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()
使用mysql.data.DLL播放
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()
大卫
答案 0 :(得分:0)
1)从MySQL site
获取最新的连接器 2)根据他们提供的内容,将MySql.Data.dll
复制到您的网络应用的Bin
文件夹
3)将此行添加到顶部的代码隐藏中:
Imports MySql.Data.MySqlClient
4)使用此代码,更新连接字符串的全部大写部分:
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
修改强>
?语法是为了避免SQL注入,你也可以连接,
答案 1 :(得分:0)
尝试添加:
<%@ Import Namespace=System.Data.Odbc %>