如何调用.NET方法的输出参数?参数、方法、NET

2023-09-05 23:15:41 作者:追逐我的明天

我只是想打电话给在 GenerateScript Microsoft.Data.Schema.ScriptDom.Sql.Sql100ScriptGenerator 从PowerShell的。

  #C

公共无效GenerateScript(
    IScriptFragment scriptFragment,
    出字符串脚本
)
 

我发现this,但我不明白它的工作

  $ SG =新的对象Microsoft.Data.Schema.ScriptDom.Sql.Sql100ScriptGenerator

$ SQL =SELECT * FROM PowerShell的'

$ OUT =''
$ sg.GenerateScript($ SQL,[参考] $出)

$出
 

这给了

 找不到该过载GenerateScript和参数数:2。
在行:6字符:19
+ $ sg.GenerateScript<<<< ($ SQL,[参考] $出)
    + CategoryInfo:NotSpecified:(:) [],MethodException
    + FullyQualifiedErrorId:MethodCountCouldNotFindBest
 
excel中如何调用公式中的某个参数

编辑:

现在的版本是

  $ SQL =SELECT * FROM PowerShell的'

$ SR =新对象System.IO.StringReader($ SQL)

$ SG =新的对象Microsoft.Data.Schema.ScriptDom.Sql.Sql100ScriptGenerator
$解析器=新的对象Microsoft.Data.Schema.ScriptDom.Sql.TSQL100parser($真)

$错误=''
$片段= $ parser.Parse($ SR([参考] $错误))

$ OUT =''
$ sg.GenerateScript($片段,([参考] [字符串] $出))

$出
 

不过,我在排队的错误

  $片段= $ parser.Parse($ SR([参考] $错误))



无法找到过载的解析,并参数计数:2。
在行:11字符:26
+ $片段= $ parser.Parse<<<< ($ SR([参考] $错误))
    + CategoryInfo:NotSpecified:(:) [],MethodException
    + FullyQualifiedErrorId:MethodCountCouldNotFindBest
 

我想转换

 的IList< ParseError>错误;

    用(StringReader SR =新StringReader(inputScript))
    {
        片段= parser.Parse(SR,出错误);
    }
 

编辑:

确定这样工作的:

  $ SQL = @
SELECT * FROM PowerShell的 - 评论
其中,psRefnr = 1
@
$选项=新的对象Microsoft.Data.Schema.ScriptDom.Sql.SqlScriptGeneratorOptions

$ SR =新对象System.IO.StringReader($ SQL)

$ SG =新的对象Microsoft.Data.Schema.ScriptDom.Sql.Sql100ScriptGenerator($选项)
$解析器=新的对象Microsoft.Data.Schema.ScriptDom.Sql.TSQL100parser($真)

$错误= $空
$片段= $ parser.Parse($ SR([参考] $错误))

从$ = $空
$ sg.GenerateScript($片段,([参考] $出))

$出
 

和生成(它消除按照预期的评论)

  SELECT *
从PowerShell的
WHERE psRefnr = 1;
 

解决方案

我相信你的问题是与你的第一个参数,它应该是一个IScriptFragment。你是传递一个字符串。

您将需要通过一些源自的 TSqlFragment 。使用类似 TSql100Parser.ParseStatementList 方法,你会得到碎片的列表。

编辑:这博客帖子也有类似的问题,你的第二个错误。

I just want to call the GenerateScript method of Microsoft.Data.Schema.ScriptDom.Sql.Sql100ScriptGenerator from PowerShell.

#C

public void GenerateScript(
    IScriptFragment scriptFragment,
    out string script
)

I found this, but I do not get it to work

$sg = new-object  Microsoft.Data.Schema.ScriptDom.Sql.Sql100ScriptGenerator

$sql = 'select * from PowerShell'

$out = ''
$sg.GenerateScript($sql, [ref] $out)

$out

this gives

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:

Current version is

$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

But I get an error in line

$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

I'm trying to convert

    IList<ParseError> errors;

    using (StringReader sr = new StringReader(inputScript))
    {
        fragment = parser.Parse(sr, out errors);
    }

Edit:

OK this works:

$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

and generates ( it removes the comment as intended )

SELECT *
FROM   PowerShell
WHERE  psRefnr = 1;

解决方案

I believe your issue is with your first parameter, which should be a IScriptFragment. You are passing a string.

You would need to pass something that derives from TSqlFragment. Using something like the TSql100Parser.ParseStatementList method, you will get a list of fragments.

EDIT: This blog post has a similar issue to your second error.