使用C#的Oracle,绑定变量和ID IN(1、2、3)之类的查询

我正在寻找以下Java技术的C#ODAC改编版本,其中代码能够将数字数组(数组大小可以变化)绑定到非PL / SQL SELECT语句中,然后在WHERE中使用结果数组ID IN(…)样式检查.

http://rafudb.blogspot.com/2011/10/variable-inlist.html

这是基于OCI的C程序中使用的该技术的另一个示例:

Oracle OCI, bind variables, and queries like ID IN (1, 2, 3)

具体来说,我们要执行如下查询

SELECT * FROM MyTable WHERE ID IN (SELECT * FROM TABLE(:1))

并将数字数组传递给:1.

解决方法:

码:

oraParam.UdtTypeName = "SYS.ODCINUMBERLIST";
VArray newArray = new VArray();
newArray.Array = new Int32[] {12,24,42};
oraParam.OracleDbType = OracleDbType.Array;
oraParam.Value = newArray;

string query = @"Select * from TABLE(:1) ";
OracleCommand command = new OracleCommand(query, MyConnection);
command.Parameters.Add(oraParam);
OracleDataReader reader;
var m_connection = new OracleConnection("The CONNECTION STRING");
m_connection.Open();
var reader = command.ExecuteReader();
reader.Close();
m_connection.Close();

接下来是以下帮助程序类:

class VArray : IOracleCustomType, INullable
{
    [OracleArrayMapping()]
    public Int32[] Array;

    private OracleUdtStatus[] m_statusArray;
    public OracleUdtStatus[] StatusArray
    {
        get
        {
            return this.m_statusArray;
        }
        set
        {
            this.m_statusArray = value;
        }
    }

    private bool m_bIsNull;

    public bool IsNull
    {
        get
        {
            return m_bIsNull;
        }
    }

    public static VArray Null
    {
        get
        {
            VArray obj = new VArray();
            obj.m_bIsNull = true;
            return obj;
        }
    }

    public void ToCustomObject(OracleConnection con, IntPtr pUdt)
    {
        object objectStatusArray = null;
        Array = (Int32[])OracleUdt.GetValue(con, pUdt, 0, out objectStatusArray);
        m_statusArray = (OracleUdtStatus[])objectStatusArray;
    }

    public void FromCustomObject(OracleConnection con, IntPtr pUdt)
    {
        OracleUdt.SetValue(con, pUdt, 0, Array, m_statusArray);
    }

    public override string ToString()
    {
        if (m_bIsNull)
            return "VArray.Null";
        else
        {
            string rtnstr = String.Empty;
            if (m_statusArray[0] == OracleUdtStatus.Null)
                rtnstr = "NULL";
            else
                rtnstr = Array.GetValue(0).ToString();
            for (int i = 1; i < m_statusArray.Length; i++)
            {
                if (m_statusArray[i] == OracleUdtStatus.Null)
                    rtnstr += "," + "NULL";
                else
                    rtnstr += "," + Array.GetValue(i).ToString();
            }
            return "VArray(" + rtnstr + ")";
        }
    }
}

[OracleCustomTypeMapping("SYS.ODCINUMBERLIST")]
public class VArrayFactory : IOracleCustomTypeFactory, IOracleArrayTypeFactory
{
    // IOracleCustomTypeFactory
    public IOracleCustomType CreateObject()
    {
        return new VArray();
    }

    // IOracleArrayTypeFactory Interface
    public Array CreateArray(int numElems)
    {
        return new Int32[numElems];
    }

    public Array CreateStatusArray(int numElems)
    {
        // CreateStatusArray may return null if null status information 
        // is not required.
        return new OracleUdtStatus[numElems];
    }
}

描述:

通常的想法是,类似于OCI示例,您必须将参数强制转换为SYS.ODCINUMBERLIST(或其他有效类型).默认情况下,在C#OracleDBType中未定义该类型,因此您必须使用UdtTypeName和自定义的Factory / Class才能成功绑定.

这是从following post on defining custom types获得灵感的.

限制:

此精确解决方案仅适用于INT / NUMBER值,因为它背负了SYS.ODCINUMBERLIST表类型.如果其他类型需要它,则可能需要查找/编写其他自定义表类型.

上一篇:Redis数据持久化—RDB持久化与AOF持久化


下一篇:elasticsearch聚合统计——pipeline使用、与mysql对比