Failure to connect my web page to a database

UPDATE: I missed that I should use using MySql.Data.MySqlClient reference and MySqlConnection since I have been trying to connect a mysql server. Java mislead me I could connect easily with two lines. sorry for taking time**


I'm trying to connect my web page to a database. I can connect easily using java in order to generate tables but I cannot connect with C# / Asp.

The error on the output screen is:

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

Code:

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);
}

Error Message:

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, TimeoutTim er 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:VisualStudioProjectsDataDeneme1DataDeneme1carSelect.aspx.cs:line 38


Your connection string isn't valid. Either use a standard .net provider connection string. Or specify the provider. See here for examples

http://www.connectionstrings.com/sql-server-2008

http://www.connectionstrings.com/sql-server-2005


The server was not found or was not accessible.

This could be caused by a number of different reasons, all of which cannot be diagnosed from our end.

You need to spin up Profiler (you may have to grab the express version if you don't have the full version of Sql Server) and watch the connection.

You will find that

a) No connection attempt is visible
Which means your connection string is invalid (see Vadim's answer for good links re connex strings), or that the sql server is not watching for connections over TCP/IP.

or, it may mean that

b) The connection was rejected for security reasons
which means you have to configure security on the server.


You cannot get a connection to your database. You need to look at how your connection string is defined (or post it if you are lost)

Also if you are using multiple Web.config files, make sure that all connection strings are correct, or that your running the Web.config file with the right connection string.

链接地址: http://www.djcxy.com/p/56728.html

上一篇: System.ComponentModel.Win32Exception:未找到网络路径错误

下一篇: 未能将我的网页连接到数据库