/// <summary> /// Execute a SqlCommand (that returns a 1x1 resultset and takes no parameters) against the database specified in /// the connection string. /// </summary> /// <param name="context">the current context/</param> /// <param name="commandType">the CommandType (stored procedure, text, etc.)</param> /// <param name="commandText">the stored procedure name or T-SQL command</param> /// <param name="commandParameters">an array of SqlParamters used to execute the command</param> /// <returns>an object containing the value in the 1x1 resultset generated by the command</returns> public static object ExecuteScalar(SqlContext context, CommandType commandType, string commandText, params SqlParameter[] commandParameters) { if(context.Transaction!=null) return ExecuteScalar(context.Transaction, commandType, commandText, commandParameters); else return ExecuteScalar(context.ConnectionString, commandType, commandText, commandParameters); }
/// <summary> /// Execute a SqlCommand (that returns no resultset and takes no parameters) against the database specified in /// the connection string. /// </summary> /// <param name="context">The current context.</param> /// <param name="commandType">the CommandType (stored procedure, text, etc.)</param> /// <param name="commandText">the stored procedure name or T-SQL command</param> /// <returns>an int representing the number of rows affected by the command</returns> public static int ExecuteNonQuery(SqlContext context, CommandType commandType, string commandText) { //pass through the call providing null for the set of SqlParameters return ExecuteNonQuery(context, commandType, commandText, (SqlParameter[])null); }
/// <summary> /// Create and prepare a SqlCommand, and call ExecuteReader with the appropriate CommandBehavior. /// </summary> /// <param name="context">the current context.</param> /// <param name="commandType">the CommandType (stored procedure, text, etc.)</param> /// <param name="commandText">the stored procedure name or T-SQL command</param> /// <returns>SqlDataReader containing the results of the command</returns> public static SqlDataReader ExecuteReader(SqlContext context, CommandType commandType, string commandText) { return ExecuteReader(context, commandType, commandText, (SqlParameter[])null); }
/// <summary> /// Executes a complex sql script. /// </summary> /// <param name="context">The current context.</param> /// <param name="Script">A sql script.</param> public static void ExecuteScript(SqlContext context, string Script) { #if DEBUG System.Diagnostics.Trace.WriteLine(Script,"ExecuteScript"); #endif using(System.IO.StringReader reader = new System.IO.StringReader(Script)) { bool bContueRead = true; StringBuilder sqlCommandText = new StringBuilder(); while(bContueRead) { while(true) { string line = reader.ReadLine(); if(line==null) { bContueRead = false; break; } string trimLine = line.Trim(); if(trimLine!=string.Empty) { if(string.Compare(trimLine,"GO",true)==0) break; else { sqlCommandText.Append(line); sqlCommandText.Append("\r\n"); } } } if(sqlCommandText.Length>0) { SqlHelper.ExecuteNonQuery(context,CommandType.Text,sqlCommandText.ToString()); sqlCommandText = new StringBuilder(); } } } }