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)

      

+3


source to share


5 answers


It could be that your connection string is wrong / unavailable due to / etc passwords.



+1


source


  • Is a firewall blocking the connection?
  • Can you access the database with settings in a config file using SQL Server Management Studio?
  • Is the sql database server on the same host?
  • Can you show us more details by explicitly changing some parts (pass / user / host).


+1


source


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

+1


source


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" />

      

0


source


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

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

      

More: check this

0


source







All Articles