繁体   English   中英

无法在CS文件中打开SQL连接-ASP.NET

[英]Can't open sql connection in cs file - asp.net

我是ASP.NET的新手,并跟随着一个教程,在该教程中,用户可以注册到站点,并检查数据库以查看用户名是否已存在。 创建数据库的时间到了,该教程说要在Visual Studio(mdf文件)中创建数据库,但是我使用MYSQL Workbench,因此我在那里创建了数据库并尝试自行连接。

在Visual中的“数据连接”下添加连接时,我能够看到我的数据库,并且测试连接有效。

添加SQLDataSource时,我能够成功运行测试查询并从数据库中读取。 我什至将它与GridView一起使用,以在网页上显示数据。

但是,当要在cs文件中打开连接时,我遇到了错误。

[Win32Exception (0x80004005): The system cannot find the file specified]

[SqlException (0x80131904): 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.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +5347119
   System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) +546
   System.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo, SqlInternalConnectionTds connHandler, Boolean ignoreSniOpenTimeout, Int64 timerExpire, Boolean encrypt, Boolean trustServerCert, Boolean integratedSecurity, Boolean withFailover) +5358907
   System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean ignoreSniOpenTimeout, TimeoutTimer timeout, Boolean withFailover) +145
   System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString connectionOptions, SqlCredential credential, TimeoutTimer timeout) +892
   System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(TimeoutTimer timeout, SqlConnectionString connectionOptions, SqlCredential credential, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance) +311
   System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData) +646
   System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions) +278
   System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions) +38
   System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection) +732
   System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection) +85
   System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection) +1057
   System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection) +78
   System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection) +196
   System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions) +146
   System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions) +16
   System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry) +94
   System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry) +110
   System.Data.SqlClient.SqlConnection.Open() +96
   Registration.Page_Load(Object sender, EventArgs e) in e:\WebSite\Registration.aspx.cs:15
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +51
   System.Web.UI.Control.OnLoad(EventArgs e) +92
   System.Web.UI.Control.LoadRecursive() +54
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +772

错误出现在代码中

15 SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["RegisterCS"].ConnectionString);
16 conn.Open();//ERROR HERE

这是我的web.config,以防万一

<?xml version="1.0"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  <connectionStrings>
    <add name="RegisterCS" connectionString="server=localhost;User Id=root;password=******;database=asptest"
      providerName="MySql.Data.MySqlClient" />
  </connectionStrings>
  <system.web>
    <compilation debug="true" targetFramework="4.0"/>
  </system.web>


</configuration>

我环顾四周,显然可以是连接字符串,但是我认为应该没问题,因为数据源实际上是从数据库成功读取的,对吗? 我缺少一些可以正确连接数据库的东西吗? 也许我必须在Workbench中进行某些操作? 我也尝试过关闭防火墙。 谢谢。

MySQL连接字符串分别是UidPwd来代替User IdPassword

http://dev.mysql.com/doc/connector-net/zh/connector-net-programming-connecting-connection-string.html

以下是示例连接字符串:

 Server=127.0.0.1;Uid=root;Pwd=12345;Database=test; 

在此示例中, MySqlConnection对象配置为使用用户名root和密码12345连接到位于127.0.0.1的MySQL服务器。 所有语句的默认数据库将是test数据库。

此外,评论讨论导致揭示该Port不是3306的标准/默认值,而是5354 因此,连接字符串应如下所示:

Server=localhost;Uid=root;Pwd=******;Database=asptest;Port=5354

此外,您可能不需要改变User IdUid ,和PasswordPwd ,对于选择的文件允许如下:

密码,pwd; 使用的MySQL帐户的密码。

用户ID,用户ID,用户名,Uid,用户名,用户:正在使用的MySQL登录帐户。

http://dev.mysql.com/doc/connector-net/zh/connector-net-connection-options.html

最后,您需要MySQL连接器。 然后,您需要将SqlConnections切换为MySqlConnections.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM