I am calling a function to query an SQL table. I only need the results of one cell. I am unable to successfully retrieve the cell data into a variable from the function.
我正在调用查询SQL表的函数。我只需要一个细胞的结果。我无法成功地将单元格数据检索到函数中的变量中。
For example, If I had a table with the following:
例如,如果我有一个包含以下内容的表:
FeedID Name Address
15 Bill Jones
I would need to capture the FeedID value of '15' into a variable. My SQL statement is only capturing the FeedID but I don't know how to extract the value
我需要将FeedID值'15'捕获到变量中。我的SQL语句只捕获FeedID,但我不知道如何提取值
Here is what I have so far:
这是我到目前为止:
function Invoke-SQL {
param(
[string] $dataSource = "10.0.100.1",
[string] $database = "Database123",
[string] $sqlCommand = $("SELECT [FeedID] FROM [dbo].[FeedList] WHERE [FeedFileName] = 'filename.txt'")
)
$connectionString = "Data Source=$dataSource; " + "Integrated Security=SSPI; " + "Initial Catalog=$database"
$connection = new-object system.data.SqlClient.SQLConnection($connectionString)
$command = new-object system.data.sqlclient.sqlcommand($sqlCommand,$connection)
$connection.Open()
$adapter = New-Object System.Data.sqlclient.sqlDataAdapter $command
$dataset = New-Object System.Data.DataSet
write-output $adapter.Fill($dataSet) | Out-Null
$connection.Close()
$dataSet.Tables
}
$FeedID = Invoke-SQL
$FeedID
2 个解决方案
#1
12
Alternatively you could use the following code, if you are looking for simple return values rather than tables for processing later.
或者,如果您要查找简单的返回值而不是表以便稍后处理,则可以使用以下代码。
[string] $Server= "10.0.100.1",
[string] $Database = "Database123",
[string] $SQLQuery= $("SELECT [FeedID] FROM [dbo].[FeedList] WHERE [FeedFileName] = 'filename.txt'")
function GenericSqlQuery ($Server, $Database, $SQLQuery) {
$Connection = New-Object System.Data.SQLClient.SQLConnection
$Connection.ConnectionString = "server='$Server';database='$Database';trusted_connection=true;"
$Connection.Open()
$Command = New-Object System.Data.SQLClient.SQLCommand
$Command.Connection = $Connection
$Command.CommandText = $SQLQuery
$Reader = $Command.ExecuteReader()
while ($Reader.Read()) {
$Reader.GetValue($1)
}
$Connection.Close()
}
#2
3
It looks like $FeedID (the variable you put the SQL output in) should have a FeedID property (from the single row returned).
看起来$ FeedID(您输入SQL输出的变量)应该具有FeedID属性(从返回的单行)。
Try this:
$FeedID.FeedID
#1
12
Alternatively you could use the following code, if you are looking for simple return values rather than tables for processing later.
或者,如果您要查找简单的返回值而不是表以便稍后处理,则可以使用以下代码。
[string] $Server= "10.0.100.1",
[string] $Database = "Database123",
[string] $SQLQuery= $("SELECT [FeedID] FROM [dbo].[FeedList] WHERE [FeedFileName] = 'filename.txt'")
function GenericSqlQuery ($Server, $Database, $SQLQuery) {
$Connection = New-Object System.Data.SQLClient.SQLConnection
$Connection.ConnectionString = "server='$Server';database='$Database';trusted_connection=true;"
$Connection.Open()
$Command = New-Object System.Data.SQLClient.SQLCommand
$Command.Connection = $Connection
$Command.CommandText = $SQLQuery
$Reader = $Command.ExecuteReader()
while ($Reader.Read()) {
$Reader.GetValue($1)
}
$Connection.Close()
}
#2
3
It looks like $FeedID (the variable you put the SQL output in) should have a FeedID property (from the single row returned).
看起来$ FeedID(您输入SQL输出的变量)应该具有FeedID属性(从返回的单行)。
Try this:
$FeedID.FeedID