Calling an Oracle Stored Procedure with C# Code
Clash 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;
c# oracle
add a comment |Â
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;
c# oracle
What framework/nuget are you using? I cannot find theOracleDbType
property on theOracleParameter
. There's onlyOracleType
.
â t3chb0t
May 22 at 16:04
@t3chb0tOracle.DataAccess.Client.OracleDbType
â ADH
May 22 at 16:35
Could you add the stored procedure too?
â t3chb0t
May 22 at 17:31
add a comment |Â
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;
c# oracle
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;
c# oracle
asked May 22 at 15:31
ADH
20829
20829
What framework/nuget are you using? I cannot find theOracleDbType
property on theOracleParameter
. There's onlyOracleType
.
â t3chb0t
May 22 at 16:04
@t3chb0tOracle.DataAccess.Client.OracleDbType
â ADH
May 22 at 16:35
Could you add the stored procedure too?
â t3chb0t
May 22 at 17:31
add a comment |Â
What framework/nuget are you using? I cannot find theOracleDbType
property on theOracleParameter
. There's onlyOracleType
.
â t3chb0t
May 22 at 16:04
@t3chb0tOracle.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
add a comment |Â
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.)
add a comment |Â
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.
I agree this looks unusual. I will look into getting this changed.
â ADH
May 23 at 12:09
add a comment |Â
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.)
add a comment |Â
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.)
add a comment |Â
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.)
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.)
edited May 22 at 17:27
answered May 22 at 16:00
Eric H
1536
1536
add a comment |Â
add a comment |Â
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.
I agree this looks unusual. I will look into getting this changed.
â ADH
May 23 at 12:09
add a comment |Â
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.
I agree this looks unusual. I will look into getting this changed.
â ADH
May 23 at 12:09
add a comment |Â
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.
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.
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
add a comment |Â
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
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
What framework/nuget are you using? I cannot find the
OracleDbType
property on theOracleParameter
. There's onlyOracleType
.â 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