Exemplo n.º 1
0
 /// <summary>
 /// Executes an INSERT, UPDATE, or DELETE statement.
 /// </summary>
 /// <param name="sql">The SQL to execute</param>
 /// <returns>
 /// The number of rows affected by the statement
 /// </returns>
 internal int ExecuteNonQuery(string sql)
 {
     using (IDbCommandFactory c = GetCommandFactory())
         using (IDbCommand cmd = c.GetCommand(sql))
         {
             return(cmd.ExecuteNonQuery());
         }
 }
Exemplo n.º 2
0
 /// <summary>
 /// Executes a query
 /// </summary>
 /// <typeparam name="T">The type of data produced by the query.</typeparam>
 /// <param name="query">The query to be executed</param>
 /// <returns>The rows (if any) that satisfy the query</returns>
 /// <seealso cref="ExecuteSelect"/>
 internal IEnumerable <T> ExecuteQuery <T>(IDataQuery <T> query)
 {
     using (IDbCommandFactory c = GetCommandFactory())
         using (IDbCommand cmd = c.GetCommand(query.Text))
             using (IDataReader reader = cmd.ExecuteReader())
             {
                 while (reader.Read())
                 {
                     yield return(query.CreateInstance(reader));
                 }
             }
 }
Exemplo n.º 3
0
        /// <summary>
        /// Executes a query that is expected to return no more than one row.
        /// </summary>
        /// <typeparam name="T">The type of data in the query field.</typeparam>
        /// <param name="sql">The query text</param>
        /// <param name="defaultValue">The default value to be returned in cases
        /// where nothing was found, or a database null was found.</param>
        /// <returns>The selected value (or <paramref name="defaultValue"/> if not found)</returns>
        /// <remarks>There is no error if the select actually relates to multiple rows
        /// (in that case, you just get the first row that was selected).</remarks>
        /// <seealso cref="ExecuteQuery"/>
        internal T ExecuteSelect <T>(string sql, T defaultValue = default(T)) where T : IConvertible
        {
            using (IDbCommandFactory c = GetCommandFactory())
                using (IDbCommand cmd = c.GetCommand(sql))
                {
                    object o = cmd.ExecuteScalar();

                    if (o == null || o == DBNull.Value)
                    {
                        return(defaultValue);
                    }

                    return((T)Convert.ChangeType(o, typeof(T)));
                }
        }