SQL Server connection string with "\" RRS feed

  • Question

  • Hi - I was wondering if you can help. I'm writing a script to pull data from several SQL Server instances and having issues with connection to a server with a backslash in the name.




    $connectionTemplate="Data Source={0};Integrated Security=SSPI;Initial Catalog={1};"

    $connectionString =[string]::Format($connectionTemplate,$server,$database)

    $connection =New-ObjectSystem.Data.SqlClient.SqlConnection

    $connection.ConnectionString =$connectionString

    $command =New-ObjectSystem.Data.SqlClient.SqlCommand

    $command.CommandText =$query

    $command.Connection =$connection


    $SqlAdapter.SelectCommand =$command




    The above shows the foreach - reading a file that contains the servers

    File looks like:



    servername\instancename, 6547

    I can process the first two without any issues and get the data but the last one is stumping me - wondering if you can help and tell me what I need to do please


    • Moved by Bill_Stewart Wednesday, September 4, 2019 5:54 PM Off-topic
    Wednesday, February 20, 2019 1:22 AM

All replies

  • pleas do not post colorized code in this forum.  It is unreadable on almost all browsers.  Use the code posting tool provided on the edit toolbar.


    Wednesday, February 20, 2019 4:32 AM