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:
And this is the code in the windows app form:
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
Posted Jun 22, 2012, 6:33 AM
SqlConnection myConnection = new SqlConnection(@"Data Source=RAJA\raja;Initial Catalog=strong;Integrated Security=True; Connection Timeout=10;");
Raja KhouryPosted Jun 22, 2012, 3:32 AM
Posted Jun 22, 2012, 3:13 AM
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.
Raja KhouryPosted Jun 22, 2012, 2:47 AM
Raja KhouryPosted Jun 22, 2012, 2:45 AM
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
Suthish NairPosted Jun 21, 2012, 1:35 PM
refer article: How to: Generate or find Connection String from Visual Studio
Posted Jun 21, 2012, 12:22 PM
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 ?