SqlConnection无法打开连接

时间:2015-06-24 19:22:53

标签: c# asp.net database-connection

我拥有什么

  1. http://www.freesqldatabase.com
  2. 中的帐户
  3. 数据库(我的asp.net应用程序中的 DB_DATABASE var)
  4. 数据库的用户和密码( DB_USER DB_PASS
  5. 主机网址(sql4.freesqldatabase.com [ DB_SERVER ])
  6. 目录,加上三个表( DB_CATALOG
  7. 端口( DB_PORT
  8. 我的目标

    从我的asp.net应用程序连接到我的数据库。

    我尝试了什么

    我尝试构建SqlConnection,就像:

    SqlConnection con = new SqlConnection(@"Data Source=(" + DB_SERVER + "\\" + DB_DATABASE + "," + DB_PORT + ");Initial Catalog=" + DB_CATALOG + ";UID=" + DB_USER + ";PWD=" + DB_PASS + ";");
    

    我已使用DB_DATABASE作为实例名称,但我不确定(任何解释都可以澄清这些混乱!)

    但是我也试过没有它:

    SqlConnection con = new SqlConnection(@"Data Source=(" + DB_SERVER + "," + DB_PORT + ");Initial Catalog=" + DB_CATALOG + ";UID=" + DB_USER + ";PWD=" + DB_PASS + ";");
    

    除了这些选项之外还有几个选项,试图修改连接字符串,但还没有人工作=(

    我获得的是什么

    好吧,当我尝试打开连接时,我收到了以下异常(我已尝试在此处查找任何有用的数据,但我不习惯这个......):

       en System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
       en System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
       en System.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo, SqlInternalConnectionTds connHandler, Boolean ignoreSniOpenTimeout, Int64 timerExpire, Boolean encrypt, Boolean trustServerCert, Boolean integratedSecurity, Boolean withFailover)
       en System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean ignoreSniOpenTimeout, TimeoutTimer timeout, Boolean withFailover)
       en System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString connectionOptions, SqlCredential credential, TimeoutTimer timeout)
       en System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(TimeoutTimer timeout, SqlConnectionString connectionOptions, SqlCredential credential, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance)
       en System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData)
       en System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
       en System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions)
       en System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
       en System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
       en System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)
       en System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)
       en System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
       en System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
       en System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
       en System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry)
       en System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
       en System.Data.SqlClient.SqlConnection.Open()
       en Gallery.Gallery.registerNewUser(String username, String encripted_password)
    

    另外,发现

    http://www.freesqldatabase.com/category/net/,他们说他们已经说过:

      

    由于某些MySQL连接器中存在错误,因此会返回“与服务器不同步”错误。

         

    要解决此问题,请使用不同版本的MySQL连接器。

    但是由于我在异常堆栈跟踪中没有看到这一点,我不确定我是否受此影响(加上它的2011年新闻)。

    构建连接时我做错了什么?我该如何解决?

    提前谢谢!

1 个答案:

答案 0 :(得分:1)

所有以“Sql”开头的对象,例如“SqlConnection”,“SqlCommand”等,都是为SQL Server制作的。显然您正在使用MySQL,因此您需要一个不同的提供商:https://dev.mysql.com/downloads/connector/net/

在项目中添加此提供程序作为引用后,您将能够使用“MySqlConnection”,“MySqlCommand”等。