Publish date: 2003-01-05

Connection to DataBase (VB, C++)

Sub sample1()
 Dim cn As New ADODB.Connection

 'use connection string from file "employee.ibp":
 'data source=localhost:d:\database\employee.gdb;User ID=gamer;password=vermut;
 'ctype=win1251;auto_commit=true;

 cn.Open "file name=d:\database\employee.ibp"
 cn.Close

 'Standart connection methods
 cn.Provider = "LCPI.IBProvider"
 cn.Open "data source=localhost:d:\database\employee.gdb;ctype=win1251", _
         "gamer", "vermut"
 cn.Close

 ' Using user id
 cn.Open "data source=localhost:d:\database\employee.gdb;ctype=win1251;" & _
         "user id=gamer;password=vermut"
 cn.Close

 ' Using user
 cn.Open "data source=localhost:d:\database\employee.gdb;ctype=win1251;" & _
         "user=gamer;password=vermut"
 cn.Close

 ' Work in auto commit mode with read commited isolation level
 cn.Open "data source=localhost:d:\database\employee.gdb;ctype=win1251;" & _
         "auto_commit=true;auto_commit_level=0x1000;" & _
         "user id=gamer;password=vermut"
 cn.Close

 ' Switching connection to auto commit mode
 cn.Open "data source=localhost:d:\database\employee.gdb;ctype=win1251;" & _
         "user id=gamer;password=vermut"
 cn.Properties("Session AutoCommit") = True          'enable auto commit
 cn.Properties("Autocommit Isolation Levels") = 4096 'read commited=0x1000

End Sub 'sample1

void TSampleCode1::execute()
{
 cout<<"Sample Code #1"<<endl;

 try
 {
  t_db_data_source source;

  //explicit definition
  _THROW_OLEDB_FAILED(source,create("LCPI.IBProvider"))
  _THROW_OLEDB_FAILED(source,load("employee.ibp"))
  _THROW_OLEDB_FAILED(source,attach(""))

  //From file
  _THROW_OLEDB_FAILED(source,attach("file name=employee.ibp"))

  //Use provider name and file for its initialize
  _THROW_OLEDB_FAILED(source,
   attach("provider=LCPI.IBProvider;file name=employee.ibp"))

  //separate definition of user and its password (and override default settings)
  _THROW_OLEDB_FAILED(source,
   attach("provider=LCPI.IBProvider;file name=employee.ibp;",
          "gamer","vermut"))

  //All parameters in one line
  TExtPropParser props(*ifstream("employee.ibp").rdbuf());

  str_formatter fmsg("provider=%1;data source=%2;");
  fmsg<<"LCPI.IBProvider"<<props["data source"];

  _THROW_OLEDB_FAILED(source,attach(fmsg,"gamer","vermut"))
 }
 catch(exception& exc)
 {
  cout<<"error: "<<exc.what()<<endl;
 }
}//execute