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

Использование System.IO.TextReader (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
////////////////////////////////////////////////////////////////////////////////
//Samples for LCPI ADO.NET Data provider for OLEDB.
//                                                                    28.05.2013
using System;
using System.Data;
using lcpi.data.oledb;
 
namespace Sample_0003
{
 class Program
 {
  private const string c_cn_str
   ="provider=LCPI.IBProvider.3;"
   +"location=localhost:d:\\database\\ibp_test_fb25_d3.gdb;"
   +"user id=gamer;"
   +"password=vermut;";
 
  //----------------------------------------------------------------------
  private static char GenChar(int i)
  {
   return (char)('a'+(i%26));
  }//GenChar
 
  //----------------------------------------------------------------------
  static int Main(string[] args)
  {
   int resultCode=0;
 
   try
   {
    const int c_chars=8*1024*1024;//8MB
 
    var chars=new System.Text.StringBuilder(c_chars);
 
    for(int i=0;i!=c_chars;++i)
     chars.Append(GenChar(i));
 
    //------
    var cn=new OleDbConnection(c_cn_str);
 
    cn.Open();
 
    var tr=cn.BeginTransaction(IsolationLevel.RepeatableRead);
 
    var cmd=new OleDbCommand("",cn,tr);
 
    //INSERT text data into text blob through System.IO.StringReader object
    cmd.CommandText="insert into TBL_CS__ASCII (COL_BLOB) values (:x)\n"
                   +"returning TEST_ID\n"
                   +"into :id";
 
    //implicit generation of command parameters
    cmd["x"].Value=new System.IO.StringReader(chars.ToString());
 
    cmd.ExecuteNonQuery();
 
    var record_id=cmd["id"].Value;
 
    Console.WriteLine("record_id: {0}",record_id);
 
    //-------
    cmd.CommandText="select COL_BLOB from TBL_CS__ASCII where test_id=:id";
 
    //provider removes old implicit descriptions of parameters and
    //obtains parameters from new sql
    cmd["id"].Value=record_id;
 
    //hint to provider: command with one result
    var reader=cmd.ExecuteReader(CommandBehavior.SingleResult);
 
    if(!reader.Read())
     throw new ApplicationException("record not found!");
 
    //access to BIN_DATA through stream object
    var blob_stream=reader.GetTextReader(0);
 
    //check blob_data
    for(int i=0;i!=c_chars;++i)
    {
     int c=blob_stream.Read();
 
     if(c!=GenChar(i))
      throw new ApplicationException("Wrong blob data!");
    }//for
 
    //check EOF
    if(blob_stream.Read()!=-1)
     throw new ApplicationException("Wrong blob data!");
 
    //Commit transaction
    tr.Commit();
 
    //-----
    Console.WriteLine("All is OK.");
   }
   catch(Exception e)
   {
    resultCode=1;
 
    Console.WriteLine("");
    Console.WriteLine("ERROR: {0} - {1}",e.Source,e.Message);
   }//catch
 
   return resultCode;
  }//Main
 }//class Program
}//Sample_0003