daily pastebin goal
71%
SHARE
TWEET

Untitled

a guest Jan 18th, 2019 64 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /// <summary>
  2.     /// This method internally uses the ExecuteNonQuery method of the SqlCommand object
  3.     /// to run a query that returns no values (updates/inserts/deletes).
  4.     /// </summary>
  5.     /// <param name="sql">The SQL to execute</param>
  6.     /// <param name="parameters">The SqlParameters to use (pass null if there are none)</param>
  7.     /// <remarks>This method is thread safe.</remarks>
  8.     public static void runNonQuery(string sql, IList<SqlParameter> parameters, string dbConnectionString)
  9.     {
  10.         // Old-school, using simple ADO.Net to run a non-query (updates/inserts/deletes) - KDR
  11.         try
  12.         {
  13.             Monitor.Enter(_lock);
  14.             using (SqlConnection conn = new SqlConnection(dbConnectionString))
  15.             {
  16.                 using (System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand())
  17.                 {
  18.                     conn.Open();
  19.                     cmd.Connection = conn;
  20.                     cmd.CommandType = CommandType.Text;
  21.                     cmd.CommandText = sql;
  22.  
  23.                     if (parameters != null && parameters.Count() != 0)
  24.                     {
  25.                         foreach (SqlParameter param in parameters)
  26.                         {
  27.                             cmd.Parameters.Add(param);
  28.                         }
  29.                     }
  30.                     cmd.ExecuteNonQuery();
  31.                 }
  32.                 if (conn.State != ConnectionState.Closed) { conn.Close(); }
  33.             }
  34.         }
  35.         finally
  36.         {
  37.             Monitor.Exit(_lock);
  38.         }
  39.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top