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.

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.

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

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.

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.

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.
- How To Check Datatype Of Column In SQL Server
- How To Enable Query Store In SQL Server
- How To Check Database Encryption In SQL Server
- How To View Database Diagram In SQL Server
- How To Create A New Database In Microsoft Sql Server Management Studio

Rajkishore Sahoo is a Microsoft Azure consultant with more than 14 years of hands-on experience building cloud-native data solutions across Azure and AWS, focused on Azure SQL Database, Azure SQL Managed Instance, and the pipelines that connect them to production apps. Day to day, he provisions Azure SQL instances, configures firewalls and connection strings, tunes queries for cloud workloads, and wires databases into Azure Functions and Logic Apps. Read more