Ole Db communication works in powershell not in c#

1

Im trying to receive information from an ABB 800xa historian via Ole Db HDA. 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.

What could be the difference between the Powershell script and my c# code? P.s.: In C# the connection via Ole Db works and some commands do work but some don't

My Powershell script:

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

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();
        }
c#
powershell
oledb
asked on Stack Overflow Sep 15, 2020 by Dion Bartelen

1 Answer

0

After an afternoon of trying I found out that when targeting .net 4.0 the System.Data.OleDb functions as expected. All other tested versions of .net (4.5, 4.6 and 4.7.2) dit not work.

answered on Stack Overflow Sep 15, 2020 by Dion Bartelen

User contributions licensed under CC BY-SA 3.0