尝试连接到MySQL服务器:建立与SQL Server的连接时发生与网络相关或特定于实例的错误

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了尝试连接到MySQL服务器:建立与SQL Server的连接时发生与网络相关或特定于实例的错误相关的知识,希望对你有一定的参考价值。

我想连接到mysql服务器,但正如错误所示,我的程序正在尝试连接到SQL Server。如何更改它连接到MySQL?我也在使用NHibernate进行映射。这应该是个问题吗?我安装了MySQL Connector,并添加了参考MySQL.Data和MySql.Web,但没有改变任何东西。

这是我的班级:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using NHibernate;
using NHibernate.Cfg;

namespace ConsoleApp1
{
    class Program
    {
        public void testMethod()
        {
            Configuration config = new Configuration();
            config.Configure();

            ISessionFactory factory = config.BuildSessionFactory();
            ISession session = factory.OpenSession();
            ITransaction tx = session.BeginTransaction();

            User u = new User();
            u.Name = "AAA";
            u.Password = "BBB";

            session.Save(u);
            tx.Commit();

            session.Close();
        }

        static void Main(string[] args)
        {
            Program p = new Program();
            p.testMethod();
        }
    }
}

这是完整的堆栈跟踪:

Unhandled Exception: 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 system cannot find the file specified
   --- End of inner exception stack trace ---
   at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, DbConnectionPool pool, String accessToken, 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.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
   at System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry)
   at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
   at System.Data.SqlClient.SqlConnection.Open()
   at NHibernate.Connection.DriverConnectionProvider.GetConnection()
   at NHibernate.Tool.hbm2ddl.SuppliedConnectionProviderConnectionHelper.Prepare()
   at NHibernate.Tool.hbm2ddl.SchemaMetadataUpdater.GetReservedWords(Dialect dialect, IConnectionHelper connectionHelper)
   at NHibernate.Tool.hbm2ddl.SchemaMetadataUpdater.Update(ISessionFactoryImplementor sessionFactory)
   at NHibernate.Impl.SessionFactoryImpl..ctor(Configuration cfg, IMapping mapping, Settings settings, EventListeners listeners)
   at NHibernate.Cfg.Configuration.BuildSessionFactory()
   at ConsoleApp1.Program.testMethod() in C:UsersStarkDocumentsVisual Studio ProjectsConsoleApp1ConsoleApp1ConsoleApp1Program.cs:line 19
   at ConsoleApp1.Program.Main(String[] args) in C:UsersStarkDocumentsVisual Studio ProjectsConsoleApp1ConsoleApp1ConsoleApp1Program.cs:line 36

另外,hibernate配置文件:

  <session-factory name="Test">
    <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
    <property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
    <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
    <property name="connection.connection_string">Server=localhost;DataBase=assignment_4;uid=root;pwd=1234</property>

    <property name="show_sql">true</property>

    <mapping assembly="ConsoleApp1" />
  </session-factory>

</hibernate-configuration>
答案

您必须更改配置中的详细信息才能使用正确的驱

<property name="connection.driver_class">
    NHibernate.Driver.MySqlDataDriver
</property>

一些例子可以在这里找到:https://www.codeproject.com/Articles/26123/NHibernate-and-MySQL-A-simple-example

以上是关于尝试连接到MySQL服务器:建立与SQL Server的连接时发生与网络相关或特定于实例的错误的主要内容,如果未能解决你的问题,请参考以下文章

从 Visual Studio 连接到 SQL Server 2005

mysql表格无法查到

尝试连接到 MySQL 数据库时 java 中的 NullPointerException

无法远程连接到 SQL Server 实例

尝试将客户端连接到服务器时出现Node.js Socket.io错误

System.Data.SqlClient.SqlException - 无法通过Management Studio连接到SQL Server