我有一个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.
如何在C#控制台屏幕中查看用于此的数据,PowerShell文件名,用户名和数据库?
答案 0 :(得分:0)
根本没有理由使用Powershell。 Powershell脚本使用ADO.NET objects,它们更容易在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;
}