我只想打电话给生成脚本的方法Microsoft.Data.Schema.ScriptDom.Sql.Sql100ScriptGenerator http://msdn.microsoft.com/en-us/library/dd195523.aspx来自 PowerShell。
#C
public void GenerateScript(
IScriptFragment scriptFragment,
out string script
)
I found this https://stackoverflow.com/questions/821744/how-to-call-method-with-output-parameters-in-powershell,但我无法让它发挥作用
$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
Edit:
当前版本是
$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
我正在尝试转换
IList<ParseError> errors;
using (StringReader sr = new StringReader(inputScript))
{
fragment = parser.Parse(sr, out errors);
}
Edit:
好的,这有效:
$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;