1

I want to get the connection string so I can use it in my c# app. I checked out other threads in here, but they were old and toolbar sections were different.

Any help would be appreciated.

jarlh
  • 42,561
  • 8
  • 45
  • 63
EL02
  • 282
  • 2
  • 9
  • https://stackoverflow.com/questions/10479763/how-to-get-the-connection-string-from-a-database – Keyvan Soleimani Jan 09 '22 at 12:44
  • 3
    For what data access technology do you need your connection string? For the standard ADO.NET stack? OleDB? ODBC? Something else entirely? Check out https://www.connectionstrings.com/sql-server/ – marc_s Jan 09 '22 at 12:56

1 Answers1

6
SELECT
    'data source=' + @@SERVERNAME +
    ';initial catalog=' + DB_NAME() +
    CASE type_desc
        WHEN 'WINDOWS_LOGIN' 
            THEN ';trusted_connection=true'
        ELSE
            ';user id=' + SUSER_NAME()
    END
FROM sys.server_principals
WHERE name = suser_name()
AlinPaul8806
  • 327
  • 2
  • 15
  • Someone down voted this answer, idk why :) Thanks to this script I figured out that my db name has a space as 1st character. Even though I added a space character to my connection string, .net does not add a space to db name. So I surrounded the db name by ' single quotes – Ahmet Remzi EKMEKCI Mar 10 '23 at 09:36