Skip to content

Instantly share code, notes, and snippets.

@thorsman99
Last active June 10, 2021 13:12
Show Gist options
  • Save thorsman99/e788dd9cce36c26edd9076c9dac288dd to your computer and use it in GitHub Desktop.
Save thorsman99/e788dd9cce36c26edd9076c9dac288dd to your computer and use it in GitHub Desktop.
C# execute a non-query SQL command with a dictionary of parameters #C#
/// <summary>
/// Executes a non query SQL command with a Dictionary Object of Parameters
/// </summary>
/// <param name="strSQL"></param>
/// <param name="colParams"></param>
/// <returns></returns>
private void ExecuteNonQuery(string strSQL, Dictionary<string, string> dctParams)
{
try
{
using (SqlConnection sqlcn = new SqlConnection(strDSN))
{
using (SqlCommand sqlCMD = new SqlCommand(strSQL, sqlcn))
{
using (SqlDataAdapter sqlDA = new SqlDataAdapter(sqlCMD))
{
sqlCMD.CommandType = CommandType.StoredProcedure;
sqlCMD.Parameters.Clear();
foreach (KeyValuePair<string, string> kvp in dctParams)
{
sqlCMD.Parameters.AddWithValue(kvp.Key, kvp.Value);
}
sqlcn.Open();
sqlCMD.ExecuteNonQuery();
}
}
}
}
catch (Exception Ex)
{
//Common.LogError(Ex, this.GetType().BaseType.Name + " " + System.Reflection.MethodBase.GetCurrentMethod().Name, Convert.ToInt32(User.Identity.Name), this.Response);
}
}
@mtnsosa
Copy link

mtnsosa commented Mar 29, 2017

you are a genius!!

@hypotermia
Copy link

excuse me sir ,
i can use it on Net Core 2.2?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment