2011-05-02 58 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