简体   繁体   English

从AS400存储过程返回ResultSet

[英]Returning ResultSet from AS400 Stored Procedure

I'm Struggling returning a result set from an AS400 database using a C# console app... I can connect to the database and i have verified the connection through the AS400 logs however I seem to run into the following error: 我正在努力使用C#控制台应用程序从AS400数据库返回结果集...我可以连接到数据库,我已经通过AS400日志验证了连接但是我似乎遇到了以下错误:

System.InvalidCastException was unhandled Message=The data type returned is currently not supported by the provider. System.InvalidCastException未处理Message =提供程序当前不支持返回的数据类型。 Source=IBM.Data.DB2.iSeries StackTrace: at IBM.Data.DB2.iSeries.iDB2DbTypeUtility.MapSQLTypeToDCPCType(DcSqlTypes sqlType, Int32 colccsid, UInt32 length) at IBM.Data.DB2.iSeries.iDB2Command.setRowOfParameterData(MpDcData[]& dcDataRow) at IBM.Data.DB2.iSeries.iDB2Command.execute() at IBM.Data.DB2.iSeries.iDB2Command.ExecuteNonQuery() at GeacFutureDelivery.Program.Main(String[] args) in C:\\Users\\harlim\\documents\\visual studio 2010\\Projects\\GeacFutureDelivery\\GeacFutureDelivery\\Program.cs:line 40 at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() at System.Threading.ThreadHelper.ThreadStart_Context(Object state) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx) at System.Threading.ExecutionContext.Run(Executi Source = IBM.Data.DB2.iSeries StackTrace:位于IBM.Data.DB2.iSeries.iDB2Command.setRowOfParameterData(MpDcData []&的IBM.Data.DB2.iSeries.iDB2DbTypeUtility.MapSQLTypeToDCPCType(DcSqlTypes sqlType,Int32 colccsid,UInt32长度) dcDataRow)位于IBM.Data.DB2.iSeries.iDB2Command.execute()位于C:\\ Users \\ harlim \\中GeacFutureDelivery.Program.Main(String [] args)的IBM.Data.DB2.iSeries.iDB2Command.ExecuteNonQuery() documents \\ visual studio 2010 \\ Projects \\ GeacFutureDelivery \\ GeacFutureDelivery \\ Program.cs:System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly,String [] args)第40行,位于System.AppDomain.ExecuteAssembly(String assemblyFile,Evidence assemblySecurity,String [] args )System.Threading上System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback回调,对象状态,布尔ignoreSyncCtx)的System.Threading.ThreadHelper.ThreadStart_Context(对象状态)中的Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() .ExecutionContext.Run(Executi onContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart() InnerException: System.Threading.ThreadHelper.ThreadStart()上的onContext executionContext,ContextCallback回调,对象状态InnerException:

My source is: 我的来源是:

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);
        }
    }

Any thoughts? 有什么想法吗?

If your value is a Byte[], I think you got some trouble with CCSID65535. 如果您的值是Byte [],我认为您在使用CCSID65535时遇到了一些问题。

If so, here is a code to resolve it :-) 如果是这样,这里有一个解决它的代码:-)

 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;
 }

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

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