SQL Network Interfaces, error: 25 - Connection string is not valid in Powershell

后端 未结 6 619
天命终不由人
天命终不由人 2021-01-12 10:36
$SQLconnection = New-Object System.Data.SqlClient.SqlConnection
$SQLconnection.ConnectionString = \"Server = MySQLservername\\MSSQLSERVER; Database = \"MYSQLDB\"; In         


        
6条回答
  •  迷失自我
    2021-01-12 11:20

    You're constructing your string incorrectly and I'm surprised this works at all for any version. You have "MYSQLDB" in the middle of the connection string and you didn't escape the double quotes in the quoted string. You can see this by running these two commands:

    $a = write-output "Server = MySQLservername\MSSQLSERVER; Database = "MYSQLDB"; Integrated Security = True" 
    $b = write-output "Server = MySQLservername\MSSQLSERVER; Database = `"MYSQLDB`"; Integrated Security = True"
    $a.gettype()
    $b.gettype()
    

    The first line returns an array instead of a string because the semi-colon is seen as running two commands

    Just remember to escape double quotes in double-quoted strings you'll be fine:

    $SQLconnection.ConnectionString = "Server = MySQLservername\MSSQLSERVER; Database = `"MYSQLDB`"; Integrated Security = True"
    

提交回复
热议问题