Calling an Oracle Stored Procedure with C# Code

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
1
down vote

favorite












I came across this code in a project I inherited. I am curious if all the nested using statements make sense to anyone else. Also, wondering if this code could not be written in a better/leaner way?



 private static Dictionary<string, List<string>> GetSubscriptionKeyList(string proc)

var addResults = new List<string>();
var deleteResults = new List<string>();

using (var conn = new OracleConnection(conStr))

conn.Open();
using (var cmd = new OracleCommand(proc, conn))

cmd.CommandType = CommandType.StoredProcedure;

using (var adds = new OracleParameter

ParameterName = "p_add",
OracleDbType = OracleDbType.RefCursor,
Direction = ParameterDirection.Output,
Value = DBNull.Value
)

using (var deletes = new OracleParameter

ParameterName = "p_delete",
OracleDbType = OracleDbType.RefCursor,
Direction = ParameterDirection.Output,
Value = DBNull.Value
)

cmd.Parameters.Add(adds);
cmd.Parameters.Add(deletes);

using (var rdr = cmd.ExecuteReader())

while (rdr.Read())

addResults.Add(rdr.GetString(0));

rdr.NextResult();
while (rdr.Read())

deleteResults.Add(rdr.GetString(0));






conn.Close();

var result = new Dictionary<string, List<string>>

"Adds", addResults,
"Deletes", deleteResults
;
return result;







share|improve this question



















  • What framework/nuget are you using? I cannot find the OracleDbType property on the OracleParameter. There's only OracleType.
    – t3chb0t
    May 22 at 16:04










  • @t3chb0t Oracle.DataAccess.Client.OracleDbType
    – ADH
    May 22 at 16:35











  • Could you add the stored procedure too?
    – t3chb0t
    May 22 at 17:31
















up vote
1
down vote

favorite












I came across this code in a project I inherited. I am curious if all the nested using statements make sense to anyone else. Also, wondering if this code could not be written in a better/leaner way?



 private static Dictionary<string, List<string>> GetSubscriptionKeyList(string proc)

var addResults = new List<string>();
var deleteResults = new List<string>();

using (var conn = new OracleConnection(conStr))

conn.Open();
using (var cmd = new OracleCommand(proc, conn))

cmd.CommandType = CommandType.StoredProcedure;

using (var adds = new OracleParameter

ParameterName = "p_add",
OracleDbType = OracleDbType.RefCursor,
Direction = ParameterDirection.Output,
Value = DBNull.Value
)

using (var deletes = new OracleParameter

ParameterName = "p_delete",
OracleDbType = OracleDbType.RefCursor,
Direction = ParameterDirection.Output,
Value = DBNull.Value
)

cmd.Parameters.Add(adds);
cmd.Parameters.Add(deletes);

using (var rdr = cmd.ExecuteReader())

while (rdr.Read())

addResults.Add(rdr.GetString(0));

rdr.NextResult();
while (rdr.Read())

deleteResults.Add(rdr.GetString(0));






conn.Close();

var result = new Dictionary<string, List<string>>

"Adds", addResults,
"Deletes", deleteResults
;
return result;







share|improve this question



















  • What framework/nuget are you using? I cannot find the OracleDbType property on the OracleParameter. There's only OracleType.
    – t3chb0t
    May 22 at 16:04










  • @t3chb0t Oracle.DataAccess.Client.OracleDbType
    – ADH
    May 22 at 16:35











  • Could you add the stored procedure too?
    – t3chb0t
    May 22 at 17:31












up vote
1
down vote

favorite









up vote
1
down vote

favorite











I came across this code in a project I inherited. I am curious if all the nested using statements make sense to anyone else. Also, wondering if this code could not be written in a better/leaner way?



 private static Dictionary<string, List<string>> GetSubscriptionKeyList(string proc)

var addResults = new List<string>();
var deleteResults = new List<string>();

using (var conn = new OracleConnection(conStr))

conn.Open();
using (var cmd = new OracleCommand(proc, conn))

cmd.CommandType = CommandType.StoredProcedure;

using (var adds = new OracleParameter

ParameterName = "p_add",
OracleDbType = OracleDbType.RefCursor,
Direction = ParameterDirection.Output,
Value = DBNull.Value
)

using (var deletes = new OracleParameter

ParameterName = "p_delete",
OracleDbType = OracleDbType.RefCursor,
Direction = ParameterDirection.Output,
Value = DBNull.Value
)

cmd.Parameters.Add(adds);
cmd.Parameters.Add(deletes);

using (var rdr = cmd.ExecuteReader())

while (rdr.Read())

addResults.Add(rdr.GetString(0));

rdr.NextResult();
while (rdr.Read())

deleteResults.Add(rdr.GetString(0));






conn.Close();

