Batch multiple select statements when calling Oracle from ADO.NET

后端 未结 4 1151
猫巷女王i
猫巷女王i 2020-12-10 12:05

I want to batch multiple select statements to reduce round trips to the database. The code looks something like the pseudo code below. It works perfectly on SQL Server, but

相关标签:
4条回答
  • 2020-12-10 12:49

    How about:

    var sql = @"
                select * from table1 UNION
                select * from table2 UNION
                select * from table3";
    
    0 讨论(0)
  • 2020-12-10 13:04

    You should write an anonymous pl/sql block that returns 3 ref cursors.

    edit1: Here it is done in an anonymous pl/sql block with one cursor. It should work with three too. Oracle ref cursors don't lock data and they are the fastest way to return a result set from a pl/sql procedure or an anonymous pl/sql bloc.

    http://www.oracle.com/technetwork/issue-archive/2006/06-jan/o16odpnet-087852.html

    0 讨论(0)
  • 2020-12-10 13:05

    An example in C# with multiple cursors and an input parameter:

    string ConnectionString = "connectionString";
    OracleConnection conn = new OracleConnection(ConnectionString);
    StringBuilder sql = new StringBuilder();
    
    sql.Append("begin ");
    sql.Append("open :1 for select * from table_1 where id = :id; ");
    sql.Append("open :2 for select * from table_2; ");
    sql.Append("open :3 for select * from table_3; ");
    sql.Append("end;");
    
    OracleCommand comm = new OracleCommand(sql.ToString(),_conn);
    
    comm.Parameters.Add("p_cursor_1", OracleDbType.RefCursor, DBNull.Value, ParameterDirection.Output);
    
    comm.Parameters.Add("p_id", OracleDbType.Int32, Id, ParameterDirection.Input);
    
    comm.Parameters.Add("p_cursor_2", OracleDbType.RefCursor, DBNull.Value, ParameterDirection.Output);
    
    comm.Parameters.Add("p_cursor_3", OracleDbType.RefCursor, DBNull.Value, ParameterDirection.Output);
    
    conn.Open();
    
    OracleDataReader dr = comm.ExecuteReader();
    
    0 讨论(0)
  • 2020-12-10 13:10

    why not use stored procedures instead?

    But, if you want to batch them in an inline query, you can use a semicolon (;) to seperate the statements.

    var sql = @"BEGIN
                    select * from table1;
                    select * from table2;
                    select * from table3;
                END;";
    

    EDIT: You take a look at this SO question.

    EDIT2: Take a look at this answer.

    0 讨论(0)
提交回复
热议问题