使用 Oledb 命令构建查询以查询 AS400 数据库

Building query with Oledb command to query AS400 database

我正在尝试使用参数化查询来查询 AS400 数据库。这是我的代码:

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

我尝试了这两个查询,其中 none 有效。如何解决这个问题?

收到此错误消息:

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.

您缺少参数。 SqlStr 需要两个。

我觉得应该是这样的

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

然后您将添加两个参数

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