sqldataadapter 在 powershell 中不起作用
Posted
技术标签:
【中文标题】sqldataadapter 在 powershell 中不起作用【英文标题】:sqldataadapter is not working in powershell 【发布时间】:2020-01-06 15:21:00 【问题描述】:请找到向数据库发出选择查询的代码。 我正在将输出加载到不工作的表中 选择查询很好,因为我在数据库中运行相同的查询并获得 1 行作为输出。
$SqlQuery = "select InputFiles,OutputFiles from $mastTableNm where JobName = '$jobname'";
$sqloutqryreader = MsSqlQueryExecutor $SqlQuery $logfile 'SELECT'
Add-Content -Value "$TimeinSec Log: Reading data from sql reader" -Path $logfile
$mstrtab = new-object System.Data.DataTable
$mstrtab.Load($sqloutqryreader)
echo $mstrtab
ForEach($mstrjobrw in $mstrtab)
Add-Content -Value "$TimeinSec Log: Reading data from sql reader $mstrjobrw.InputFiles $mstrjobrw.OutputFiles " -Path $logfile
以下是执行查询并返回适配器的函数。
function Global:MsSqlQueryExecutor(
$SqlQuery,
$logfile,
$QueryType
)
$Global:sqloutput = $Null
try
# make sure that the output from the following code block does not pollute return value
$Null = @(
Add-Content -Value "$TimeinSec Log: Setting up the sql query to execute" -Path $logfile
$SQLUsername = "aa"
$SQLPassword = "aa"
$Database = "aa"
$SQLServer = "aa.aa.aa.windows.net"
# Define the connection to the SQL Database
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server=$SQLServer;Database=$Database;User ID=$SQLUsername;Password=$SQLPassword;Trusted_Connection=False;Encrypt=True;Connection Timeout=5000;"
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = $SqlQuery
$SqlCmd.Connection = $SqlConnection
$SqlCmd.CommandType = [System.Data.CommandType]::Text
$SqlCmd.CommandTimeout = 0
Add-Content -Value "$TimeinSec Log: Preparation to execute query: $SqlQuery is completed" -Path $logfile
if ($SqlConnection.State -eq [System.Data.ConnectionState]'Closed')
$SqlConnection.Open()
if ($QueryType -eq 'SELECT')
$adp = New-Object System.Data.SqlClient.SqlDataAdapter $SqlCmd
$dtst = New-Object System.Data.DataSet
$adp.Fill($dtst)
$Global:sqloutput = $dtst.Tables[0]
Add-Content -Value "$TimeinSec Log: Execution of the $QueryType query: $SqlQuery completed successfully." -Path $logfile
else
$Global:sqloutput = $SqlCmd.ExecuteReader()
Add-Content -Value "$TimeinSec Log: Execution of the $QueryType query: $SqlQuery completed successfully." -Path $logfile
)
return $Global:sqloutput
catch
Add-Content -Value "$TimeinSec Error: Failed to Query: $SqlQuery" -Path $logfile
Add-Content -Value $_.Exception.Message -Path $logfile
EXIT
finally
$SqlConnection.Close()
$SqlConnection.Dispose();
Add-Content -Value "$TimeinSec Cleanup: Connection is disposed" -Path $logfile
表格未加载。
我尝试了另一种解决方法,但也不起作用。
请找到该问题的链接
read() method inside sqldatareader in powershell is not working with while loop
【问题讨论】:
是否抛出异常?还是什么都不返回? @kundan 它没有抛出异常。它只是不返回任何东西 【参考方案1】:你能先用简单的代码 sn-p 用 ExecuteReader() 来填充数据表吗?查看基本查询是否返回任何数据,
$jobname = "<jobname>"
$mastTableNm = "<tablename>"
$sqlConn = New-Object System.Data.SqlClient.sqlConnection "<Your Connection String Here>";
$sqlConn.Open();
$sqlCommand = $sqlConn.CreateCommand();
$sqlCommand.CommandText = "select InputFiles,OutputFiles from $mastTableNm where JobName = '$jobname'";
Write-Host $sqlCommand.CommandText;
$result = $sqlCommand.ExecuteReader();
$dtTable = New-Object System.Data.DataTable;
$dtTable.Load($result);
【讨论】:
我发现了问题。问题在于退货。不知何故,一旦函数调用结束,函数返回的值就不会持续存在。解决方法是使用全局变量,并在函数调用结束后使用它。【参考方案2】:问题在于退货。 Powershell 有一些恼人的返回行为。我使用了一种解决方法来使我的代码正常工作。我使用全局变量而不是 return 来初始化数据表。然后在我需要的代码中访问这个全局变量。
function Global:MsSqlQueryExecutor(
$SqlQuery,
$logfile,
$QueryType
)
try
# make sure that the output from the following code block does not pollute return value
$Null = @(
Add-Content -Value "$TimeinSec Log: Setting up the sql query to execute" -Path $logfile
$SQLUsername = "aaa"
$SQLPassword = "aaa"
$Database = "aaa"
$SQLServer = "aaat"
# Define the connection to the SQL Database
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server=$SQLServer;Database=$Database;User ID=$SQLUsername;Password=$SQLPassword;Trusted_Connection=False;Encrypt=True;Connection Timeout=5000;"
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = $SqlQuery
$SqlCmd.Connection = $SqlConnection
$SqlCmd.CommandType = [System.Data.CommandType]::Text
$SqlCmd.CommandTimeout = 0
Add-Content -Value "$TimeinSec Log: Preparation to execute query: $SqlQuery is completed" -Path $logfile
if ($SqlConnection.State -eq [System.Data.ConnectionState]'Closed')
$SqlConnection.Open()
if ($QueryType -eq 'SELECT')
$adp = New-Object System.Data.SqlClient.SqlDataAdapter $SqlCmd
$dtst = New-Object System.Data.DataSet
$adp.Fill($dtst)
$Global:sqlexecoutput = $dtst.Tables[0]
Add-Content -Value "$TimeinSec Log: Execution of the $QueryType query: $SqlQuery completed successfully." -Path $logfile
else
$Global:sqlexecoutput = $SqlCmd.ExecuteReader()
Add-Content -Value "$TimeinSec Log: Execution of the $QueryType query: $SqlQuery completed successfully." -Path $logfile
)
catch
Add-Content -Value "$TimeinSec Error: Failed to Query: $SqlQuery" -Path $logfile
Add-Content -Value $_.Exception.Message -Path $logfile
EXIT
finally
$SqlConnection.Close()
$SqlConnection.Dispose();
Add-Content -Value "$TimeinSec Cleanup: Connection is disposed" -Path $logfile
【讨论】:
以上是关于sqldataadapter 在 powershell 中不起作用的主要内容,如果未能解决你的问题,请参考以下文章
如何在 where 子句上基于 SqlCommand 或 SqlDataAdapter 填充 datagridview? SqlDataAdapter 不适用于哪里?
使用带参数的 SqlDataAdapter.Update()