您现在的位置是:网站首页> 编程资料编程资料
Powershell访问SQL Server数据库代码实例_PowerShell_
2023-05-26
425人已围观
简介 Powershell访问SQL Server数据库代码实例_PowerShell_
支持所有版本的SQLserver。
你是否需要连接数据库?这里有一段代码演示如何查询和获取SQL数据,只需非常简单正确的配置你的账户信息、服务器地址及SQL语句就行:
复制代码 代码如下:
$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
您可能感兴趣的文章:
相关内容
- Powershell读取PFX证书并输入密码的脚本分享_PowerShell_
- Powershell改变脚本执行优先权的代码分享_PowerShell_
- Powershell实现导入安装证书功能脚本分享_PowerShell_
- Windows Powershell 定义函数_PowerShell_
- Windows Powershell Switch 循环_PowerShell_
- Windows Powershell For 循环_PowerShell_
- Powershell小技巧之使用WS-Man来调用PowerShell命令_PowerShell_
- 使用HTTP api简单的远程执行PowerShell脚本_PowerShell_
- Powershell小技巧之开启关闭远程连接_PowerShell_
- PowerShell实现在控制台中插入绿色的打勾符号_PowerShell_
