/// <summary> /// Tries to execute an SQL statement. /// The return value indicates whether or not the statement executed successfully. /// </summary> /// <param name="aStatement">The SQL statement structure to execute</param> /// <param name="aResult">The resulting structure that contains the return value /// and an exception object if the SQL statement failed to execute properly</param> /// <param name="aCloseConnection">If true, closes the connection after execution</param> /// <param name="aConnection">The connection to use for this SQL statement</param> /// <typeparam name="T">The data type the return value should be</typeparam> /// <returns>A boolean value that indicates whether or not the Sql Statement was executed successfully</returns> /// <seealso cref="Sql.Statement"/> /// <seealso cref="System.Data.SqlClient.SqlConnection"/> /// <seealso cref="Items.Results.SqlResultWithValue{T}"/> public Boolean TryExecute <T>(Statement aStatement, SqlConnection aConnection, Boolean aCloseConnection, out SqlResultWithValue <T> aResult) { return(TryExecute <T>(aStatement, aConnection, aCloseConnection, null, out aResult)); }
/// <summary> /// Tries to execute an SQL statement. /// The return value indicates whether or not the statement executed successfully. /// </summary> /// <param name="aStatement">The SQL statement structure to execute</param> /// <param name="aResult">The resulting structure that contains the return value /// and an exception object if the SQL statement failed to execute properly</param> /// <typeparam name="T">The data type the return value should be</typeparam> /// <returns>A boolean value that indicates whether or not the Sql Statement was executed successfully</returns> /// <seealso cref="Sql.Statement"/> /// <seealso cref="Items.Results.SqlResultWithValue{T}"/> public Boolean TryExecute <T>(Statement aStatement, out SqlResultWithValue <T> aResult) { return(TryExecute <T> ( aStatement, CreateConnection(), out aResult )); }
/// <summary> /// Tries to execute an SQL statement. /// The return value contains whether or not the statement executed successfully. /// </summary> /// <param name="aResult">The resulting structure that contains the return value /// and an exception object if the SQL statement failed to execute properly</param> /// <param name="aSqlStatement">The SQL statement to execute</param> /// <param name="aCommandType">The type of SQL statement being passed</param> /// <param name="aParameters">A list of parameters that will be used by the statement</param> /// <typeparam name="T">The data type the return value should be</typeparam> /// <returns>A boolean value that indicates whether or not the Sql Statement was executed successfully</returns> /// <seealso cref="System.Data.CommandType"/> /// <seealso cref="System.Data.SqlClient.SqlParameter"/> /// <seealso cref="System.Collections.Generic.IEnumerable{T}"/> /// <seealso cref="Items.Results.SqlResultWithValue{T}"/> public Boolean TryExecute <T>(String aSqlStatement, CommandType aCommandType, out SqlResultWithValue <T> aResult, params SqlParameter[] aParameters) { return(TryExecute <T> ( aSqlStatement, aCommandType, aParameters == null ? null : aParameters.AsEnumerable(), out aResult )); }
/// <summary> /// Tries to execute an SQL statement. /// The return value contains whether or not the statement executed successfully. /// </summary> /// <param name="aResult">The resulting structure that contains the return value /// and an exception object if the SQL statement failed to execute properly</param> /// <param name="aSqlStatement">The SQL statement to execute</param> /// <param name="aCommandType">The type of SQL statement being passed</param> /// <param name="aParameters">A list of parameters that will be used by the statement</param> /// <typeparam name="T">The data type the return value should be</typeparam> /// <returns>A boolean value that indicates whether or not the Sql Statement was executed successfully</returns> /// <seealso cref="System.Data.CommandType"/> /// <seealso cref="System.Data.SqlClient.SqlParameter"/> /// <seealso cref="System.Collections.Generic.IEnumerable{T}"/> /// <seealso cref="Items.Results.SqlResultWithValue{T}"/> public Boolean TryExecute <T>(String aSqlStatement, CommandType aCommandType, IEnumerable <SqlParameter> aParameters, out SqlResultWithValue <T> aResult) { return(TryExecute <T> ( new Statement { Sql = aSqlStatement, Type = aCommandType, Parameters = aParameters }, out aResult )); }
/// <summary> /// Tries to execute an SQL statement. /// The return value indicates whether or not the statement executed successfully. /// </summary> /// <param name="aStatement">The SQL statement structure to execute</param> /// <param name="aResult">The resulting structure that contains the return value /// and an exception object if the SQL statement failed to execute properly</param> /// <param name="aCloseConnection">If true, closes the connection after execution</param> /// <param name="aTransaction">The SqlTransaction object to use during execution</param> /// <param name="aConnection">The connection to use for this SQL statement</param> /// <typeparam name="T">The data type the return value should be</typeparam> /// <returns>A boolean value that indicates whether or not the Sql Statement was executed successfully</returns> /// <seealso cref="Sql.Statement"/> /// <seealso cref="System.Data.SqlClient.SqlConnection"/> /// <seealso cref="Items.Results.SqlResultWithValue{T}"/> public Boolean TryExecute <T>(Statement aStatement, SqlConnection aConnection, Boolean aCloseConnection, SqlTransaction aTransaction, out SqlResultWithValue <T> aResult) { // Declare the defaulted value aResult = new SqlResultWithValue <T>(); aResult.Value = default(T); try { // Execute the query to get the proper return value aResult = Execute <T>(aStatement, aConnection, aCloseConnection, aTransaction); } catch (Exception e) { // Some error happened aResult.Exception = e; } // Return return(aResult.Success); }
/// <summary> /// Performs an asynchronous query with a single return value against a database /// </summary> /// <typeparam name="T">The return type expected as a result of the query</typeparam> /// <param name="aStatement">The SQL statement structure to execute</param> /// <param name="aConnection">The connection to use for this SQL statement</param> /// <param name="aCloseConnection">A boolean flag that indicates whether or not the /// connection should be closed after the statement is executed</param> /// <param name="aTransaction">The current transaction that this command should be executed on</param> /// <returns>A Task object that represents the asynchronous request</returns> /// <seealso cref="System.Threading.Tasks.Task{T}"/> /// <seealso cref="SqlMagic.Monitor.Items.Results.SqlResultWithValue{T}"/> /// <seealso cref="namespace SqlMagic.Sql.Statement"/> /// <seealso cref="System.Data.SqlClient.SqlConnection"/> /// <seealso cref="System.Data.SqlClient.SqlTransaction"/> public async Task <SqlResultWithValue <T> > ExecuteAsync <T>(Statement aStatement, SqlConnection aConnection, Boolean aCloseConnection, SqlTransaction aTransaction) { // Make a call to OpenAsync to return a dataset. SqlResultWithDataSet oDataSet = await OpenAsync(aStatement, aConnection, aCloseConnection, aTransaction); // Create a result w/ value opbject SqlResultWithValue <T> oResult = new SqlResultWithValue <T>(); // Assign stuff manually oResult.Exception = oDataSet.Exception; oResult.Statistics = oDataSet.Statistics; // Check for success+actual results if (oDataSet.Success && oDataSet.Results.Tables[0].Rows.Count > 0) { oResult.Value = (T)Convert.ChangeType(oDataSet.Results.Tables[0].Rows[0][0], typeof(T)); } // All done return(oResult); }
/// <summary> /// Tries to execute an SQL statement. /// The return value indicates whether or not the statement executed successfully. /// </summary> /// <param name="aResult">The resulting structure that contains the return value /// and an exception object if the SQL statement failed to execute properly</param> /// <param name="aSqlStatement">The SQL statement to execute</param> /// <param name="aCommandType">The type of SQL statement being passed</param> /// <typeparam name="T">The data type the return value should be</typeparam> /// <returns>A boolean value that indicates whether or not the Sql Statement was executed successfully</returns> /// <seealso cref="System.Data.CommandType"/> /// <seealso cref="Items.Results.SqlResultWithValue{T}"/> public Boolean TryExecute <T>(String aSqlStatement, CommandType aCommandType, out SqlResultWithValue <T> aResult) { return(TryExecute <T>(aSqlStatement, aCommandType, null, out aResult)); }