How to get server name through code if SQL Server (Standard Edition) is installed.
We pass the server name while creating a connection string to connect SQL Server. Can we retrieve this value through code?
string sqlConnectionString = string.Format(
"user id={0};password={1};server={2};Trusted_Connection=no;database=TestDB;
connection timeout={3}",
dirDBinfo.UserName, dirDBinfo.Password, "ServerName", dirDBinfo.TimeOut);
I’m not sure I understand what you want.
If you already have a connection string, and you are trying to extract the server name from it for use elsewhere, you can reverse-engineer it like so:
If you are constructing your connection string for the first time, then:
(local)for the server name. If the SQL Server has an instance name, then specify it like this:(local)\myinstancename.