Salman Mushtaq

Salman Mushtaq

  • NA
  • 284
  • 219.4k

How to get @@SERVERNAME and use in app.config file C#

Oct 25 2018 9:18 AM
Hi All,
 
In my winform application I use Linq to Sql.
 
My app.config file is following code
 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <configuration>  
  3.     <configSections>  
  4.     </configSections>  
  5.     <connectionStrings>  
  6.         <add name="SmartexFxDashboard.Properties.Settings.smartex_fx_reportsConnectionString" connectionString="Data Source=(local)\SQLEXPRESS;Initial Catalog=smartex_fx_reports;Integrated Security=True" providerName="System.Data.SqlClient"/>  
  7.     </connectionStrings>  
  8.     <startup>   
  9.         <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1"/>  
  10.     </startup>  
  11. </configuration>  
In Data Source I need to add 2 things
 
1. Server name
2. Instance name
 
Now if I have default instance I need to write
 
(local)
 
And in case I have names instance e.g. SQLEXPRESS I need to write
 
(local)/SQLEXPRESS
 
But I didn't know in client computer he have which instance. So, I need to know the instance name and save in key and instead of SQLEXPRESS I use that key.
 
In MS SQL SERVER 2008 if I execute query 
 
  1. select @@SERVERNAME  
  2. select @@SERVICENAME  
First one gives me my laptop name / instance name and second one give me only instance name 
 
So, I need your expertise how I get this instance name in app.config file and save in key and use it. So it's not hard coded and work for all.
 
Advance thanks and looking forward to your answers. 

Answers (6)