Мне нужно обновить таблицу с помощью текстового файла. В настоящее время мой код работает нормально, если я выполняю Get-Content
из txt-файла, а затем запускаю запрос SQL-запроса, но только в случае небольших данных. Если размер текста слишком длинный или содержит специальные символы, он выдает ошибку следующим образом:SQL-запрос для массового обновления
Exception calling "ExecuteReader" with "0" argument(s): "Incorrect syntax near ')</td><td style=\"border:1px solid #cccccc\">#fieldValueEmpty($issue.getCustom FieldValue($componentTypeCf),'." At C:\Users\d-mansings\Desktop\Scripted Field Configuration\Script\Prod_UpdateS cript.ps1:78 char:37 + $Reader = $Command.ExecuteReader <<<<() + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : DotNetMethodException
Ниже приводится код, я использую:
Function DatabaseQueries(){
#To connect to the SQL database
$Connection = New-Object System.Data.SQLClient.SQLConnection
$Connection.ConnectionString = "Server=$IPSource ; Database=$DBNameSource ; User ID=$UserIDSource ; Password=$LoginPwdSource;"
$Connection.Open()
#Query to get the ID of the stored script field from propertyentry
$Command1 = New-Object System.Data.SQLClient.SQLCommand
$Command1.Connection = $Connection
$Command1.CommandText = "SELECT [ID] FROM [dbo].[propertyentry] WHERE [PROPERTY_KEY]='com.onresolve.jira.groovy.groovyrunner:customfields' "
$Reader = $Command1.ExecuteReader()
while ($Reader.Read()) {
$ID = $Reader.GetValue($1)
}
#To get the updated script file
$ScriptDir = $ParentDir + '\Script.txt'
$ScriptData = Get-Content "$ScriptDir"
$Connection.Close()
#Query to update the Script in JIRA database
$Connection.Open()
$Command = New-Object System.Data.SQLClient.SQLCommand
$Command.Connection = $Connection
$Command.CommandText = @"
Update [dbo].[propertytext] set [propertyvalue] ='$ScriptData' Where ID=$ID
"@
$Reader = $Command.ExecuteReader()
$Connection.Close()
}
'MySQL' или' SQL-SERVER'? –
Что такое содержимое 'script.txt'? –
Похоже, вы активировали уязвимость [SQL injection] (https://powershellstation.com/2009/09/15/executing-sql-the-right-way-in-powershell/) в своем коде. –