Table of Contents

Class DatabaseConnectionProvider

Namespace
Dynamicweb.Data
Assembly
Dynamicweb.Core.dll

Represents the database connection provider for replace the logic of creating database connections and data adapters to the Dynamicweb database.

public class DatabaseConnectionProvider : IDatabaseConnectionProvider
Inheritance
DatabaseConnectionProvider
Implements
Inherited Members

Methods

CreateAdapter()

Creates a DataAdapter based on the database type of the solution.

public virtual IDbDataAdapter CreateAdapter()

Returns

IDbDataAdapter

A SqlDataAdapter

Examples

using System.Data;

namespace Dynamicweb.Data.Examples;

public static class DatabaseCreateConnection
{
    public static void ConnectToDatabase()
    {
        //Create a connection to default database
        using (var myConnection = Database.CreateConnection())
        {
            //Create a command object from the connection 
            using (var myCommand = myConnection.CreateCommand())
            {
                //Create a DataAdapter
                var daAdapter = Database.CreateAdapter();

                //Prepare command object
                myCommand.CommandText = "SELECT TOP 1 * FROM Page";
                daAdapter.SelectCommand = myCommand;

                //Fill a dataset
                using var myDataSet = new DataSet();
                daAdapter.Fill(myDataSet);
            }
        }

        //Create a connection to another database in /Database folder when running Access
        using (var myConnection = Database.CreateConnection())
        {
            //Do stuff witht the connection
        }

        using var con = (System.Data.SqlClient.SqlConnection)Database.CreateConnection();
    }
}

CreateConnection()

Creates and opens a database connection to the specified database.

public virtual IDbConnection CreateConnection()

Returns

IDbConnection

A Database Connection (a IDbConnection object). The connection is connected to the SQL Server database specified in database setup (/Files/GlobalSettings.aspx).

Examples

using System.Data;

namespace Dynamicweb.Data.Examples;

public static class DatabaseCreateConnection
{
    public static void ConnectToDatabase()
    {
        //Create a connection to default database
        using (var myConnection = Database.CreateConnection())
        {
            //Create a command object from the connection 
            using (var myCommand = myConnection.CreateCommand())
            {
                //Create a DataAdapter
                var daAdapter = Database.CreateAdapter();

                //Prepare command object
                myCommand.CommandText = "SELECT TOP 1 * FROM Page";
                daAdapter.SelectCommand = myCommand;

                //Fill a dataset
                using var myDataSet = new DataSet();
                daAdapter.Fill(myDataSet);
            }
        }

        //Create a connection to another database in /Database folder when running Access
        using (var myConnection = Database.CreateConnection())
        {
            //Do stuff witht the connection
        }

        using var con = (System.Data.SqlClient.SqlConnection)Database.CreateConnection();
    }
}

Remarks

The returned IDbConnection instance has to be a SqlConnection.

To top