gpt4 book ai didi

ASP.NET 4、VSD2010和MySQL 5使用ASP.net进行连接

转载 作者:行者123 更新时间:2023-11-29 14:58:15 25 4
gpt4 key购买 nike

我正在尝试从 ASP.net 代码连接到 MySQL 数据库。有些我的连接无法正常工作。

对于下面的代码,我得到无效的参数 DRIVER。你能帮我纠正这个问题吗?

或者给我看一些示例代码。

Label1.Text = ""
Label2.Text = ""
Try
Dim conStr As New SqlClient.SqlConnection
conStr.ConnectionString = "DRIVER={MySQL ODBC 5.1 Driver};" +
"SERVER=localhost;" +
"DATABASE=aa;" +
"UID=aa;" +
"PASSWORD=aa;" +
"OPTION=3"

Response.Write("Connection string: " & conStr.ConnectionString)
conStr.Open()
If conStr.State = ConnectionState.Open Then
Label1.Text = "SQLConnection conStr is Open"
conStr.Close()
ElseIf conStr.State = ConnectionState.Closed Then
Label1.Text = "SQLConnection conStr is closed"
End If
Catch sqlxcp As SqlClient.SqlException
Label2.Text = sqlxcp.ToString

Finally
End Try

谢谢

最佳答案

SqlClient 用于 SQL Server。通过上面的连接字符串,您需要使用 OBBC 提供程序类(来自 here ):

using Microsoft.Data.Odbc;

namespace myodbc3
{
class mycon
{
static void Main(string[] args)
{
try
{
//Connection string for Connector/ODBC 3.51
string MyConString = "DRIVER={MySQL ODBC 3.51 Driver};" +
"SERVER=localhost;" +
"DATABASE=test;" +
"UID=venu;" +
"PASSWORD=venu;" +
"OPTION=3";

// Connect to MySQL using Connector/ODBC
OdbcConnection MyConnection = new OdbcConnection(MyConString);
MyConnection.Open();
...

使用MySql.Data.MySqlClient.MySqlConnection类,MySQL连接应采用以下形式:

Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;

(默认端口为3306)

如果您使用 MySQL Connector/ODBC 3.51 (您的代码建议):

Driver={MySQL ODBC 3.51 Driver};
Server=myServerAddress;Database=myDataBase;
User=myUsername; Password=myPassword;Option=3;

ConnectionStrings网站是一个很好的资源。

关于ASP.NET 4、VSD2010和MySQL 5使用ASP.net进行连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3951933/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com