How to pass an Informix Collection Parameter (LIST, SET, MULTISET) in c# through an ODBC Connection

雨燕双飞 提交于 2019-12-11 05:09:09

问题


I'm looking for a way to pass an Informix List Parameter in C#.

I asked a previus question of How to pass a multi value parameter to Informix, but now I need to execute it from C#.

The related question is here.

In resume I have a procedure like this.

CREATE PROCEDURE test_3(c LIST(CHAR(10) NOT NULL))
    RETURNING CHAR(10) AS r;
    DEFINE r CHAR(10);
    FOREACH SELECT * INTO r FROM TABLE(c)
        RETURN r WITH RESUME;
    END FOREACH;
END PROCEDURE;

It works fine executing it in Aqua Data Studio.8.0.22 like this

 EXECUTE PROCEDURE test_3('LIST{''stspols'',''stsrepo''}');

So I made a quick example of how executing it in c#.

First like a CommandType.Text

string strParameters = "LIST{''stspols'',''stsrepo''}";
using (OdbcConnection oConnection = new OdbcConnection("DSN=MYDSN;UID=MYUID;PWD=MYPWD;"))
      {
           oConnection.Open();
           using (OdbcDataAdapter oCommand = new OdbcDataAdapter(string.Format("EXECUTE PROCEDURE test_3('{0}')", strParameters), oConnection))
           {                   
                using (DataTable dt = new DataTable())
                {
                            oCommand.Fill(dt);
                }

            }
      }

This one works FINE.

So I got courious and tried to execute it but as CommandType.StoredProcedure

string strParameters = "LIST{''stspols'',''stsrepo''}";
            using (OdbcConnection oConnection = new OdbcConnection("DSN=MYDSN;UID=MYUID;PWD=MYPWD;"))
            {
                oConnection.Open();
                using (OdbcCommand oCommand = new OdbcCommand("{CALL test_3(?)}", oConnection))
                {
                    oCommand.CommandType = CommandType.StoredProcedure;
                    OdbcParameter oParameter = new OdbcParameter("c", OdbcType.Char, 4000);
                    oParameter.Value = strParameters;
                    oCommand.Parameters.Add(oParameter);

                    using (OdbcDataAdapter oDataAdapter = new OdbcDataAdapter(oCommand))
                    {
                        using (DataTable dt = new DataTable())
                        {
                            oDataAdapter.Fill(dt);
                        }

                    }
                }

            }

But now I get ERROR [HY000] [Informix][Informix ODBC Driver][Informix]Invalid collection literal value.

So after all of this, my final question is

How I can execute this kind of Informix procedures from C#, with a Collection parameter Type (LIST, SET, MULTISET) as a Stored Procedure.

Apparently I'm doing something wrong.

Thanks in advance for your valuable help.


回答1:


The doubled-up single quotes were necessary in raw SQL, but should not be needed in the parameterized query. You should be able to replace:

string strParameters = "LIST{''stspols'',''stsrepo''}";

with:

string strParameters = "LIST{'stspols','stsrepo'}";

Understanding when quotes need to be doubled and when they don't is tricky, but 'not in placeholder values' is accurate.



来源:https://stackoverflow.com/questions/11038298/how-to-pass-an-informix-collection-parameter-list-set-multiset-in-c-sharp-th

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!