Exemplo n.º 1
0
        static void ConnectionStringSettings_Connection()
        {
            ConnectionStringSettings database = ConfigurationManager.ConnectionStrings["MyDatabase"];

            // run a query right off the connection (this performs an auto-open/close)
            database.Connection().QuerySql("SELECT * FROM Beer", Parameters.Empty);
        }
        /// <summary>
        /// Creates and returns a new SqlConnection.
        /// </summary>
        /// <param name="settings">The ConnectionStringSettings containing the connection string.</param>
        /// <returns>A closed SqlConnection.</returns>
        public static ReliableConnection ReliableConnection(this ConnectionStringSettings settings)
        {
            if (settings == null)
            {
                throw new ArgumentNullException("settings", "ConnectionStringSettings cannot be null");
            }

            return(new ReliableConnection(settings.Connection()));
        }
        public void ConnectionShouldThrowArgumentNullExceptionOnNull()
        {
            ConnectionStringSettings settings = null;

            Assert.Throws <ArgumentNullException>(() => settings.Connection());
        }
Exemplo n.º 4
0
 /// <summary>
 /// Opens and returns a database connection.
 /// </summary>
 /// <param name="settings">The connection string to open and return.</param>
 /// <returns>The opened connection.</returns>
 public static DbConnection Open(this ConnectionStringSettings settings)
 {
     return(settings.Connection().OpenConnection());
 }
Exemplo n.º 5
0
        /// <summary>
        /// Creates and returns a new multi-threaded connection implementing the given interface.
        /// The object can support making multiple calls at the same time.
        /// </summary>
        /// <typeparam name="T">The interface to implement on the connection.</typeparam>
        /// <param name="settings">The ConnectionStringSettings containing the connection string.</param>
        /// <returns>A closed connection that implements the given interface.</returns>
        public static T AsParallel <T>(this ConnectionStringSettings settings) where T : class
        {
            Func <IDbConnection> constructor = (() => settings.Connection());

            return(constructor.AsParallel <T>());
        }
Exemplo n.º 6
0
 /// <summary>
 /// Creates and returns a new DbConnection for the connection string and implments the given interface.
 /// </summary>
 /// <typeparam name="T">The interface to implement on the connection.</typeparam>
 /// <param name="settings">The ConnectionStringSettings containing the connection string.</param>
 /// <returns>A closed connection.</returns>
 public static T As <T>(this ConnectionStringSettings settings) where T : class
 {
     return(settings.Connection().As <T>());
 }
Exemplo n.º 7
0
 /// <summary>
 /// Converts the connection to a connection that can be invoked dynamically to return lists of type T.
 /// </summary>
 /// <param name="settings">The connection to use.</param>
 /// <typeparam name="T">The type of object to return from queries.</typeparam>
 /// <returns>A DynamicConnection using the given connection.</returns>
 public static dynamic Dynamic <T>(this ConnectionStringSettings settings)
 {
     return(settings.Connection().Dynamic <T>());
 }
Exemplo n.º 8
0
 /// <summary>
 /// Asynchronously opens a database connection implementing a given interface, and begins a new transaction that is disposed when the returned object is disposed.
 /// </summary>
 /// <typeparam name="T">The interface to implement.</typeparam>
 /// <param name="settings">The settings for the connection.</param>
 /// <param name="cancellationToken">The cancellation token to use for the operation.</param>
 /// <returns>A task returning a connection when the connection has been opened.</returns>
 public static Task <T> OpenWithTransactionAsAsync <T>(this ConnectionStringSettings settings, CancellationToken?cancellationToken = null) where T : class, IDbConnection, IDbTransaction
 {
     return(settings.Connection().OpenWithTransactionAsAsync <T>(cancellationToken));
 }
Exemplo n.º 9
0
 /// <summary>
 /// Opens a database connection implementing a given interface and begins a new transaction that is disposed when the returned object is disposed.
 /// </summary>
 /// <typeparam name="T">The interface to implement.</typeparam>
 /// <param name="settings">The settings for the connection.</param>
 /// <returns>A wrapper for the database connection.</returns>
 public static T OpenWithTransactionAs <T>(this ConnectionStringSettings settings) where T : class, IDbConnection, IDbTransaction
 {
     return(settings.Connection().OpenWithTransactionAs <T>());
 }
Exemplo n.º 10
0
 /// <summary>
 /// Asynchronously opens a database connection and begins a new transaction that is disposed when the returned object is disposed.
 /// </summary>
 /// <param name="settings">The settings for the connection.</param>
 /// <param name="cancellationToken">The cancellation token to use for the operation.</param>
 /// <returns>A task returning a connection when the connection has been opened.</returns>
 public static Task <DbConnectionWrapper> OpenWithTransactionAsync(this ConnectionStringSettings settings, CancellationToken?cancellationToken = null)
 {
     return(settings.Connection().OpenWithTransactionAsync(cancellationToken));
 }
Exemplo n.º 11
0
 /// <summary>
 /// Opens a database connection and begins a new transaction that is disposed when the returned object is disposed.
 /// </summary>
 /// <param name="settings">The settings for the connection.</param>
 /// <returns>A wrapper for the database connection.</returns>
 public static DbConnectionWrapper OpenWithTransaction(this ConnectionStringSettings settings)
 {
     return(settings.Connection().OpenWithTransaction());
 }
        public void ConnectionShouldThrowArgumentNullExceptionOnNull()
        {
            ConnectionStringSettings settings = null;

            settings.Connection();
        }
Exemplo n.º 13
0
        static void Execute()
        {
            Beer beer = new Beer()
            {
                Name = "IPA"
            };

            // map a beer the stored procedure parameters
            Database.Connection().Insert("InsertBeer", beer);

            // map an anonymous object to the stored procedure parameters
            Database.Connection().Execute("DeleteBeer", beer);
        }
 /// <summary>
 /// Asynchronously opens and returns a database connection implementing a given interface.
 /// </summary>
 /// <typeparam name="T">The interface to implmement.</typeparam>
 /// <param name="settings">The connection string to open and return.</param>
 /// <param name="cancellationToken">The cancellation token to use for the operation.</param>
 /// <returns>The opened connection.</returns>
 public static Task <T> OpenAsAsync <T>(this ConnectionStringSettings settings, CancellationToken cancellationToken = default(CancellationToken)) where T : class, IDbConnection
 {
     return(settings.Connection().OpenAsAsync <T>(cancellationToken));
 }
 /// <summary>
 /// Opens and returns a database connection.
 /// </summary>
 /// <param name="settings">The connection string to open and return.</param>
 /// <param name="cancellationToken">The cancellation token to use for the operation.</param>
 /// <returns>The opened connection.</returns>
 public static Task <DbConnection> OpenAsync(this ConnectionStringSettings settings, CancellationToken cancellationToken = default(CancellationToken))
 {
     return(settings.Connection().OpenConnectionAsync(cancellationToken));
 }