var result = new Dictionary<string, List<string>>

"Adds", addResults,
"Deletes", deleteResults
;
return result;







share|improve this question











I came across this code in a project I inherited. I am curious if all the nested using statements make sense to anyone else. Also, wondering if this code could not be written in a better/leaner way?



 private static Dictionary<string, List<string>> GetSubscriptionKeyList(string proc)

var addResults = new List<string>();
var deleteResults = new List<string>();

using (var conn = new OracleConnection(conStr))

conn.Open();
using (var cmd = new OracleCommand(proc, conn))

cmd.CommandType = CommandType.StoredProcedure;

using (var adds = new OracleParameter

ParameterName = "p_add",
OracleDbType = OracleDbType.RefCursor,
Direction = ParameterDirection.Output,
Value = DBNull.Value
)

using (var deletes = new OracleParameter

ParameterName = "p_delete",
OracleDbType = OracleDbType.RefCursor,
Direction = ParameterDirection.Output,
Value = DBNull.Value
)

cmd.Parameters.Add(adds);
cmd.Parameters.Add(deletes);

using (var rdr = cmd.ExecuteReader())

while (rdr.Read())

addResults.Add(rdr.GetString(0));

rdr.NextResult();
while (rdr.Read())

deleteResults.Add(rdr.GetString(0));






conn.Close();

var result = new Dictionary<string, List<string>>

"Adds", addResults,
"Deletes", deleteResults
;
return result;









share|improve this question










share|improve this question




share|improve this question









asked May 22 at 15:31









ADH

20829




20829











  • What framework/nuget are you using? I cannot find the OracleDbType property on the OracleParameter. There's only OracleType.
    – t3chb0t
    May 22 at 16:04










  • @t3chb0t Oracle.DataAccess.Client.OracleDbType
    – ADH
    May 22 at 16:35











  • Could you add the stored procedure too?
    – t3chb0t
    May 22 at 17:31
















  • What framework/nuget are you using? I cannot find the OracleDbType property on the OracleParameter. There's only OracleType.
    – t3chb0t
    May 22 at 16:04










  • @t3chb0t Oracle.DataAccess.Client.OracleDbType
    – ADH
    May 22 at 16:35











  • Could you add the stored procedure too?
    – t3chb0t
    May 22 at 17:31















What framework/nuget are you using? I cannot find the OracleDbType property on the OracleParameter. There's only OracleType.
– t3chb0t
May 22 at 16:04




What framework/nuget are you using? I cannot find the OracleDbType property on the OracleParameter. There's only OracleType.
– t3chb0t
May 22 at 16:04












@t3chb0t Oracle.DataAccess.Client.OracleDbType
– ADH
May 22 at 16:35





@t3chb0t Oracle.DataAccess.Client.OracleDbType
– ADH
May 22 at 16:35













Could you add the stored procedure too?
– t3chb0t
May 22 at 17:31




Could you add the stored procedure too?
– t3chb0t
May 22 at 17:31










2 Answers
2






active

oldest

votes

















up vote
2
down vote



accepted










I could see using it for the connection, command and reader since they implement IDisposable and the documentation for them does say to always call Dispose().



For the parameters it seems not only unnecessary but in my opinion it really decreases readability. I would kill the using statements for the parameters and make them simple declarations.



For what it's worth, here's how I'd write it.



private static Dictionary<string, List<string>> GetSubscriptionKeyList(string proc)


var result = new Dictionary<string, List<string>>();
result.Add("Adds", new List<string>());
result.Add("Deletes", new List<string>());

using (var conn = new OracleConnection(conStr))

conn.Open();
using (var cmd = new OracleCommand(proc, conn))

cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.Add(new OracleParameter("p_add", OracleDbType.RefCursor, ParameterDirection.Output));
cmd.Parameters.Add(new OracleParameter("p_delete", OracleDbType.RefCursor, ParameterDirection.Output));

using (var rdr = cmd.ExecuteReader())

while (rdr.Read())

result["Adds"].Add(rdr.GetString(0));

rdr.NextResult();
while (rdr.Read())

result["Deletes"].Add(rdr.GetString(0));




conn.Close();

return result;



Obviously some things are a matter of taste, like whether to declare the parameters on a separate line and whether to keep the initializers there for the dictionary. (I thought it was more clear to separate them.)






