Dynamicweb Namespace > Database Class > CreateConnection Method : CreateConnection() Method |
'Declaration
Public Overloads Shared Function CreateConnection() As IDbConnection
public static IDbConnection CreateConnection()
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
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