How To Get The Connection String From SQL Server

Recently, I got a requirement to work with a C# application where we were trying to call a stored procedure from that application where we required the connection string of my SQL server database. In this article, I will walk you through multiple ways to retrieve the connection string from SQL Server.

How To Get The Connection String From SQL Server

Let us discuss all the approaches individually.

Approach-1 Using SQL Query

We can execute the below SQL query to retrieve the connection string details.

SELECT 
    'Data Source=' + @@SERVERNAME + 
    ';Initial Catalog=' + DB_NAME() + 
    ';Integrated Security=True;' AS ConnectionString

After executing the above query, I got the expected output as shown in the below screenshot.

How To Get The Connection String From SQL Server

Check out How To Find Database Name In SQL Server

Approach-2 Using SQL Server Management Studio (SSMS)

To get the connection string from SQL server, follow the below steps

1. Open SQL Server Management Studio and connect to your SQL Server database instance.

2. Righ-click on the Object explorer and select the Properties option as shown below.

how to find connection string sql server

3. On the Server Properties window, click on the View Connection Properties link, as shown in the screenshot below.

sql server management studio get connection string

Approach-3 Using sys.databases

We can query the system view to get sql server connection string. We can just execute the SQL query below.

SELECT 'Data Source=' + @@SERVERNAME + 
       ';Initial Catalog=' + name + 
       ';Integrated Security=True;' AS ConnectionString
FROM sys.databases
WHERE database_id > 4

After executing the above query, I got the expected output below.

how to find the connection string of a sql server

Check out How To Check DB Size In SQL Server

Approach-4 Using sys.server_principals

We can also query the sys.server_principals to get connection string in sql server Management Studio 2019. You can check out the screenshot below, where we executed the the below query and got the expected output.

how to get connection string in sql server management studio 2019

Conclusion

Getting the connection string from SQL server is so easy using SQL query or SQL Server Management Studio, or quering sys.databases and sys.server_principals as explained in this article. Now it is your choice to choose the best methods that suit your requirements.

You may also like following the articles below.

Top 200 SQL Server Interview Questions and Answers

Free PDF On Top 200 SQL Server Interview Questions And Answers

Download A 40 pages PDF And Learn Now.