Дата публикации: 26.03.2015

Совместная работа ADO.NET, ADODB и OLE DB технологий. (C#)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
////////////////////////////////////////////////////////////////////////////////
//Samples for LCPI ADO.NET Data provider for OLEDB.
//                                                                   26.03.2015.
using System;
using System.Data;
using System.Diagnostics;
using lcpi.data.oledb;
 
using com_lib=lcpi.lib.com;
using adodb_lib=lcpi.lib.adodb;
 
namespace Sample_0021{
////////////////////////////////////////////////////////////////////////////////
//
// LCPI.IBP.Samples.IBGenManager.1
//  - Sample COM-object (writed on C++) from IBProvider installation kit.
//    Works with OLEDB provider through native OLEDB interfaces.
//
////////////////////////////////////////////////////////////////////////////////
//
// Scenario:
// - ADO.NET provider creates the connection to database
// - ADODB connection initiates the transaction
// - Sample COM object (GenManager) executes "GEN_ID" statement for generation of record identifier
// - ADODB connection commits the transaction
//
////////////////////////////////////////////////////////////////////////////////
//class Program
 
class Program
{
 private const string c_cn_str
   ="provider=LCPI.IBProvider.5;"
   +"location=localhost:d:\\database\\fb_03_0_0\\employee.fdb;"
   +"dbclient_type=fb.direct;"
   +"user id=SYSDBA;"
   +"password=masterkey;";
 
 //-----------------------------------------------------------------------
 static int Main()
 {
  int resultCode=0;
 
  dynamic adodbCn=null;
 
  dynamic genMng=null;
 
  try //[catch] [finally]
  {
   using(var oledbCn=new OleDbConnection(c_cn_str))
   {
    Console.WriteLine("[ADO.NET] Connection to database...");
 
    oledbCn.Open();
 
    //--------------------------------------
    Console.WriteLine("Creation of ADODB.Connection object ...");
 
    adodbCn
      =com_lib.ObjectUtils.CreateInstance
        ("ADODB.Connection",
         com_lib.ClsCtxCode.CLSCTX_INPROC_SERVER).GetObject();
 
    Console.WriteLine("Connect ADODB to ADO.NET");
 
    adodb_lib.AdoDbConstructor.attach_adodb_cn_to_oledb_session
     (adodbCn,
      oledbCn.GetNativeSession());
 
    //--------------------------------------
    Console.WriteLine("[ADODB] Start transaction...");
 
    adodbCn.BeginTrans();
 
    //--------------------------------------
    Console.WriteLine("Creation of LCPI.IBP.Samples.IBGenManager.1");
 
    genMng
      =com_lib.ObjectUtils.CreateInstance
         ("LCPI.IBP.Samples.IBGenManager.1",
          com_lib.ClsCtxCode.CLSCTX_INPROC_SERVER).GetObject();
 
    //--------------------------------------
    Console.WriteLine("Connect GenMng to ADODB");
 
    genMng.Connection=adodbCn;
 
    //--------------------------------------
    Console.WriteLine("[GenMng] Generate ID: {0}",
                      genMng.GenID("CUST_NO_GEN"));
 
    //--------------------------------------
    Console.WriteLine("[ADODB] Commit transaction...");
 
    adodbCn.CommitTrans();
   }//using oledbCn
  }
  catch(Exception exc)
  {
   resultCode=1;
 
   Console.WriteLine("ERROR: {0} - {1}",exc.Source,exc.Message);
  }//catch
  finally
  {
   Helper__ReleaseComObject(ref adodbCn);
   Helper__ReleaseComObject(ref genMng);
  }//finally
 
  return resultCode;
 }//Main
 
 //Helper interface ------------------------------------------------------
 private static void Helper__ReleaseComObject<T>(ref T obj) where T:class
 {
  var x=System.Threading.Interlocked.Exchange(ref obj,null);
 
  Helper__ReleaseComObject(x);
 }//Helper__ReleaseComObject
 
 //-----------------------------------------------------------------------
 private static void Helper__ReleaseComObject(object obj)
 {
  if(!Object.ReferenceEquals(obj,null))
   System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
 }//Helper__ReleaseComObject
};//class Program
 
////////////////////////////////////////////////////////////////////////////////
}//nms Sample_0021


Output of sample.
Вывод.

Ревизия [2020-04-22]. Явное освобождение adodbCn и genMng через Helper__ReleaseComObject.