Geek Logbook

Tech sea log book

Know your String Connection using SQL

I was looking how to know my server on the internet, and I’ve found this interesting question in Stackoverflow: How to get the connection String from a database. And one question give us an example of how do it using SQL:

select
    'data source=' + @@servername +
    ';initial catalog=' + db_name() +
    case type_desc
        when 'WINDOWS_LOGIN' 
            then ';trusted_connection=true'
        else
            ';user id=' + suser_name() + ';password=<<YourPassword>>'
    end
    as ConnectionString
from sys.server_principals
where name = suser_name()

And it Works properly:

Tags: