Publish date: 2003-04-23

Database Connection methods (C#, VB.Net)

Download the example archive
 using System;
using System.Data;  
using System.Data.OleDb;  

namespace ConnectionString_Property
{
    /// <summary>
    /// this sample demonstrate how to use ConnectionStringProperty for attach to database
    /// </summary>
    class ADONetConnectionStringProperty
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            OleDbConnection con; 

            try
            {    
            //Standart connection methods
            Console.WriteLine("Standart connection methods");
            con = new OleDbConnection("Provider=LCPI.IBProvider;" + 
                                      "data source=e:\\database\\employee.gdb;" +
                                      "ctype=win1251;user id=sysdba;password=masterkey");
            con.Open();
            con.Close();
            Console.WriteLine("Standart connection methods... Done");
            Console.WriteLine("-----------------------------------");

            /*Create new Connection from UDL file (generated with Data Link Properties Editor)
              need to place udl file to bin project directory 
              Source UDL text 
              [OleDb]()
            ; Everything after this line is an OLE DB initstring
            Provider=LCPI.IBProvider.1;Password=masterkey;Persist Security Info=True;User ID=sysdba;Data Source=e:\database\employee.gdb
            */

            Console.WriteLine("Trying open connection using UDL File");
            con = new OleDbConnection("file name=employee.udl");
            con.Open();
            con.Close();
            Console.WriteLine("Open connection using UDL File... Done");
            Console.WriteLine("--------------------------------------");

            // Work in auto commit mode with read commited isolation level
            Console.WriteLine("Work in auto commit mode with read commited isolation level");
            con.ConnectionString = ("Provider=LCPI.IBProvider;" +
                                    "data source=e:\\database\\employee.gdb;" +
                                    "ctype=win1251;" +
                                    "auto_commit=true;auto_commit_level=0x1000;" +
                                    "user=sysdba;password=masterkey");
            con.Open(); 
            con.Close();

            Console.WriteLine("Work in auto commit mode with read commited isolation level... Done");
            Console.WriteLine("-------------------------------------------------------------------`");

            // Switching connection to auto commit mode
            Console.WriteLine("Switching connection to auto commit mode");
            con = new OleDbConnection("Provider=LCPI.IBProvider;" +
                                      "data source=e:\\database\\employee.gdb;"  +
                                      "ctype=win1251;" +
                                      "user=sysdba;password=masterkey" +
                                      "Session AutoCommit=true" +
                                      "Autocommit Isolation Levels= 4096"); //read commited=0x1000
            con.Open();
            con.Close();

            Console.WriteLine("Switching connection to auto commit mode.Done");
            Console.WriteLine("----------------------------------------");
    
            if (con.State  == ConnectionState.Open)
              {con.Close();}

            }    
            catch(OleDbException myOLEDBException)
            { 
                //ole db exception
                //error collection sample 
                Console.WriteLine("----------------------------------------");
                for (int i = 0; i<=myOLEDBException.Errors.Count-1;i++)
                {
                    Console.WriteLine("Message " + (i + 1) + ": " + myOLEDBException.Errors[i].Message);
                    Console.WriteLine("Native: " + myOLEDBException.Errors[i].NativeError.ToString());
                    Console.WriteLine("Source: " + myOLEDBException.Errors[i].Source);
                    Console.WriteLine("SQL: " + myOLEDBException.Errors[i].SQLState);
                    Console.WriteLine("----------------------------------------");
                }
            }
            catch (Exception myException)
            {
                //other exception
                //error collection sample
                Console.WriteLine("----------------------------------------");
                Console.WriteLine(myException.Message);
            }
    
            Console.WriteLine("All Done. Press Enter to close");
            Console.ReadLine();

        }
    }
}
Download the example archive
Output of sample.
Output.



Download the example archive
 Imports System.Data
Imports System.Data.OleDb
Imports System.Collections

Module ADONET_ConnectionStringProperty

    Sub Main()
        Dim i As Integer
        Dim con As OleDbConnection

        Try
            'Standart connection methods
            Console.WriteLine("Standart connection methods")
            con = New OleDbConnection("Provider=LCPI.IBProvider;" + _
                                      "data source=e:\database\employee.gdb;ctype=win1251;" & _
                                      "user id=sysdba;password=masterkey")
            con.Open()
            con.Close()
            Console.WriteLine("Standart connection methods... Done")
            Console.WriteLine("-----------------------------------")

            'Create new Connection from UDL file (generated with Data Link Properties Editor)
            'need to place udl file to bin project directory 
            'Source UDL text 
            '[OleDb]()
            '; Everything after this line is an OLE DB initstring
            'Provider=LCPI.IBProvider.1;Password=masterkey;Persist Security Info=True;User ID=sysdba;Data Source=e:\database\employee.gdb
            '
            Console.WriteLine("Trying open connection using UDL File")
            con = New OleDbConnection("file name=employee.udl")
            con.Open()
            con.Close()
            Console.WriteLine("Open connection using UDL File... Done")
            Console.WriteLine("--------------------------------------")

            ' Work in auto commit mode with read commited isolation level
            Console.WriteLine("Work in auto commit mode with read commited isolation level")
            con.ConnectionString = ("Provider=LCPI.IBProvider;data source=e:\database\employee.gdb;ctype=win1251;" & _
                    "auto_commit=true;auto_commit_level=0x1000;" & _
                    "user=sysdba;password=masterkey")
            con.Open()
            con.Close()

            Console.WriteLine("Work in auto commit mode with read commited isolation level... Done")
            Console.WriteLine("-------------------------------------------------------------------`")

            ' Switching connection to auto commit mode
            Console.WriteLine("Switching connection to auto commit mode")
            con = New OleDbConnection("Provider=LCPI.IBProvider;data source=e:\database\employee.gdb;ctype=win1251;" & _
                    "user=sysdba;password=masterkey" & _
                    "Session AutoCommit=true" & _
                    "Autocommit Isolation Levels= 4096") 'read commited=0x1000
            con.Open()
            con.Close()

            Console.WriteLine("Switching connection to auto commit mode... Done")
            Console.WriteLine("----------------------------------------")

        Catch myOLEDBException As OleDbException 'ole db exception
            'error collection sample 
            Console.WriteLine("----------------------------------------")
            For i = 0 To myOLEDBException.Errors.Count - 1
                Console.WriteLine("Message " & i.ToString + 1 & ": " + myOLEDBException.Errors(i).Message, True)
                Console.WriteLine("Native: " + myOLEDBException.Errors(i).NativeError.ToString())
                Console.WriteLine("Source: " + myOLEDBException.Errors(i).Source)
                Console.WriteLine("SQL: " + myOLEDBException.Errors(i).SQLState)
                Console.WriteLine("----------------------------------------")
            Next i

        Catch myException As Exception  'other exception
            'error collection sample
            Console.WriteLine("----------------------------------------")
            Console.WriteLine(myException.Message)
            Exit Sub
        End Try

        Console.WriteLine("Close connection if connection state is open", True)
        If con.State = ConnectionState.Open Then
            con.Close()
        End If

        Console.WriteLine("All Done. Press Enter to close", True)
        Console.ReadLine()

    End Sub

End Module
Download the example archive
Output of sample.
Output.