通过ADO.Net命令调用Informix存储过程的最佳/正确方法?

时间:2011-12-13 09:18:46

标签: c# .net ado.net informix

我有一个暴露一些存储过程的Informix数据库,我有一个抽象的数据访问器来处理与它们的通信,但我有一个null值的问题。

您可以直接致电:

execute procedure some_stored_procedure(1,2,NULL,3)

并获得正确的结果,我宁愿没有这个可空的字段,但它不在我的手中。无论如何,我最初试图这样称呼它:

var command = connection.CreateCommand();
command.CommandType = CommandTypes.StoredProcedure
command.CommandText = "some_stored_procedure"
// Pass in the parameters

但是这样做会导致Informix抛出语法错误,所以相反我被迫使用:

var command = connection.CreateCommand();
command.CommandText = "execute procedure some_stored_procedure(?,?,?,?)";
// Pass in parameters

哪个有效,但从未传回正确的结果,如果我尝试将参数3设为null,则会出现另一个语法错误。我错过了什么或者有更好的方法来调用这些存储过程吗?

3 个答案:

答案 0 :(得分:1)

尝试参数化参数(如果使用odbcdriver,可以使用OdbcParameters)然后将DbNull.Value传递给需要Null的地方。

试试这个:

var command = connection.CreateCommand(); 
command.CommandType = CommandTypes.Text;
command.CommandText = "call some_stored_procedure(?,?,?,?)";

command.Parameters.Add(param); //add all your parameters.

答案 1 :(得分:0)

将查询格式设置如下:

        strQuery = string.Format("EXECUTE PROCEDURE Cronos_UpdateStateLegacyProduct ({0})", oValidityProducts.PurchaseId);

        OdbcConnection oConnection = new OdbcConnection(this.strConnectionString);
        OdbcCommand oCommand = new OdbcCommand();
        oCommand.CommandType = CommandType.StoredProcedure;
        oCommand.CommandText = strQuery;
        oCommand.Connection = oConnection;

        oConnection.Open();

        intResult = oCommand.ExecuteNonQuery();

最好的问候

答案 2 :(得分:-1)

如果您使用的是ODBC连接,则在使用CommandType.StoredProcedure时,必须对该过程的名称使用不同的语法。在你的情况下:

CommandText = "{ CALL some_stored_procedure(?,?,?,?)}"

点击此链接了解详情:https://support.microsoft.com/en-us/kb/310130