Working on writing a C# application which will query a SQL Server 2008 R2 database instance. My code (with mods to the connection string) is as follows:
/// Interaction logic for MainWindow.xaml
public partial class MainWindow : Window
// Establish a connection string
string connectionString = "Data Source=IPADDRESS;Password=PASSWORD;User ID=USERID;Initial Catalog=dbName";
dbConnection = new SqlConnection(connectionString);
private void Test()
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)"}
The error you are having is not related to your code, it is caused from the parameters you have in the connection string. It cannot find the Server.
To connect to SQL Server sometimes is not enough to specify the IP address or server name only, you need to specify the port if it is not the default one (usually 1433) and the instance name. For example, using SQL Server Express by default it creates the instance named "SQLEXPRESS", so to be able to connect to it you need to include it in the server name/IP address:
string connectionString = "Data Source=IpAddress\InstanceName,Port; Password=PWD; User ID=USERID; Initial Catalog=dbName";
Take a look here: SQL Server 2008 ConnectionStrings it is all explained there. Hope this helps!