Connecting to Database in Windows Application
I am browsing a Windows application in C # .net using SQL Server 2008 as my database server. Below is the code in the app.config file:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="myconnection" value="Data Source=ritesh-pc\SQLEXPRESS;Initial Catalog=dbname;Integrated Security=SSPI"/>
</appSettings>
</configuration>
Whenever I try to access this key
"myconnection" as string connectionString = ConfigurationSettings.AppSettings["myconnection"];
the following error occurs
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: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)
source to share
You want something like
string connStr = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
Refer to Save your connection string in web.config I find the article very helpful
source to share
I got the same error before and what I am doing:
How do I resolve error 26 in SQL Server?
I tried to use (Server IP) \ SQLEXPRESS. Sometimes the instance name is unrecognizable it is better to use the IP address of your SQL server.
or perhaps you have the wrong connection string. In your app.config, check how the syntax looks like this:
<add name="conString"
connectionString="Data Source=10.99.89.80;Initial Catalog=EWB_FileDownloader;User ID = sa; Password = 12435@"
providerName="System.Data.SqlClient" />
source to share