使用 Powershell 从 sql 表中获取值 [英] Using Powershell get Values from sql table

查看:42
本文介绍了使用 Powershell 从 sql 表中获取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们知道我们可以使用 sql 查询窗口从数据库中获取值,例如select * from....".有没有办法通过powershell方式获得价值.我找到了数据库表本身的方法,但没有找到值..

we know that we can use sql query window to get values from Database like "select * from....". Is there any way to get a value through powershell way. I found the way to database table itself but not to the values..

例如:

Set-location SQLserver:\sql\default\localhost\databases\database\tablesGet-childitem 

上面的命令为我提供了数据库中的表,但我如何从中获取值.

Above command gives me tables in the DB , but how can i get the values from it.

推荐答案

SQL Server Powershell 提供程序使用 SMO 来公开数据库对象层次结构.一旦你得到一个 SMO 子项,你就可以在该对象上调用相应的 SMO 方法.

The SQL Server Powershell provider uses SMO to expose the database object hierarchy. Once you get a SMO child item, you can invoke the corresponding SMO methods on the object.

SMO 数据库的 ExecuteQueryWithResults 方法可用于在给定数据库的上下文中执行查询.获取所需的数据库项并调用此方法以返回包含结果的 DataSet 对象.然后根据需要从 DataSet 中检索数据.

The SMO Database ExecuteQueryWithResults method can be used to execute a query in the context of a given database. Get the desired database item and invoke this method to return a DataSet object containing the results. Then retrieve the data as desired from the DataSet.

以下是从 SMO 参考中收集的示例 ((https://msdn.microsoft.com/en-us/library/ms205775.aspx),可以从数据库节点的上下文中运行:

Below is an example gleaned from the SMO reference ((https://msdn.microsoft.com/en-us/library/ms205775.aspx) that can be run from the context of the Databases node:

$db = Get-Item SomeDatabase
$dt = $db.ExecuteWithResults("SELECT * FROM sys.objects;")
$dt.Tables[0] | Format-Table

这篇关于使用 Powershell 从 sql 表中获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