简体   繁体   English

Powershell脚本中的C#DataTable

[英]C# DataTable within Powershell Script

I am able to get a C# code sample to run within a Powershell v2.0 script as follows: 我能够获得在Powershell v2.0脚本中运行的C#代码示例,如下所示:

$Source = @"
using System;

namespace CSharpInPowershell
{
    public static class Sample
    {
        public static void TryDataTable()
        {
            Console.WriteLine("Hello World");
            Console.ReadLine();
        }
    }
}
"@

Add-Type -TypeDefinition $Source -Language CSharp

[CSharpInPowershell.Sample]::TryDataTable()

However, I am getting an error when trying to add a data table: 但是,尝试添加数据表时出现错误:

Add-Type -AssemblyName System.Data

$Source = @"
using System;
using System.Data;

namespace CSharpInPowershell
{
    public static class Sample
    {
        public static void TryDataTable()
        {
            Console.WriteLine("Hello World");
            DataTable table = new DataTable();
            Console.ReadLine();
        }
    }
}
"@

Add-Type -TypeDefinition $Source -Language CSharp

[CSharpInPowershell.Sample]::TryDataTable()  

The error I get is the following: 我得到的错误如下:

Add-Type : c:\\Users(userid)\\AppData\\Local\\Temp\\qbefurwr.0.cs(2) : The type or namespace name 'Data' does not exist in the namespace 'System' (are you missing an assembly reference?) c:\\Users(userid)\\AppData\\Local\\Temp\\qbefurwr.0.cs(1) : using System; Add-Type:c:\\ Users(用户ID)\\ AppData \\ Local \\ Temp \\ qbefurwr.0.cs(2):类型或名称空间名称'Data'在名称空间'System'中不存在(您是否缺少程序集参考?)c:\\ Users(用户ID)\\ AppData \\ Local \\ Temp \\ qbefurwr.0.cs(1):使用系统; c:\\Users(userid)\\AppData\\Local\\Temp\\qbefurwr.0.cs(2) : >>> using System.Data; c:\\ Users(用户ID)\\ AppData \\ Local \\ Temp \\ qbefurwr.0.cs(2):>>>使用System.Data; c:\\Users(userid)\\AppData\\Local\\Temp\\qbefurwr.0.cs(3) : namespace CSharpInPowershell At line:1 char:9 + Add-Type <<<< -TypeDefinition $Source -Language CSharp + CategoryInfo : InvalidData: (c:\\Users(userid)...bly reference?):CompilerError) [Add-Type], Exception + FullyQualifiedErrorId : SOURCE_CODE_ERROR,Microsoft.PowerShell.Commands.AddTypeCommand c:\\ Users(用户ID)\\ AppData \\ Local \\ Temp \\ qbefurwr.0.cs(3):名称空间CSharpInPowershell在第1行:1个字符:9 + Add-Type <<<< -TypeDefinition $ Source -Language CSharp + CategoryInfo: InvalidData:(c:\\ Users(userid)...引用?):CompilerError)[添加类型],异常+ FullyQualifiedErrorId:SOURCE_CODE_ERROR,Microsoft.PowerShell.Commands.AddTypeCommand

Add-Type : Cannot add type. 添加类型:无法添加类型。 There were compilation errors. 出现编译错误。 At line:1 char:9 + Add-Type <<<< -TypeDefinition $Source -Language CSharp + CategoryInfo : InvalidData: (:) [Add-Type], InvalidOperationException + FullyQualifiedErrorId : COMPILER_ERRORS,Microsoft.PowerShell.Commands.AddTypeCommand 在第1行:9个字符:9 + Add-Type <<<< -TypeDefinition $ Source -Language CSharp + CategoryInfo:InvalidData:(:) [Add-Type],InvalidOperationException + FullyQualifiedErrorId:COMPILER_ERRORS,Microsoft.PowerShell.Commands.AddTypeCommand

As you may see, I tried to add a reference using Add-Type -AssemblyName System.Data . 如您所见,我尝试使用Add-Type -AssemblyName System.Data引用。 My goal is to be able to use C# code samples within a Powershell script. 我的目标是能够在Powershell脚本中使用C#代码示例。 I know I could re-write this all in Powershell, but I'm trying to get this type of scripting to work. 我知道我可以在Powershell中重新编写所有代码,但是我正在尝试使这种脚本起作用。

How can I get the assembly reference for System.Data recognized within the C# code? 如何在C#代码中获取System.Data的程序集引用?

UPDATE: Thanks to @SomeShinyObject, I have the following working script: 更新:感谢@SomeShinyObject,我有以下工作脚本:

$Source = @"
using System;
using System.Data;

namespace CSharpInPowershell
{
    public static class Sample
    {
        public static void TryDataTable()
        {
            Console.WriteLine("Hello World");
            DataTable table = new DataTable();
            Console.ReadLine();
        }
    }
}
"@

Add-Type -TypeDefinition $Source -Language CSharp `
         -ReferencedAssemblies System.Data, System.XML

[CSharpInPowershell.Sample]::TryDataTable()

You'll need to use the ReferencedAssemblies parameter. 您将需要使用ReferencedAssemblies参数。

#When I tested, System.XML also needed to be added for it to work so I included it
Add-Type -TypeDefinition $Source -Language CSharpVersion3 -ReferencedAssemblies System.Data, System.XML

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM