gpt4 book ai didi

c# - 无法连接本地mysql数据库

转载 作者:行者123 更新时间:2023-11-29 18:08:44 29 4
gpt4 key购买 nike

我正在尝试连接到我使用 MySQL Workbench 在本地创建的数据库。我能够将数据库添加到我的服务器资源管理器中,当我测试连接时,我收到一条“成功”消息。我可以展开下拉菜单并查看我创建的表“项目”以及表中存储的任何信息。

当我尝试使用以下内容时:

using (SqlConnection connection = new SqlConnection("server=localhost;user id=root;persistsecurityinfo=True;database=gpbuddy"))
{
connection.Open(); // Failure here
Console.WriteLine(connection.State);
}

我这里的连接字符串来自单击我的数据库:“gpbuddy”并从属性部分复制“连接字符串”。

我也尝试过这个连接字符串

"server=localhost;user id=root;password=mypass;persistsecurityinfo=True;database=gpbuddy"

我还尝试创建一个名为 admin 的新数据库用户:

"server=localhost;user id=admin;password=mypass;persistsecurityinfo=True;database=gpbuddy"

我收到以下错误:

System.Data.SqlClient.SqlException: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) ---> System.ComponentModel.Win32Exception: The network path was not found
--- End of inner exception stack trace ---
at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, Object providerInfo, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, Boolean applyTransientFaultHandling)
at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions)
at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
at System.Data.SqlClient.SqlConnection.Open()
at GEScanner.Tools.Database.testDbConnection() in C:\Users\Acina\Documents\Visual Studio 2017\Projects\GEScanner\GEScanner\Tools\Database.cs:line 28
ClientConnectionId:00000000-0000-0000-0000-000000000000
Error Number:53,State:0,Class:20

顺便说一句:我正在使用 .NET Core 2.0,我在 .csproj 中使用以下引用 <PackageReference Include="System.Data.SqlClient" Version="4.4.0" />

编辑:对具有相同代码和相同错误的普通 C# 控制台应用程序进行了快速测试,因此我不认为 .NET Core 是这里的问题。绝对与通过 MySQL Workbench 创建数据库有关。

最佳答案

由于我使用的是 MySQL 数据库而不是 Microsoft SQL Server 数据库,因此我必须使用 MySQL 提供的库。

要使用 MySQL 提供的库,请下载并安装:https://dev.mysql.com/downloads/connector/net/

然后转到 Visual Studio,在您的项目下右键单击“引用”>“添加引用”并将 MySql.Data 引用添加到您的项目。

在代码中,使用 MySqlConnection 而不是 SqlConnection,如以下代码片段所示:

using (MySqlConnection connection = new MySqlConnection(connectionString))
{
connection.Open();
Console.WriteLine(connection.State);
}

关于c# - 无法连接本地mysql数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47585127/

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