Rajiv Shah
Rajiv Shah

Reputation: 31

database connection in windows application

I am going through a windows application in C#.net using SQL Server 2008 as database server. The following is the code in my 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 that 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)

Upvotes: 3

Views: 176

Answers (5)

Lamloumi Afif
Lamloumi Afif

Reputation: 9081

The problem is in the connection string , I think you must set your account parameters (if you have) like this

Data Source=.\SQLEXPRESS;Initial Catalog=attendence;Integrated Security=SSPI;
User ID=myUsername;Password=myPassword;

For full details : check this

Upvotes: 0

Izzy
Izzy

Reputation: 6876

You want something like

string connStr = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;

Please refer to Store connection string in web.config I find the article very useful

Upvotes: 1

nire
nire

Reputation: 458

  • Is a firewall blocking the connection?
  • Can you access the database, with the settings in the configuration file, using SQL Server management studio?
  • Is the sql database server situated on the same host?
  • Can you show us more details, changing some parts obviously (pass/user/host)

Upvotes: 1

Kuriyama Mirai
Kuriyama Mirai

Reputation: 937

I got the same error before and what I do is :

How to resolve Error 26 in SQL Server?

I tried using the (IP address of the server)\SQLEXPRESS. Sometimes instance name is unrecognizable better use the IP address of your sql server.

or maybe you've got a wrong connection string. In your app.config check if the syntax is 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" />

Upvotes: 0

user3913686
user3913686

Reputation:

This could be that your connection string is wrong/inaccessible due to passwords/etc.

Upvotes: 1

Related Questions