【问题标题】:Can't open sql connection in cs file - asp.net无法在 cs 文件中打开 sql 连接 - asp.net
【发布时间】:2015-08-29 02:01:31
【问题描述】:

我是 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 中做点什么?我也尝试关闭防火墙。谢谢。

【问题讨论】:

    标签: c# mysql asp.net database localhost


    【解决方案1】:

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

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

    以下是一个示例连接字符串:

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

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

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

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

    此外,您可能不需要User Id 更改为Uid,并将Password 更改为Pwd,选项文档允许以下内容:

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

    User Id, UserID, Username, Uid, User name, User:正在使用的 MySQL 登录账户。

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

    最后,您需要 MySQL 连接器。然后你需要把你的SqlConnections换成MySqlConnections.

    【讨论】:

    • 感谢您的回复,但似乎没有帮助,仍然出现同样的错误。
    • 您的服务器在线吗?您是否使用了正确的 IP?它在标准端口上吗?
    • 是的,它应该在线,带有 GridVIew 的页面正在显示来自数据库的正确数据。对于 ip,我尝试了 localhost 和 127.0.0.1。而对于港口,不太熟悉,我该如何检查?
    • localhost 上?不在其他设备上?您可以使用netstat -tnb 并检查一个进程是否与mysql 类似,或者检查端口3306 是否列出。 (这是默认设置。)
    • 好的,运行netstat,发现mysqld.exe在127.0.0.1:5354上运行。我还注意到我的通知托盘中有一个 asp.net 开发服务器的图标,它在端口 55181 上运行
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多