Oracle 查詢多個資料

後生哥哥發表於2024-08-19
<MyCommand Name="LoadDataSet" Database="DataBase" CommandType="Text">
        <Parameters>
            
<Parameter Name="cur1" Type="RefCursor" Direction="Output" />
<Parameter Name="cur2" Type="RefCursor" Direction="Output" />
            <Parameter Name="Sysid" Type="Int32" Direction="Output" />
        </Parameters>
        <CommandText><![CDATA[
begin
OPEN :cur1 FOR SELECT * FROM Table1;
OPEN :cur2 For Select count(1)  FROM Table2;
Select count(1) into :Sysid FROM Table3;
end;

]]></CommandText>
    </MyCommand>

foreach (var p in mycommand.Parameters.FindAll(x=>x.ParamType != "RefCursor"))
{
    //引數賦值後,不在語句中使用就會報 ORA-01006: 繫結變數不存在
    if (command.CommandType == CommandType.Text && !command.CommandText.Contains($":{p.ParamName}"))
    {
        continue;
    }
    //var par = new Oracle.ManagedDataAccess.Client.OracleParameter(p.ParamName,OracleDbType.Varchar2,System.Data.ParameterDirection.Input);

    var par = new Oracle.ManagedDataAccess.Client.OracleParameter();
    par.ParameterName = p.ParamName;
    par.Value = param.GetPropertyValue(p.ParamName);

    if (!string.IsNullOrEmpty(p.ParamType) && Enum.IsDefined(typeof(OracleDbType), p.ParamType))
    {
        OracleDbType enumItem = (OracleDbType)Enum.Parse(typeof(OracleDbType), p.ParamType);
        par.OracleDbType = enumItem;
    }
    if (!string.IsNullOrEmpty(p.ParamDirection) && Enum.IsDefined(typeof(ParameterDirection), p.ParamDirection))
    {
        ParameterDirection direction = (ParameterDirection)Enum.Parse(typeof(ParameterDirection), p.ParamDirection);
        par.Direction = direction;
    }

    command.Parameters.Add(par);
}
foreach (var p in mycommand.Parameters.FindAll(x => x.ParamType == "RefCursor")) {
    
    command.Parameters.Add(p.ParamName, OracleDbType.RefCursor, ParameterDirection.Output);
}
OracleDataAdapter adapter = new OracleDataAdapter();
adapter.SelectCommand = command;
DataSet ds = new DataSet();
adapter.Fill(ds);
//command.Parameters.Clear();

Dictionary<string, object> dic = new Dictionary<string, object>();
foreach (var p in mycommand.Parameters.FindAll(x => x.ParamDirection == ParameterDirection.Output.ToString() && x.ParamType != "RefCursor"))
{
    dic.Add(p.ParamName, command.Parameters[p.ParamName].Value);
    if (p.ParamType.Contains("Int"))
    {
        param.SetPropertyValue(p.ParamName, Convert.ToInt32(command.Parameters[p.ParamName].Value.ToString()));
        continue;
    }
    param.SetPropertyValue(p.ParamName, command.Parameters[p.ParamName].Value);
}

相關文章