Я просто хочу вызвать метод GenerateScript из Microsoft.Data.Schema.ScriptDom.Sql.Sql100ScriptGenerator из PowerShell .
#C
public void GenerateScript(
IScriptFragment scriptFragment,
out string script
)
Я нашел это , но я не могу заставить его работать
$sg = new-object Microsoft.Data.Schema.ScriptDom.Sql.Sql100ScriptGenerator
$sql = 'select * from PowerShell'
$out = ''
$sg.GenerateScript($sql, [ref] $out)
$out
это дает
Cannot find an overload for "GenerateScript" and the argument count: "2".
At line:6 char:19
+ $sg.GenerateScript <<<< ($sql, [ref] $out)
+ CategoryInfo : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : MethodCountCouldNotFindBest
Изменить:
Текущая версия -
$sql = 'select * from PowerShell'
$sr = new-Object System.IO.StringReader($sql)
$sg = new-object Microsoft.Data.Schema.ScriptDom.Sql.Sql100ScriptGenerator
$parser = new-object Microsoft.Data.Schema.ScriptDom.Sql.TSQL100parser($true)
$errors = ''
$fragment = $parser.Parse($sr,([ref]$errors))
$out = ''
$sg.GenerateScript($fragment,([ref][string]$out))
$out
Но я получаю сообщение об ошибке в строке
$fragment = $parser.Parse($sr,([ref]$errors))
Cannot find an overload for "Parse" and the argument count: "2".
At line:11 char:26
+ $fragment = $parser.Parse <<<< ($sr,([ref]$errors))
+ CategoryInfo : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : MethodCountCouldNotFindBest
Я' m пытается преобразовать
IList errors;
using (StringReader sr = new StringReader(inputScript))
{
fragment = parser.Parse(sr, out errors);
}
Изменить:
ОК, это работает:
$sql = @'
select * from PowerShell -- a comment
where psRefnr = 1
'@
$options = new-object Microsoft.Data.Schema.ScriptDom.Sql.SqlScriptGeneratorOptions
$sr = new-Object System.IO.StringReader($sql)
$sg = new-object Microsoft.Data.Schema.ScriptDom.Sql.Sql100ScriptGenerator($options)
$parser = new-object Microsoft.Data.Schema.ScriptDom.Sql.TSQL100parser($true)
$errors = $null
$fragment = $parser.Parse($sr,([ref]$errors))
$out = $null
$sg.GenerateScript($fragment,([ref]$out))
$out
и генерирует (он удаляет комментарий, как предполагалось)
SELECT *
FROM PowerShell
WHERE psRefnr = 1;