3

我正在努力使用 C# 控制台应用程序从 AS400 数据库返回结果集...我可以连接到数据库并且我已经通过 AS400 日志验证了连接但是我似乎遇到了以下错误:

System.InvalidCastException 未处理 Message=提供者当前不支持返回的数据类型。Source=IBM.Data.DB2.iSeries StackTrace:在 IBM.Data.DB2.iSeries.iDB2Command.setRowOfParameterData(MpDcData[]& dcDataRow) 在 IBM.Data.DB2.iSeries.iDB2Command.execute() 在 IBM.Data.DB2.iSeries.iDB2Command.ExecuteNonQuery() 在 GeacFutureDelivery.Program.Main(String[] args) 在 C:\Users\harlim\文档\visual studio 2010\Projects\GeacFutureDelivery\GeacFutureDelivery\Program.cs:System.AppDomain._nExecuteAssembly 的第 40 行(RuntimeAssembly 程序集,String[] args)在 System.AppDomain.ExecuteAssembly(字符串 assemblyFile,证据 assemblySecurity,

我的来源是:

var connectionString = "<Connection String>";
        int startDate = 1120530;
        int endDate = 1120602;

        try
        {
            using (var connection = new iDB2Connection(connectionString))
            {
                connection.Open();

                iDB2Transaction trans = connection.BeginTransaction();

                iDB2Command command = connection.CreateCommand();


                string query = "DRLOBJ01.GETORDPCD";
                command.Transaction = trans;
                command.CommandType = CommandType.StoredProcedure; 
                command.CommandText = query;
                command.CommandTimeout = 0;

                command.Parameters.Add("DTSTR", iDB2DbType.iDB2Integer).Value = startDate;
                command.Parameters.Add("DTEND", iDB2DbType.iDB2Integer).Value = endDate;

                command.Prepare();


                using (iDB2DataReader reader = command.ExecuteReader())
                 {
                    int iCUSO51 = reader.GetOrdinal("CUSO51");
                    int iORDN51 = reader.GetOrdinal("ORDN51");
                    int iDTDR51 = reader.GetOrdinal("DTDR51");
                    int iOPST45 = reader.GetOrdinal("OPST45");

                    while (reader.Read())
                    {
                        if (!string.IsNullOrEmpty(reader.GetString(iCUSO51)))
                        {
                            Console.WriteLine((string)reader[iCUSO51]);
                            Console.WriteLine((string)reader[iORDN51]);
                            Console.WriteLine((string)reader[iDTDR51]);
                            Console.WriteLine((string)reader[iOPST45]);
                        }
                    }
                }
            }
        }
        catch (iDB2CommErrorException ex)
        {
            Console.WriteLine(ex.Message);
        }
    }

有什么想法吗?

4

1 回答 1

0

如果您的值是 Byte[],我认为您在使用 CCSID65535 时遇到了一些问题。

如果是这样,这是解决它的代码:-)

 private DataTable parseCCSID65535(DataTable p_dt)
 {
     DataTable dt = new DataTable();

     // Build a new DataTable
     for (int i = 0; i < p_dt.Columns.Count; i++)
     {
         dt.Columns.Add(p_dt.Columns[i].Caption);
     }

     //loop through the rows
     for (int r = 0; r < p_dt.Rows.Count; r++)
     {
         //create a new row
         string[] row = new string[p_dt.Columns.Count];

         //loop through all columns
         for (int c = 0; c < p_dt.Columns.Count; c++)
         {
             if (p_dt.Rows[r][c].GetType() == typeof(System.Byte[]))
             {
                 // if this value is CCSID65535, change it ;-)
                 iDB2CharBitData cbd = new iDB2CharBitData((Byte[])p_dt.Rows[r][c]);
                 row[c] = cbd.ToString(65535);
             }
             else
             {
                 // else: go on.
                 row[c] = p_dt.Rows[r][c].ToString();
             }
         }
         // passing to the new DataTable.
         dt.Rows.Add(row);
     }
     return dt;
 }
于 2012-07-06T12:33:26.170 回答