开发者

Passing parameterized query list of int's returns error ora-01722

开发者 https://www.devze.com 2023-03-06 03:57 出处:网络
Using the oledb provider.Query is something like this: SELECT appid FROM table WHERE response_id IN (?)

Using the oledb provider. Query is something like this:

SELECT appid 
  FROM table 
 WHERE response_id IN (?)

I take an int array and send it to a method that adds a comma delimiter between the array values, and returns a string. This string is then sent as the parameter.

This works fine if I have one value to pass through, but when I send two values I get the ORA-01722 error.

I've tried looking at the v_$sql table to see what's being executed, but it's not s开发者_高级运维howing queries executed by my page. I can only see things I executed via toad, even though I'm using the same login in both cases. Not sure if there are other tables that store sql data.

The string builder is below.

public string intArrayToString(int[] array)
{
    if (array != null)
    {
        string delimiter = ",";

        if (array.Length > 0)
        {
            StringBuilder builder = new StringBuilder();

            builder.Append(array[0]);
            for (int i = 1; i < array.Length; i++)
            {
                builder.Append(delimiter);
                builder.Append(array[i]);
            }                    
            return builder.ToString();
        }
        else
        {
            return string.Empty;
        }
    }
    else
    {
        return null;
    }
}


You cannot just put a comma-separated string as the IN value unfortunately. What you can do is automatically generating a bind variable for each of the array elements and binding each value, like so:

select appid from table where response_id in (:id1, :id2, :id3)


Which driver to you use to connect to Oracle? Here two different odp.net solutions: http://forums.oracle.com/forums/thread.jspa?threadID=892457&tstart=810

edit: I see that you use the oledb provider. I guess that limits the possibilities? (I've never used that provider so I don't know). Maybe it is time to switch?

0

精彩评论

暂无评论...
验证码 换一张
取 消