我有一个powershell脚本,我正在尝试建立与azure sql db的连接。以下列方式构建我的连接字符串时,SqlConnection对象的创建总是失败,并显示一条错误,指出连接字符串的格式不符合规范(变量cred的类型为" PSCredential")
$connString = “Server=$($serverName).database.windows.net;”
$connString = $connString + “Database=$($dbName);”
$connString = $connString + “Integrated Security=False;”
$connString = $connString + “User ID=$($cred.Username)@$($serverName);”
$connString = $connString + “Password=$($cred.GetNetworkCredential().Password);”
$connString = $connString + “Trusted_Connection=False;”
$connString = $connString + “Encrypt=True;”
$connString = $connString + “Connection Timeout=30;”
$SqlConnection = New-Object -TypeName System.Data.SqlClient.SqlConnection($connString)
任何人都知道这里出了什么问题?
答案 0 :(得分:1)
尝试以下方法:
$SQLServer = "aaaa.database.windows.net"
$SQLDBName = "Database"
$uid ="john"
$pwd = "pwd123"
$SqlQuery = "SELECT * from table;"
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server = $SQLServer; Database = $SQLDBName; Integrated Security = False; User ID = $uid; Password = $pwd;"
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = $SqlQuery
$SqlCmd.Connection = $SqlConnection
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd
$DataSet = New-Object System.Data.DataSet
$SqlAdapter.Fill($DataSet)
$DataSet.Tables[0] | out-file "C:\Scripts\xxxx.csv"