Ole DB通信在Powershell中工作而不是在C#中

问题描述

我试图通过Ole Db HDA从ABB 800xa历史学家那里接收信息。我在Powershell中有一个工作脚本,但是当我在C#Visual Studio控制台应用程序项目中执行相同的操作时,我得到一个退出代码0xc0000374。

Powershell脚本和我的C#代码之间有什么区别? 附注:在C#中,通过Ole Db的连接有效,有些命令有效,但有些不起作用

我的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()

我的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();
        }

解决方法

经过一个下午的尝试,我发现以.net 4.0为目标时,System.Data.OleDb可以正常运行。 .net的所有其他测试版本(4.5、4.6和4.7.2)均无法正常工作。

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...