Skip to content

Error Connecting to SQL Server. Why?

Raja Khoury

Asked Jun 21, 2012, 6:29 AM EDT7 answers
Share
LinkedInX
Hi there,

I have been facing an error connecting to SQL server, I setup a firewall exception for both SQL server and Visual studio. 

I first created the app.config file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="myConnection" connectionString="Data Source=RAJA;Initial Catalog=strong; Integrated Security=true" providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>

And this is the code in the windows app form:

TypeScript
    private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                SqlConnection myConnection = new SqlConnection("Data Source=RAJA;Initial Catalog=strong;Integrated Security=True; Connection Timeout=10;");
                SqlCommand cmd = new SqlCommand();
                Object returnValue = null;
                cmd.CommandText = "SELECT COUNT(*) FROM Dictionnary";
                cmd.Connection = myConnection;
                myConnection.Open();
                returnValue = cmd.ExecuteScalar();
                myConnection.Close();
                textBox2.Text = returnValue.ToString();
                textBox2.Text = "hello";
            }
            catch (Exception Ex)
            {
                textBox2.Text = Ex.ToString();


            }


ERROR MESSAGE:
System.Data.SqlClient.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)
   




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 strong.Form1.button1_Click(Object sender, EventArgs e)




Why I am not able to connect to SQL server? the server is running I checked it the configuration manager and I added the firewall exceptions. Did I forget anything? Thanks

7 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    Are you able to connect the sql server database via sql server management studio ?
    What is the authentication type, you've used to connect sql server management studio ?

    Can you please post sql server management studio login screen  here ?
    0
  • 0
    Dear Muralidharan,

    Yes I guess I can connect as the table appears in the server explorer. Please find attached a zip files with the images requested.

    Thanks,
    Raja
    0
  • 0
    Hi Suthish, Yes I have read your article before the attempts. Thanks for posting it. I have attached some images that might help. Raja,
    0
  • 0
    Your connection is wrong.
    In the sql server management studio, server name : Raja\Raja where in the c#code or in the config file it "Raja". Change the data soruce, Raja to Raja\Raja.

    Hope this resolves your issue.
    0
  • 0
    I tried that and they tell me unrecognized escape sequence (for the \)
    0
  • 0
    Add "@" before the connectionstring.

    SqlConnection myConnection = new SqlConnection(@"Data Source=RAJA\raja;Initial Catalog=strong;Integrated Security=True; Connection Timeout=10;");
    0