无法连接到本地mysql数据库

时间:2017-12-01 01:03:16

标签: c# mysql

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

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

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

我这里的连接字符串是点击我的数据库:' gpbuddy'并复制'连接字符串'来自物业部分。

我也试过这个连接字符串

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

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

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

我收到以下错误:

<PackageReference Include="System.Data.SqlClient" Version="4.4.0" />

BTW:我使用的是.NET Core 2.0,我在.csproj中使用了以下引用 <label id="lbl1" fupsetLabel="Current Status,|sltcurrent_status" runat="server" > <select id="sltcurrent_status" runat="server" dfield="sltcurrent_status" class="form-control" funsetForm="form2" funsetMsgName="Current Status" funsetOptionsFromTable="astatus,atype,where id='##{$page->evtp}#'" ></select> </label>

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

2 个答案:

答案 0 :(得分:1)

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

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

然后转到Visual Studio,在您的项目下右键单击References&gt;添加引用并将MySql.Data引用添加到项目中。

在代码中,使用MySqlConnection代替SqlConnection,如以下代码段所示:

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

答案 1 :(得分:-1)

试试这个

- &gt;创建.txt文件。

- &gt;将其扩展名更改为.udl(这是一个连接字符串创建向导)。

- &gt;打开弹出向导的文件。

- &gt;填写详细信息并单击测试连接。

如果连接成功,只需将扩展名恢复为.txt即可获得连接字符串。

希望这可以解决问题。