2

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.

3
  • What do you mean why say "not working"? Commented Mar 8, 2017 at 22:38
  • store the connection string in a .Config file and then change this line using (OleDbCommand command = con.CreateCommand()) to using(OleDbCommand command = new OleDbCommand(sqlStr, con)) also look up your syntax for CASE WHEN you should END as IsBatch if the syntax is the same for example in Sql Server Commented Mar 8, 2017 at 22:43
  • @abatishchev added the exception Commented Mar 8, 2017 at 22:48

1 Answer 1

1

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));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.