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();
}
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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With