简体   繁体   English

如何用oledb调用as400存储过程?

[英]How do you call an as400 stored procedure with oledb?

I am trying to call a stored procedure from my code using oledb but I can't figure out how to do this. 我正在尝试使用oledb从我的代码中调用存储过程,但是我不知道该怎么做。 This is the code I wrote but it's not working 这是我写的代码,但是没有用

OleDbCommand sp = connectionDB.CreateCommand();
sp.CommandText = "CALL NASVARWG.§SP001('?','?','?','?','?','?')";
sp.CommandType = CommandType.StoredProcedure;

sp.Parameters.Add("P1", System.Data.OleDb.OleDbType.Char).Value = "ESANASTRIS";
sp.Parameters["P1"].Size = 10;
sp.Parameters["P1"].Direction = ParameterDirection.Input;

sp.Parameters.Add("P2", System.Data.OleDb.OleDbType.Char).Value = "SAMNAS";
sp.Parameters["P2"].Size = 10;
sp.Parameters["P2"].Direction = ParameterDirection.Input;

sp.Parameters.Add("P3", System.Data.OleDb.OleDbType.Char).Value = "blah";
sp.Parameters["P3"].Size = 10;
sp.Parameters["P3"].Direction = ParameterDirection.Input;

sp.Parameters.Add("P4", System.Data.OleDb.OleDbType.Char);
sp.Parameters["P4"].Size = 2;
sp.Parameters["P4"].Direction = ParameterDirection.Output;

sp.Parameters.Add("P5", System.Data.OleDb.OleDbType.Char);
sp.Parameters["P5"].Size = 256;
sp.Parameters["P5"].Direction = ParameterDirection.Output;

sp.Parameters.Add("P6", System.Data.OleDb.OleDbType.Char).Value = textBox_Reparto.Text;
sp.Parameters["P6"].Size = 6;
sp.Parameters["P6"].Direction = ParameterDirection.Input;

sp.Parameters.Add("P7", System.Data.OleDb.OleDbType.Char).Value = "we can do this";
sp.Parameters["P7"].Size = 60;
sp.Parameters["P7"].Direction = ParameterDirection.Input;

sp.Parameters.Add("P8", System.Data.OleDb.OleDbType.Char).Value = "help";
sp.Parameters["P8"].Size = 256;
sp.Parameters["P8"].Direction = ParameterDirection.Input;

sp.Prepare();
sp.ExecuteNonQuery();

I get an exception at "sp.Prepare();" 我在“ sp.Prepare();”处遇到异常 telling me that the syntax of the command is wrong. 告诉我该命令的语法错误。

If you can, use the IBM i Access ADO.NET drivers and place the program call in a stored procedure. 如果可以,请使用IBM i Access ADO.NET驱动程序并将程序调用放置在存储过程中。 They can convert the data from i to PC. 他们可以将数据从i转换为PC。

As @Syafiqur__ mentioned the ADO.NET drivers are a better choice. 正如@Syafiqur__所提到的,ADO.NET驱动程序是一个更好的选择。

And as always there is an IBM redbook about it. 和往常一样,有一本关于它的IBM红皮书 In my solutions I use the IBMDA400 version. 在我的解决方案中,我使用IBMDA400版本。

For your case it should look like this: 对于您的情况,它应如下所示:

var command = new iDB2Command(cmdText: "§SP001", cmdType: CommandType.StoredProcedure, connection: masterSystemConnection);
var p1 = new iDB2Parameter(parameterName: "@P1", type: iDB2DbType.iDB2Char, size: 10);
    p1.Direction = ParameterDirection.Input;
    p1.Value = "ESANASTRIS";
    command.Parameters.Add(p1);
// ... all your other parameters, no call to Prepare
command.ExecuteNonQuery();

Regarding your SQL0469: 关于您的SQL0469:

Either change the attribute of the parameter on the DECLARE PROCEDURE, CREATE PROCEDURE, or ALTER PROCEDURE statement or change the parameter. 更改DECLARE PROCEDURE,CREATE PROCEDURE或ALTER PROCEDURE语句上参数的属性或更改参数。

For additional details please have a look at the IBM documentation . 有关更多详细信息,请参阅IBM文档

Bernd 伯恩德

I think your problem is most likely that you have NO parameter markers in your SQL statement. 我认为您的问题很可能是您的SQL语句中没有参数标记。

sp.CommandText = "CALL NASVARWG.§SP001('?','?','?','?','?','?')";

The '?' '?' are string constants, not parameter markers ? 是字符串常量,不是参数标记? which should appear without apostrophes. 应该没有撇号出现。 So the statement should really look like this: 所以该语句应该看起来像这样:

sp.CommandText = "CALL NASVARWG.§SP001(?, ?, ?, ?, ?, ?)";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM