使用Windows身份驗證連接到SQL服務器數據庫的正確語法是什麼?使用Windows身份驗證連接到SQL數據庫的Powershell腳本
0
A
回答
0
TechNet文章https://technet.microsoft.com/en-us/magazine/hh855069.aspx給出瞭如何使用PowerShell連接到SQL Server數據庫的一個很好的運行。它還包括你可以在腳本中使用的例子功能:
function Get-DatabaseData {
[CmdletBinding()]
param (
[string]$connectionString,
[string]$query,
[switch]$isSQLServer
)
if ($isSQLServer) {
Write-Verbose 'in SQL Server mode'
$connection = New-Object-TypeName System.Data.SqlClient.SqlConnection
} else {
Write-Verbose 'in OleDB mode'
$connection = New-Object-TypeName System.Data.OleDb.OleDbConnection
}
$connection.ConnectionString = $connectionString
$command = $connection.CreateCommand()
$command.CommandText = $query
if ($isSQLServer) {
$adapter = New-Object-TypeName System.Data.SqlClient.SqlDataAdapter $command
} else {
$adapter = New-Object-TypeName System.Data.OleDb.OleDbDataAdapter $command
}
$dataset = New-Object-TypeName System.Data.DataSet
$adapter.Fill($dataset)
$dataset.Tables[0]
}
function Invoke-DatabaseQuery {
[CmdletBinding()]
param (
[string]$connectionString,
[string]$query,
[switch]$isSQLServer
)
if ($isSQLServer) {
Write-Verbose 'in SQL Server mode'
$connection = New-Object-TypeName System.Data.SqlClient.SqlConnection
} else {
Write-Verbose 'in OleDB mode'
$connection = New-Object-TypeName System.Data.OleDb.OleDbConnection
}
$connection.ConnectionString = $connectionString
$command = $connection.CreateCommand()
$command.CommandText = $query
$connection.Open()
$command.ExecuteNonQuery()
$connection.close()
}
在從TechNet文章上面的腳本,你只需要提供3個參數:連接字符串(你會使用受信任的連接=真集成安全性),要運行的查詢和數據庫類型(SQL Server或OleDB)。
0
另外,您還可以恢復爲功能Invoke-Sqlcmd2,它可以爲您自動完成所有這些操作。我們非常成功地使用它,它使生活變得更加輕鬆。
相關問題
- 1. 如何通過PowerShell使用Windows身份驗證而非本地身份驗證連接到SQL?
- 2. ASP.NET - 使用傳遞身份驗證(Windows身份驗證)運行PowerShell腳本
- 3. 如何使用Windows身份驗證遠程連接到SQL Server數據庫?
- 4. 使用Windows身份驗證將Java連接到MS SQL Server數據庫
- 5. 使用Windows身份驗證從傳統ASP連接到SQL Server數據庫
- 6. 使用Python連接到SQL Server Express數據庫(Windows身份驗證)
- 7. 無法使用Windows身份驗證連接到SQL Server數據庫
- 8. 使用Python使用Windows身份驗證連接到MS SQL Server?
- 9. 使用Windows身份驗證連接到本地SQL Server數據庫的連接字符串
- 10. Perl DBI:連接到使用Windows身份驗證的SQL Server
- 11. 數據源連接「使用Windows身份驗證」不適用於XMLA腳本
- 12. Windows身份驗證到Oracle數據庫
- 13. 無法使用SQL Server身份驗證本地連接到SQL
- 14. 無法使用os身份驗證連接到oracle數據庫?
- 15. 如何使用os身份驗證連接到oracle數據庫?
- 16. 連接到SQL Server使用Windows身份驗證
- 17. 使用Windows身份驗證連接到不同域上的不同數據庫
- 18. 使用Windows身份驗證連接到使用JDBC中不同用戶帳戶的SQL Server身份驗證
- 19. 使用Windows身份驗證的SQL Server連接wso2 dss
- 20. PowerShell腳本和表單身份驗證
- 21. 通過使用Java提供憑據的Windows身份驗證連接到SQL Server
- 22. 通過使用Windows身份驗證ODBC連接連接到SQL Server從PHP
- 23. 如何使用Windows身份驗證指定Windows用戶連接到MS SQL Server數據庫
- 24. 無法使用SSMS通過Windows身份驗證連接到本地SQL Server
- 25. 使用Windows身份驗證與MS SQL無DSN連接
- 26. SQL服務器 - 連接使用Windows身份驗證
- 27. SQL Server Management Studio無法使用Windows身份驗證連接
- 28. ASP.NET Windows +數據庫身份驗證
- 29. 使用Windows身份驗證的SQL Server
回答此鏈接:[SO鏈接](http://stackoverflow.com/a/18872802/2628302) – objectNotFound
謝謝,它工作:) – Abhaya