简体   繁体   English

使用Powershell从MSSQL数据库获取行数

[英]Get count of rows from MSSQL database with Powershell

I'm trying to get the number of rows from a Microsoft SQL database table in Powershell as an integer and am stuck peeling the figure out of the object Powershell is returning. 我试图从Powershell中的Microsoft SQL数据库表中获取整数作为行数,并且被卡住,将图从Powershell返回的对象中剥离。

I have; 我有;

$SQLServer = "MSSQL01"   $SQLDBName = "TheTable" $SqlQuery = "select count(*) from cases where opened_date =< '2014-07-30'"

$SqlConnection = New-Object System.Data.SqlClient.SqlConnection $SqlConnection.ConnectionString = "Server = $SQLServer; Database = $SQLDBName; Integrated Security = True"

$SqlCmd = New-Object System.Data.SqlClient.SqlCommand $SqlCmd.CommandText = $SqlQuery $SqlCmd.Connection = $SqlConnection

$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter $SqlAdapter.SelectCommand = $SqlCmd

$DataSet = New-Object System.Data.DataSet $SqlAdapter.Fill($DataSet)

$SqlConnection.Close()

Now $DataSet.Tables[0] in this instance returns an object like; 现在$ DataSet.Tables [0]在此实例中返回一个对象,例如:

Table Name
-----------

45

How can I grab just the 45 as an int? 我怎样才能仅将45作为整数?

Look at ExecuteScalar : 看一下ExecuteScalar

$SQLServer = "MSSQL01"   
$SQLDBName = "TheTable" 
$SqlQuery = "select count(*) from cases where opened_date =< '2014-07-30'"
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection 
$SqlConnection.ConnectionString = "Server = $SQLServer; Database = $SQLDBName; Integrated Security = True"
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand $SqlCmd.CommandText = $SqlQuery $SqlCmd.Connection = $SqlConnection
$SqlConnection.Open() 
$Rows= [Int32]$SqlCmd.ExecuteScalar()
$SqlConnection.Close()

Here's a version which provides two encapsulated functions, based on @jessehouwing's answer. 这是一个基于@jessehouwing答案的提供两个封装函数的版本。 This also handles an optional port, choice of TrustedAuth vs. User/Passwd Authentication, and an optional WHERE clause. 这还将处理可选端口,TrustedAuth与用户/密码验证的选择以及可选的WHERE子句。

function createSqlConnection($sqlServer, $sqlPort, $dbName, $user, $password) {
    $sqlConnection = New-Object System.Data.SqlClient.sqlConnection
    if ($sqlPort) { $sqlServer = "$sqlServer,$sqlPort" }
    if ($user -and $password) {
        $sqlConnection.ConnectionString = "Server=$sqlServer; Database=$dbName; User Id=$user; Password=$password"
    } else {
        $sqlConnection.ConnectionString = "Server=$sqlServer; Database=$dbName; Integrated Security=True"
    }

    return $sqlConnection
}

function getRowCount($sqlConnection, $table, $where = "1=1") {
    $sqlQuery = "SELECT count(*) FROM $table WHERE $where"
    $sqlConnection.Open()
    $SqlCmd = New-Object System.Data.SqlClient.SqlCommand($sqlQuery, $sqlConnection)
    $row_count = [Int32] $SqlCmd.ExecuteScalar()
    $sqlConnection.Close()
    return $row_count
}

To use it: 要使用它:

$dbConn = createSqlConnection 'db-server' $null 'myDatabase' $user $password
$count = getRowCount $dbConn 'the_table'

Using ExecuteScalar is a much better solution. 使用ExecuteScalar是更好的解决方案。 If, for some reason, you want to stick with the DataSet use either: 如果出于某种原因要坚持使用DataSet使用以下任一方法:

$DataSet.Tables[0].'Table Name'

or 要么

$DataSet.Tables[0] | Select-Object -ExpandProperty "Table Name"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM