SqlConnection无法打开连接
是)我有的
- https://www.freesqldatabase.com上的帐户
- 一个数据库(我的asp.net应用程序中的DB_DATABASE var)
- DB的用户和密码( DB_USER , DB_PASS )
- 主机URL(sql4.freesqldatabase.com [ DB_SERVER ])
- 目录,加上三个表( DB_CATALOG )
- 一个端口( DB_PORT )
我的目标是什么
从我的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 + ";");
除了这些之外还有几个选项,试图修改连接字符串,但还没有人工作=(
我得到了什么
好吧,当我试图打开连接时,我得到以下exception(我试图在这里寻找任何有用的数据,但我还不习惯这个……):
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)
另外,发现了
在https://www.freesqldatabase.com/category/net/ ,他们说他们说过:
由于某些MySQL连接器中存在错误,因此返回“与服务器不同步”错误。
要解决此问题,请使用不同版本的MySQL连接器。
但由于我在exception堆栈跟踪中没有看到这个,我不确定我是否受此影响(加上它的2011年新闻)。
建立连接时我做错了吗? 我该如何解决?
先感谢您!
所有以“Sql”开头的对象,例如“SqlConnection”,“SqlCommand”等,都是为SQL Server制作的。 显然你正在使用MySQL,所以你需要一个不同的提供者: https : //dev.mysql.com/downloads/connector/net/
将此提供程序添加为项目中的引用后,您将能够使用“MySqlConnection”,“MySqlCommand”等。
上述就是C#学习教程:SqlConnection无法打开连接分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/1004883.html