iDB2 Select command with parameters returning SQL0

2019-05-29 05:49发布

问题:

I'm developing a .NET application that connects to a DB2 iSeries 7.1 database, using the IBM.Data.DB2.iSeries.dll.

I need to do a SELECT command that has n parameters which are defined in the query as @paramX, setting the parameter values afterwards, but when I run the code I get a SQL048 Use of parameter marker not valid.. I've searched everywhere for documentation / examples but everything I've read is in par with the code I'm using. Am I missing something? If this is not valid, what is the best alternative?

This is the isolated code I'm using to test.

    static void Main(string[] args)
    {
        String myConnectionString = "DataSource=*******;Database=*******;UserId=*******;Password=*******;";
        iDB2Connection myConnection = new iDB2Connection();
        try{
            myConnection.ConnectionString = myConnectionString;
            myConnection.Open();

            var cmd = new iDB2Command("SELECT TIMESTAMP(DATE(@param0),TIME(@param1)) FROM SYSIBM.SYSDUMMY1", myConnection);

            cmd.Parameters.Add(new iDB2Parameter("@param0", iDB2DbType.iDB2Char));
            cmd.Parameters["@param0"].Value = "1900-01-01";

            cmd.Parameters.Add(new iDB2Parameter("@param1", iDB2DbType.iDB2Char));
            cmd.Parameters["@param1"].Value = "00.00.00";

            using (var reader = cmd.ExecuteReader())
            {
                if (reader.HasRows)
                {
                    reader.Read();
                    StringBuilder sb = new StringBuilder();

                    for (int i = 0; i < reader.FieldCount; i++)
                    {
                        sb.AppendLine(reader[i].ToString().Trim());
                    }

                    Console.Out.WriteLine(sb.ToString());
                }
            }
        }catch(Exception e)
        {
                Console.Out.WriteLine(e.ToString());
        }finally{
            if (myConnection != null)
            {
                myConnection.Close();
            }
        }
        Console.Read();
    }

EDIT

In an unrelated answer I've found that the problem might be that DB2 doesn't know the underlying type of the parameter (which is strange since I'm strong typing it), thus, a possible solution is to do a cast in the query to the expected param type, as such:

SELECT TIMESTAMP(DATE(cast(@param0 as char(10))),TIME(cast(@param1 as char(10)))) FROM SYSIBM.SYSDUMMY1

This actually worked but, isn't there any better way to handle this?

回答1:

AFAIK, this is a platform limitation. that can be confirmed by an explanation that the platform adds to the application exception*. That being said, as I can't change the parameters I receive and don't have access to the info they are going to held in the query, the best solution to my specific problem is to do a CAST to the types that the TIMESTAMP scalar function uses, e.g.:

SELECT TIMESTAMP(cast(@param0 as DATE),cast(@param1 as TIME)) FROM SYSIBM.SYSDUMMY1



标签: c# sql .net db2