Dynamicweb 8 Documentation
CreateConnection(String) Method
Example 

A string holding the Name of the Database (i.e. "Dynamic.mdb"). Only used when connecting to a MS Access database. If the solution is running SQL-Server, this parameter is ignored.
Creates and opens a database connection to the specified database.
Syntax
'Declaration
 
Public Overloads Shared Function CreateConnection( _ 
   ByVal database As String _ 
) As IDbConnection
public static IDbConnection CreateConnection( 
   string database 
)

Parameters

database
A string holding the Name of the Database (i.e. "Dynamic.mdb"). Only used when connecting to a MS Access database. If the solution is running SQL-Server, this parameter is ignored.

Return Value

A Database Connection (a IDbConnection object). The connection is connected to /Database/Dynamic.mdb if the solution runs on MS Access or to the SQL Server database specified in database setup (/Files/GlobalSettings.aspx) if the solution runs on MS SQL-Server.
Remarks
The returned IDbConnection instance is either a System.Data.SqlClient.SqlConnection or a System.Data.OleDb.OleDbConnection depending on the database type.
Example
Full database exampleFull database example
using System.Data;

namespace Dynamicweb.Examples.CSharp
{
    public class DatabaseCreateConnection
    {

        public 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
                    var myDataSet = new DataSet();
                    daAdapter.Fill(myDataSet);
                }
            }

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

            //Check the database type and change the connection object to the database specific type returned.
            if (Database.IsAccess)
            {
                var con = (System.Data.OleDb.OleDbConnection)Database.CreateConnection();
            }
            else
            {
                var con = (System.Data.SqlClient.SqlConnection)Database.CreateConnection();
            }
        }
    }

}
Public Class DatabaseCreateConnection
    Public Sub ConnectToDatabase()

        'Create a connection to default database
        Using myConnection As IDbConnection = Database.CreateConnection()
            'Create a command object from the connection 
            Using myCommand As System.Data.IDbCommand = myConnection.CreateCommand
                'Create a DataAdapter
                Dim daAdapter As IDbDataAdapter = Database.CreateAdapter()

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

                'Fill a dataset
                Dim myDataSet As DataSet = New DataSet
                daAdapter.Fill(myDataSet)
            End Using
        End Using

        'Create a connection to another database in /Database folder when running Access
        Using myConnection As IDbConnection = Database.CreateConnection("Access.mdb")
            'Do stuff witht the connection
        End Using

        'Check the database type and change the connection object to the database specific type returned.
        If Database.IsAccess Then
            Dim con As OleDb.OleDbConnection = DirectCast(Database.CreateConnection(), OleDb.OleDbConnection)
        Else
            Dim con As SqlClient.SqlConnection = DirectCast(Database.CreateConnection(), SqlClient.SqlConnection)
        End If
    End Sub
End Class
Requirements

Target Platforms: Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

See Also

Reference

Database Class
Database Members
Overload List

Send Feedback