share|improve this answer






























    up vote
    1
    down vote














    using (var rdr = cmd.ExecuteReader())

    while (rdr.Read())

    addResults.Add(rdr.GetString(0));

    rdr.NextResult();
    while (rdr.Read())

    deleteResults.Add(rdr.GetString(0));





    I don't think it's such a good idea to rely on the result order of your stored procedure.



    You assume that the first row is always adds and the second row is deletes. I find it'd be better if you included this in the result themself and then created a dictionary based on the value in that specific column.






    share|improve this answer





















    • I agree this looks unusual. I will look into getting this changed.
      – ADH
      May 23 at 12:09










    Your Answer




    StackExchange.ifUsing("editor", function ()
    return StackExchange.using("mathjaxEditing", function ()
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
    );
    );
    , "mathjax-editing");

    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "196"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );








     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f194954%2fcalling-an-oracle-stored-procedure-with-c-code%23new-answer', 'question_page');

    );

    Post as a guest






























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote



    accepted










    I could see using it for the connection, command and reader since they implement IDisposable and the documentation for them does say to always call Dispose().



    For the parameters it seems not only unnecessary but in my opinion it really decreases readability. I would kill the using statements for the parameters and make them simple declarations.



    For what it's worth, here's how I'd write it.



    private static Dictionary<string, List<string>> GetSubscriptionKeyList(string proc)


    var result = new Dictionary<string, List<string>>();
    result.Add("Adds", new List<string>());
    result.Add("Deletes", new List<string>());

    using (var conn = new OracleConnection(conStr))

    conn.Open();
    using (var cmd = new OracleCommand(proc, conn))

    cmd.CommandType = CommandType.StoredProcedure;

    cmd.Parameters.Add(new OracleParameter("p_add", OracleDbType.RefCursor, ParameterDirection.Output));
    cmd.Parameters.Add(new OracleParameter("p_delete", OracleDbType.RefCursor, ParameterDirection.Output));

    using (var rdr = cmd.ExecuteReader())

    while (rdr.Read())

    result["Adds"].Add(rdr.GetString(0));

    rdr.NextResult();
    while (rdr.Read())

    result["Deletes"].Add(rdr.GetString(0));




    conn.Close();

    return result;



    Obviously some things are a matter of taste, like whether to declare the parameters on a separate line and whether to keep the initializers there for the dictionary. (I thought it was more clear to separate them.)






    share|improve this answer



























      up vote
      2
      down vote



      accepted










      I could see using it for the connection, command and reader since they implement IDisposable and the documentation for them does say to always call Dispose().



      For the parameters it seems not only unnecessary but in my opinion it really decreases readability. I would kill the using statements for the parameters and make them simple declarations.



      For what it's worth, here's how I'd write it.



      private static Dictionary<string, List<string>> GetSubscriptionKeyList(string proc)


      var result = new Dictionary<string, List<string>>();
      result.Add("Adds", new List<string>());
      result.Add("Deletes", new List<string>());

      using (var conn = new OracleConnection(conStr))

      conn.Open();
      using (var cmd = new OracleCommand(proc, conn))

      cmd.CommandType = CommandType.StoredProcedure;

      cmd.Parameters.Add(new OracleParameter("p_add", OracleDbType.RefCursor, ParameterDirection.Output));
      cmd.Parameters.Add(new OracleParameter("p_delete", OracleDbType.RefCursor, ParameterDirection.Output));

      using (var rdr = cmd.ExecuteReader())

      while (rdr.Read())

      result["Adds"].Add(rdr.GetString(0));

      rdr.NextResult();
      while (rdr.Read())

      result["Deletes"].Add(rdr.GetString(0));




      conn.Close();

      return result;



      Obviously some things are a matter of taste, like whether to declare the parameters on a separate line and whether to keep the initializers there for the dictionary. (I thought it was more clear to separate them.)






      share|improve this answer

























        up vote
        2
        down vote



        accepted







        up vote
        2
        down vote



        accepted






        I could see using it for the connection, command and reader since they implement IDisposable and the documentation for them does say to always call Dispose().



        For the parameters it seems not only unnecessary but in my opinion it really decreases readability. I would kill the using statements for the parameters and make them simple declarations.



        For what it's worth, here's how I'd write it.



        private static Dictionary<string, List<string>> GetSubscriptionKeyList(string proc)


        var result = new Dictionary<string, List<string>>();
        result.Add("Adds", new List<string>());
        result.Add("Deletes", new List<string>());

        using (var conn = new OracleConnection(conStr))

        conn.Open();
        using (var cmd = new OracleCommand(proc, conn))

        cmd.CommandType = CommandType.StoredProcedure;

        cmd.Parameters.Add(new OracleParameter("p_add", OracleDbType.RefCursor, ParameterDirection.Output));
        cmd.Parameters.Add(new OracleParameter("p_delete", OracleDbType.RefCursor, ParameterDirection.Output));

        using (var rdr = cmd.ExecuteReader())

        while (rdr.Read())

        result["Adds"].Add(rdr.GetString(0));

        rdr.NextResult();
        while (rdr.Read())

        result["Deletes"].Add(rdr.GetString(0));




        conn.Close();

        return result;



        Obviously some things are a matter of taste, like whether to declare the parameters on a separate line and whether to keep the initializers there for the dictionary. (I thought it was more clear to separate them.)






        share|improve this answer















        I could see using it for the connection, command and reader since they implement IDisposable and the documentation for them does say to always call Dispose().



        For the parameters it seems not only unnecessary but in my opinion it really decreases readability. I would kill the using statements for the parameters and make them simple declarations.



        For what it's worth, here's how I'd write it.



        private static Dictionary<string, List<string>> GetSubscriptionKeyList(string proc)


        var result = new Dictionary<string, List<string>>();
        result.Add("Adds", new List<string>());
        result.Add("Deletes", new List<string>());

        using (var conn = new OracleConnection(conStr))

        conn.Open();
        using (var cmd = new OracleCommand(proc, conn))

        cmd.CommandType = CommandType.StoredProcedure;

        cmd.Parameters.Add(new OracleParameter("p_add", OracleDbType.RefCursor, ParameterDirection.Output));
        cmd.Parameters.Add(new OracleParameter("p_delete", OracleDbType.RefCursor, ParameterDirection.Output));

        using (var rdr = cmd.ExecuteReader())

        while (rdr.Read())

        result["Adds"].Add(rdr.GetString(0));

        rdr.NextResult();
        while (rdr.Read())

        result["Deletes"].Add(rdr.GetString(0));




        conn.Close();

        return result;



        Obviously some things are a matter of taste, like whether to declare the parameters on a separate line and whether to keep the initializers there for the dictionary. (I thought it was more clear to separate them.)







        share|improve this answer















        share|improve this answer



        share|improve this answer








        edited May 22 at 17:27


























        answered May 22 at 16:00









        Eric H

        1536




        1536






















            up vote
            1
            down vote














            using (var rdr = cmd.ExecuteReader())

            while (rdr.Read())

            addResults.Add(rdr.GetString(0));

            rdr.NextResult();
            while (rdr.Read())

            deleteResults.Add(rdr.GetString(0));





            I don't think it's such a good idea to rely on the result order of your stored procedure.



            You assume that the first row is always adds and the second row is deletes. I find it'd be better if you included this in the result themself and then created a dictionary based on the value in that specific column.






            share|improve this answer





















            • I agree this looks unusual. I will look into getting this changed.
              – ADH
              May 23 at 12:09














            up vote
            1
            down vote














            using (var rdr = cmd.ExecuteReader())

            while (rdr.Read())

            addResults.Add(rdr.GetString(0));

            rdr.NextResult();
            while (rdr.Read())

            deleteResults.Add(rdr.GetString(0));





            I don't think it's such a good idea to rely on the result order of your stored procedure.



            You assume that the first row is always adds and the second row is deletes. I find it'd be better if you included this in the result themself and then created a dictionary based on the value in that specific column.






            share|improve this answer





















            • I agree this looks unusual. I will look into getting this changed.
              – ADH
              May 23 at 12:09












            up vote
            1
            down vote










            up vote
            1
            down vote










            using (var rdr = cmd.ExecuteReader())

            while (rdr.Read())

            addResults.Add(rdr.GetString(0));

            rdr.NextResult();
            while (rdr.Read())

            deleteResults.Add(rdr.GetString(0));





            I don't think it's such a good idea to rely on the result order of your stored procedure.



            You assume that the first row is always adds and the second row is deletes. I find it'd be better if you included this in the result themself and then created a dictionary based on the value in that specific column.






            share|improve this answer














            using (var rdr = cmd.ExecuteReader())

            while (rdr.Read())

            addResults.Add(rdr.GetString(0));

            rdr.NextResult();
            while (rdr.Read())

            deleteResults.Add(rdr.GetString(0));





            I don't think it's such a good idea to rely on the result order of your stored procedure.



            You assume that the first row is always adds and the second row is deletes. I find it'd be better if you included this in the result themself and then created a dictionary based on the value in that specific column.







            share|improve this answer













            share|improve this answer



            share|improve this answer











            answered May 23 at 6:36









            t3chb0t

            31.9k54195




            31.9k54195











            • I agree this looks unusual. I will look into getting this changed.
              – ADH
              May 23 at 12:09
















            • I agree this looks unusual. I will look into getting this changed.
              – ADH
              May 23 at 12:09















            I agree this looks unusual. I will look into getting this changed.
            – ADH
            May 23 at 12:09




            I agree this looks unusual. I will look into getting this changed.
            – ADH
            May 23 at 12:09












             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f194954%2fcalling-an-oracle-stored-procedure-with-c-code%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            Greedy Best First Search implementation in Rust

            Function to Return a JSON Like Objects Using VBA Collections and Arrays

            C++11 CLH Lock Implementation