Show / Hide Table of Contents

    IAdoDbConnection Interface

    .NET Standard 2.x

    Represents a connection to the database.

    Namespace: SnapObjects.Data

    Assembly: SnapObjects.Data.dll

    Syntax

       public interface IAdoDbConnection : IAdoDbTransactionManager, IDisposable;
    

    Property

    Name Returns Type Description
    ConnectionString string Gets the GUID of this IAdoDbConnection object.
    ConnectionId Guid Gets the connection string used to open the database.
    DbConnection DbConnection Gets an IAdoDbTransaction object which represents the current database transaction.
    CurrentTransaction IAdoDbTransaction Gets the internal DbConnection object.

    Method

    Name Returns Type Description
    Close() bool Closes the connection to the database.
    Open() bool Opens the database connection using the connection string.

    Examples

    The following code example demonstrates how to use the methods and properties of the IAdoDbConnection object.

    using SnapObjects.Data;
    using SnapObjects.Data.SqlServer;
    using Microsoft.Extensions.Configuration;
    using System;
    using System.Data.Common;
    using System.IO;
    
    namespace Appeon.ApiDoc.IAdoDbConnectionExamples
    {
        public class IAdoDbConnectionExample
        {
            // Declares an IAdoDbConnection object
            public IAdoDbConnection _connection;
    
            // Instantiates the IAdoDbConnection object
            public IAdoDbConnectionExample()
            {
                // Gets the file location of appsettings.json
                string path = Path.Combine(Directory.GetCurrentDirectory(),
                    "appsettings.json");
    
                // Creates a ConfigurationBuilder object and adds the JSON file
                var build = new ConfigurationBuilder();
                build.AddJsonFile(path);
    
                // Builds the IConfiguration object
                var Configuration = build.Build();
    
                // Gets the configuration information of school 
                // via IConfiguration object
                string schoolconnect = Configuration.GetConnectionString("school");
    
                // Connects to the database using the configuraiton information
                _connection = new SqlServerAdoDbConnection(schoolconnect);
            }
    
            public void Example()
            {
                // Opens the database connection
                _connection.Open();
    
                // Gets and shows the ConnectionId
                var id = _connection.ConnectionId;
                Console.WriteLine("ConnectionId = {0}", id);
    
                // Gets and shows the ConnectionString
                string connString = _connection.ConnectionString;
                Console.WriteLine("ConnectionString = {0}", connString);
    
                // Starts the database transaction
                _connection.BeginTransaction();
    
                // Gets and shows the current transaction ID
                var transaction = _connection.CurrentTransaction;
                Console.WriteLine("CurrentTransaction = {0}", transaction.TransactionId);
    
                // closes the database connection
                transaction.Rollback();
                _connection.Close();
    
    
                // The following code example creates a DbCommand object using the 
                // AdoDbConnection DbConnection property, and performs database query
                _connection.Open();
    
                var command = _connection.DbConnection.CreateCommand();
                command.CommandText = "Select Name From Department";
    
                // Performs data query
                var reader = command.ExecuteReader();
    
                while (reader.Read())
                {
                    Console.WriteLine(reader.GetString(0));
                }
    
                // Releases resource and closes the database connection
                _connection.Close();
    
                /*This code produces the following output:
                 
                ConnectionId = f7b4bbd4-af87-41a6-9fcb-5c57b73a67fc
                ConnectionString = Data Source=172.16.3.122;Initial Catalog=School;Integrated Security=False;User ID=dba;Password=sql;Pooling=True;Min Pool Size=0;Max Pool Size=100;ApplicationIntent=ReadWrite
                CurrentTransaction = 031655e8-b94a-4558-bfac-96c7519f54c7
                Engineering
                English
                Economics
                Mathematics
                */
            }
        }
    }
    
    Back to top Generated by Appeon