我正在尝试使用 PowerShell 从 SQL 数据库中提取数据。
这是我想要做的 SQL
使用数据库文件
选择 *
FROM 表.marker
WHERE 表.marker = 0
ORDER BY table.sessionid
如何将其放入 PowerShell 中?
我对 PowerShell 的理解水平还不错,但这超出了我当前的知识范围,我只是没有时间资源来用老式的方法来解决它。
最佳答案
首先,您的 powershell 脚本中需要一个连接字符串,例如:
$connectionstring= "SERVER=servername; database=databasename; user id=username;password=password
然后您需要将查询附加到类似以下内容的字符串:
$myQuery =
@"
USE dbfile
SELECT *
FROM table.marker
WHERE table.marker = 0
ORDER BY table.sessionid
"@
然后,您需要使用查询打开 SQL 连接,使用数据集创建 SQL 适配器 - 类似于:
$connection = New-Object System.Data.SqlClient.SqlConnection
$connection.ConnectionString = $connectionstring
$command = New-Object System.Data.SqlClient.SqlCommand
$command.CommandText = $myQuery
$command.Connection = $connection
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $command
$myDataSet = New-Object System.Data.DataSet
$SqlAdapter.Fill($myDataSet)
然后您可以在任何地方使用
调用数据集$myDataset.Tables[0].rows (etc)...
关于mysql - PowerShell转SQL,简单查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47080377/