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);
In the Server name box, type the name of the instance of the Database Engine. For the default instance of SQL Server, the server name is the computer name. For a named instance of SQL Server, the server name is the <computer_name>\<instance_name>, such as ACCTG_SRVR\SQLEXPRESS.
In Microsoft SQL Server Management Studio, in the Object Explorer pane, right click the server and select properties. In the pane, there should be a heading called "Connection" and in that heading a link to a new window called "View connection properties". The value next to "Server name" is the name of your server.
Click Start, point to All Programs, point to Microsoft SQL Server, point to Configuration Tools, and then click SQL Server Configuration Manager. If you do not have these entries on the Start menu, SQL Server is not correctly installed.
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:
var parser = new SqlConnectionStringBuilder(connectionString);
var serverName = parser.DataSource;
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
.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With