none
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.

    foreach($server

    in(get-contentE:\Powershell_scripts\ServerList_wrong1.txt))

    {

    $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=New-ObjectSystem.Data.SqlClient.SqlDataAdapter

    $SqlAdapter.SelectCommand =$command

    $DataSet=New-ObjectSystem.Data.DataSet

    $SqlAdapter.Fill($DataSet)

    $connection.Close()

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

    File looks like:

    servername1

    servername2,6123

    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

    thanks

    • 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