Last active
June 10, 2021 13:12
-
-
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#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <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); | |
} | |
} |
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
you are a genius!!