Search code examples
c#ado.netoledbibm-midrangedb2-400

Building query with Oledb command to query AS400 database


Am trying to query the AS400 database with parameterised query. Here is my code:

string connectionString = ConfigurationManager.ConnectionStrings["****"].ConnectionString;
using (OleDbConnection con = new OleDbConnection(connectionString))
using (OleDbCommand command = con.CreateCommand())
{
    string sqlStr = @"SELECT CASE IsBatch WHEN 'Y' THEN 1 ELSE 0 END
                      FROM Batch200 
                      WHERE BatchID=LEFT(?,4) AND BatchNumber=CAST(RIGHT(?,9)AS INT)";

    command.CommandType = CommandType.Text;
    command.CommandText = str;
    command.Parameters.Add(new OleDbParameter("@p1", polNumber));

    var option = command.ExecuteScalar();
}

I tried with both the queries, none of them is working. How to resolve this issue ?

Am getting this error message:

SQL5016: Qualified object name **** not valid.

Cause . . . . . : One of the following has occurred:

-- The syntax used for the qualified object name is not valid for the naming option specified. With system naming, the qualified form of an object name is schema-name/object-name. With SQL naming the qualified form of an object name is authorization-name.object-name.

-- The syntax used for the qualified object name is not allowed. User-defined types cannot be qualified with the schema in the system naming convention on parameters and SQL variables of an SQL procedure or function.

Recovery . . . : Do one of the following and try the request again:

-- If you want to use the SQL naming convention, verify the SQL naming option in the appropriate SQL command and qualify the object names in the form authorization-id.object-name.

-- If you want to use the system naming convention, specify the system naming option in the appropriate SQL command and qualify the object names in the form schema-name/object-name.

-- With the system naming convention, ensure the user-defined types specified for parameters and variables in an SQL routine can be found in the current path.


Solution

  • You're missing parameters. SqlStr expects two.

    I think it should read something like

    string sqlStr = @"SELECT CASE IsBatch WHEN 'Y' THEN 1 ELSE 0 END
                      FROM Batch200 
                      WHERE BatchID=LEFT(@BATCHID,4) AND BatchNumber=CAST(RIGHT(@BATCHNUMBER,9)AS INT)";
    

    You would then add the two parameters

    command.Parameters.Add(new OleDbParameter("@BATCHID", batchID));
    command.Parameters.Add(new OleDbParameter("@BATCHNUMBER", batchNumber));