2011-05-02 57 views
0

更新:我錯過了我應該使用MySql.Data.MySqlClient引用和MySqlConnection,因爲我一直在嘗試連接一個mysql服務器。 Java誤導我,我可以輕鬆地連接兩條線。抱歉花時間**未能將我的網頁連接到數據庫


我試圖將我的網頁連接到數據庫。爲了生成表格,我可以使用java輕鬆連接,但無法使用C#/ Asp連接。

輸出屏幕上的錯誤是:

A first chance exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll

代碼:

try { 
    con = new SqlConnection("Data Source = urlhere ;" + 
          "uid = u_yucel;" + 
          "pwd = *****; " + 
          "database = u_yucel; " + 
          "connection timeout = 2"); 
    cmd.CommandText = "select * from person"; 
    con.Open(); 
    cmd.Connection = con; 

    DropDownList1.DataSource = cmd.ExecuteReader(); 
    DropDownList1.DataTextField = "name"; 
    DropDownList1.DataBind(); 
} 
catch (Exception ex) 
{ 
    Response.Write(ex.Message); 
    Console.WriteLine("123"); 
    Response.Write(ex.StackTrace); 
} 

錯誤消息:

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) at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning() at System.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo, SqlInternalConnectionTds connHandler, Boolean ignoreSniOpenTimeout, Int64 timerExpire, Boolean encrypt, Boolean trustServerCert, Boolean integratedSecurity) at System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, Boolean ignoreSniOpenTimeout, TimeoutTimer timeout, SqlConnection owningObject) at System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, Boolean redirectedUserInstance, SqlConnection owningObject, SqlConnectionString connectionOptions, TimeoutTimer timeout) at System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnection owningObject, TimeoutTimer timeout, SqlConnectionString connectionOptions, String newPassword, Boolean redirectedUserInstance) at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, Object providerInfo, String newPassword, SqlConnection owningObject, Boolean redirectedUserInstance) at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection) at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnection owningConnection, DbConnectionPool pool, DbConnectionOptions options) at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject) at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject) at System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection owningObject) at System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection) at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory) at System.Data.SqlClient.SqlConnection.Open() at DataDeneme1.carSelect.rebind() in E:\VisualStudioProjects\DataDeneme1\DataDeneme1\carSelect.aspx.cs:line 38

+5

你可以請發佈你正在嘗試使用的連接字符串和一些代碼可能嗎?這非常含糊。謝謝 – Pepe 2011-05-02 19:18:28

+1

對於異常,消息和堆棧跟蹤是什麼?並且,發佈您在該跟蹤中引用的任何代碼。 – 2011-05-02 19:19:26

+2

而實際的異常細節(至少是消息,也許是堆棧跟蹤) – jeroenh 2011-05-02 19:19:44

回答

1

你不能讓你的數據庫的連接。您需要查看您的連接字符串是如何定義的(或者如果您丟失,請發佈它)

另外如果您使用多個Web.config文件,請確保所有連接字符串都正確,或者您運行的Web .config文件與正確的連接字符串。

4

The server was not found or was not accessible.

這可能是由許多不同的原因造成的,所有這些原因都不能從我們的最終診斷出來。

您需要啓動Profiler(如果您沒有Sql Server的完整版本,您可能需要獲取express version)並觀察連接。

你會發現,

一)無連接嘗試可見
這意味着你的連接字符串是無效的(見瓦迪姆的答案良好的聯繫,重新CONNEX字符串),或在SQL Server是not watching for connections over TCP/IP

或者,這可能意味着

二)的連接被拒絕出於安全原因
這意味着你必須configure security on the server