I made a generic function to call stored procedures from C#. The code looks like this:
public static void executeStoredProcedure(string SPName, Dictionary<string, object> parameters, string connectionStringName)
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand(SPName, con))
{
cmd.CommandType = CommandType.StoredProcedure;
if (parameters != null)
{
foreach (KeyValuePair<string, object> kvp in parameters)
cmd.Parameters.Add(new SqlParameter(kvp.Key, kvp.Value));
}
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
And the code when I use it goes like this:
Dictionary<string, object> parameters = new Dictionary<string, object>();
parameters.Add("UserId", UserId);
parameters.Add("UserName", UserName);
GlobalClass.executeStoredProcedure("UpdateUserName", parameters, "userDB");
How do I modify this to be able to work with a stored procedure that has an output parameter, and store the output in a C# variable?