2015-06-12 21 views
0

我是ASP.NET新手,並且遵循一個教程,其中用戶可以註冊到站點並檢查數據庫以查看用戶名是否已存在。當創建數據庫的時候,教程說要在visual studio(mdf文件)中創建它,但是我使用MYSQL Workbench,所以我在那裏創建了它並嘗試連接它自己。無法在cs文件中打開sql連接 - asp.net

在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中需要一些東西?我也嘗試了防火牆。謝謝。

回答

2

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數據庫。

此外,意見的討論導致其顯露Port不是3306標準/默認,但5354。因此,連接字符串應該如下:

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

此外,您可能需要改變User IdUid,並PasswordPwd,對於選擇的文件允許進行以下:

密碼,pwd;正在使用的MySQL帳戶的密碼。

用戶標識,用戶標識,用戶名,用戶名,用戶名,用戶:正在使用的MySQL登錄帳戶。

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

最後,你需要的MySQL連接。然後,您需要將您的SqlConnections換爲MySqlConnections.

+0

感謝您的迴應,但似乎沒有幫助,仍然有同樣的錯誤。 – bella

+0

你的服務器在線嗎?你使用正確的IP嗎?它在標準端口上嗎? –

+0

是的,它應該在線,帶有GridVIew的頁面顯示來自數據庫的正確數據。對於IP,我嘗試了localhost和127.0.0.1。對於那個不太熟悉的港口,我該如何檢查? – bella