ber*_*d_k 9 .net powershell out
我只想从PowerShell 调用Microsoft.Data.Schema.ScriptDom.Sql.Sql100ScriptGenerator的GenerateScript方法.
#C
public void GenerateScript(
IScriptFragment scriptFragment,
out string script
)
Run Code Online (Sandbox Code Playgroud)
我找到了这个,但我没有得到它的工作
$sg = new-object Microsoft.Data.Schema.ScriptDom.Sql.Sql100ScriptGenerator
$sql = 'select * from PowerShell'
$out = ''
$sg.GenerateScript($sql, [ref] $out)
$out
Run Code Online (Sandbox Code Playgroud)
这给了
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
Run Code Online (Sandbox Code Playgroud)
编辑:
目前的版本是
$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
Run Code Online (Sandbox Code Playgroud)
但我得到一个错误
$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
Run Code Online (Sandbox Code Playgroud)
我正在尝试转换
IList<ParseError> errors;
using (StringReader sr = new StringReader(inputScript))
{
fragment = parser.Parse(sr, out errors);
}
Run Code Online (Sandbox Code Playgroud)
编辑:
好的,这有效:
$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
Run Code Online (Sandbox Code Playgroud)
并生成(它按预期删除注释)
SELECT *
FROM PowerShell
WHERE psRefnr = 1;
Run Code Online (Sandbox Code Playgroud)
我相信您的问题在于第一个参数,它应该是 IScriptFragment。您正在传递一个字符串。
您需要传递从TSqlFragment派生的内容。使用类似TSql100Parser.ParseStatementList方法,您将获得片段列表。
编辑:这篇博文与您的第二个错误有类似的问题。