Powershell訪問SQL Server數(shù)據(jù)庫代碼實例
更新時間:2014年11月07日 09:28:09 投稿:junjie
這篇文章主要介紹了Powershell訪問SQL Server數(shù)據(jù)庫代碼實例,本文直接給出代碼,使用時只需要替換數(shù)據(jù)庫配置參數(shù)即可,需要的朋友可以參考下
支持所有版本的SQLserver。
你是否需要連接數(shù)據(jù)庫?這里有一段代碼演示如何查詢和獲取SQL數(shù)據(jù),只需非常簡單正確的配置你的賬戶信息、服務(wù)器地址及SQL語句就行:
復(fù)制代碼 代碼如下:
$Database = 'Name_Of_SQLDatabase'
$Server = '192.168.100.200'
$UserName = 'DatabaseUserName'
$Password = 'SecretPassword'
$SqlQuery = 'Select * FROM TestTable'
# Accessing Data Base
$SqlConnection = New-Object -TypeName System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Data Source=$Server;Initial Catalog=$Database;user id=$UserName;pwd=$Password"
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = $SqlQuery
$SqlCmd.Connection = $SqlConnection
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd
$set = New-Object data.dataset
# Filling Dataset
$SqlAdapter.Fill($set)
# Consuming Data
$Path = "$env:temp\report.hta"
$set.Tables[0] | ConvertTo-Html | Out-File -FilePath $Path
Invoke-Item -Path $Path
相關(guān)文章
powershell網(wǎng)絡(luò)蜘蛛解決亂碼問題
這篇文章主要介紹了powershell網(wǎng)絡(luò)蜘蛛解決亂碼問題,需要的朋友可以參考下2017-10-10
PowerShell使用枚舉變量定義帶智能提示功能的函數(shù)參數(shù)
這篇文章主要介紹了PowerShell使用枚舉變量定義帶智能提示功能的函數(shù)參數(shù),但定義后只在ISE當中有效,需要的朋友可以參考下2014-07-07
PowerShell中使用replace操作符替換字符串實例
這篇文章主要介紹了PowerShell中使用replace操作符與替換字符串實例,著重介紹了replace的語法,需要的朋友可以參考下2014-07-07
Windows Powershell Foreach 循環(huán)
Foreach-object 為cmdlet命令,使用在管道中,對管道結(jié)果逐個處理,foreach為遍歷集合的關(guān)鍵字。2014-10-10

