繁体   English   中英

如何在C#控制台应用程序后台运行具有ODP依赖关系的Powershell文件?

[英]How to run Powershell file with ODP dependency in background of C# console app?

我有一个PowerShell脚本文件,其中包含用于连接到oracle数据库的ODP程序集。 我想在C#控制台应用程序的后台运行它,然后在C#控制台中显示结果。 这是PowerShell脚本:

Add-Type -path "C:\path\Oracle.DataAccess.dll"
$constr = "User Id=userxxx;Password=xxxx;Data Source=xxxx"
$conn= New-Object Oracle.DataAccess.Client.OracleConnection($constr)
$conn.Open()
$sql="SELECT A,B,C,D FROM XXXXX"
$command = New-Object Oracle.DataAccess.Client.OracleCommand( $sql,$conn)
$reader=$command.ExecuteReader()
$columnNames=$reader.GetSchemaTable() | Select-Object -ExpandProperty ColumnName
$resultSet=@()
while ($reader.Read()) {
    $result=New-Object object
        $result | Add-Member -NotePropertyName $columnNames[0] -NotePropertyValue $reader.GetDateTime(0)
        $result | Add-Member -NotePropertyName $columnNames[1] -NotePropertyValue $reader.GetDateTime(1)
        $result | Add-Member -NotePropertyName $columnNames[2] -NotePropertyValue $reader.GetDateTime(2)
        $result | Add-Member -NotePropertyName $columnNames[3] -NotePropertyValue $reader.GetDateTime(3)
    $resultSet += $result
}
$conn.Close()
$resultSet | Format-Table -AutoSize 
Read-Host -Prompt “Press Enter to exit”

我使用C#尝试了以下代码以使用PowerShell脚本:-

RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();
Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration);
runspace.Open();
RunspaceInvoke scriptInvoker = new RunspaceInvoke(runspace);
Pipeline pipeline = runspace.CreatePipeline();
Command myCommand = new Command(@"C:\path\connectDB.ps1");
pipeline.Commands.Add(myCommand);
pipeline.Commands.Add("Out-String");
Collection<PSObject> results = pipeline.Invoke();
runspace.Close();
StringBuilder stringBuilder = new StringBuilder();
foreach (PSObject obj in results)
{
    stringBuilder.AppendLine(obj.ToString());
}

Console.WriteLine(stringBuilder.ToString());
Console.ReadLine(); 

但是我遇到了这个异常: An unhandled exception of type 'System.Management.Automation.CmdletInvocationException' occurred in System.Management.Automation.dll

这是消息: Additional information: Could not load file or assembly 'file:///C:\\path\\Oracle.DataAccess.dll' or one of its dependencies. An attempt was made to load a program with an program with an incorrect format. Additional information: Could not load file or assembly 'file:///C:\\path\\Oracle.DataAccess.dll' or one of its dependencies. An attempt was made to load a program with an program with an incorrect format.

如何在C#控制台屏幕中查看用于此的数据,PowerShell文件名,用户名和数据库?

完全没有理由使用Powershell。 Powershell脚本使用ADO.NET对象 ,这些对象在C#中更易于使用。 OracleDataReader的文档中有一个示例,该示例显示了如何执行查询和读取结果。

public void ReadData(string connectionString)
{
   string queryString = "SELECT A,B,C,D FROM XXXXX";
   using (var connection = new OracleConnection(connectionString))
   {
       var command = new OracleCommand(queryString, connection);
       connection.Open();
       using(OracleDataReader reader = command.ExecuteReader())
       {
           while (reader.Read())
           {
               Console.WriteLine("{0}, {1}, {2}, {3}",
                                 reader.GetDateTime(0),
                                 reader.GetDateTime(1),
                                 reader.GetDateTime(2),
                                 reader.GetDateTime(3));
           }
       }
  }
}

该问题并未说明如何使用结果。 它们很可能不会被打印出来。

一种选择是在循环本身中使用它们。 另一个选择是通过调用DataTable.Load()将它们加载到DataTable中 ,例如

using(OracleDataReader reader = command.ExecuteReader())
{
    var table=new DataTable();
    table.Load(reader);
    return table;
}

DataTable将包含阅读器返回的列和类型。

ADO.NET是.NET的一部分从一开始,所以它很好地涵盖了文档,课程,文章和教程。

如今,使用诸如实体框架之类的ORM或诸如Dapper之类的微ORM直接将结果直接加载到强类型对象中,已经变得越来越普遍。 假设我们有这个课:

class MyResultClass
{
    public DateTime A {get;set;}
    public DateTime B {get;set;}
    public DateTime C {get;set;}
    public DateTime D {get;set;}
}

Dapper允许我们使用简单的方法加载结果列表:

string queryString = "SELECT A,B,C,D FROM XXXXX";
using (var connection = new OracleConnection(connectionString))
{
    var results=connection.Query<MyResultClass>(queryString);
    return results;
}

暂无
暂无

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

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