简体   繁体   English

Ole Db 通信在 powershell 中而不是在 c# 中工作

[英]Ole Db communication works in powershell not in c#

Im trying to receive information from an ABB 800xa historian via Ole Db HDA.我正在尝试通过 Ole Db HDA 从 ABB 800xa 历史数据库接收信息。 I have a working script in powershell but when i do the same in a C# visual studio console app project i get an exit code 0xc0000374.我在 powershell 中有一个工作脚本,但是当我在 C# Visual Studio 控制台应用程序项目中执行相同操作时,我得到一个退出代码 0xc0000374。

What could be the difference between the Powershell script and my c# code? Powershell 脚本和我的 c# 代码之间有什么区别? Ps: In C# the connection via Ole Db works and some commands do work but some don't Ps:在 C# 中,通过 Ole Db 的连接有效,有些命令有效,但有些无效

My Powershell script:我的 Powershell 脚本:

clear-host
$objConn = New-Object System.Data.OleDb.OleDbConnection ("Provider=ABB OLE DB Provider for HT;Persist Security Info=False;User ID="";Data Source="";Location="";Mode=ReadWrite;Extended Properties=""")
$sqlCommand = New-Object System.Data.OleDb.OleDbCommand("GET_HISTORY(OBJECTHELPER=History Log Template Dion)")
$sqlCommand.Connection = $objConn
$objConn.open()
write-host $sqlCommand.CommandText
$reader = $sqlCommand.ExecuteReader()
$Counter = $Reader.FieldCount
while ($Reader.Read()) {
    for ($i = 0; $i -lt $Counter; $i++) {
        @{ $Reader.GetName($i) = $Reader.GetValue($i); }
    }
}
$reader.Close()
$sqlCommand.Dispose()
$objConn.close()
$objConn.Dispose()

My C# code我的 C# 代码

static void Main(string[] args)
        {
            string connetionString = null;
            OleDbConnection cnn;
            OleDbCommand cmd;
            string sql = null;
            OleDbDataReader reader;

            connetionString = "Provider=ABB OLE DB Provider for HT;Persist Security Info=False;User ID=\"\";Data Source=\"\";Location=\"\";Mode=ReadWrite;Extended Properties=\"\"";

            //OBJECTHELPER
            sql = "GET_HISTORY(OBJECTHELPER=History Log Template Dion)";

            cnn = new OleDbConnection(connetionString);

            try
            {
                cmd = new OleDbCommand(sql, cnn);
                cnn.Open();
                reader = cmd.ExecuteReader();
                Console.WriteLine("Values:");
                while (reader.Read())
                {
                    for (int i = 0; i < reader.FieldCount; i++)
                    {
                        Console.WriteLine(reader.GetName(i) + " - " + reader.GetValue(i));
                    }
                }
                reader.Close();
                cmd.Dispose();
                cnn.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error while executing command 2 !");
                Console.WriteLine(ex.Message);
            }

            Console.ReadLine();
        }

After an afternoon of trying I found out that when targeting .net 4.0 the System.Data.OleDb functions as expected.经过一个下午的尝试,我发现当面向 .net 4.0 时,System.Data.OleDb 会按预期运行。 All other tested versions of .net (4.5, 4.6 and 4.7.2) dit not work.所有其他经过测试的 .net 版本(4.5、4.6 和 4.7.2)都不起作用。

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

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