annotate Database.cs @ 111:df53bdd49507 default tip

Merge
author Ivo Smits <Ivo@UCIS.nl>
date Fri, 07 Nov 2014 18:37:39 +0100
parents 40715b34d0d3
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
1 ???using System;
7
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
2 using System.Collections.Generic;
0
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
3 using System.Data;
7
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
4 using System.Reflection;
0
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
5
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
6 namespace UCIS {
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
7 public class Database {
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
8 private delegate object ConstructorDelegate();
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
9 private ConstructorInfo _ConnectionConstructor;
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
10
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
11 public Database(Type DBConnectionType, string connectionString) {
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
12 this.ConnectionString = connectionString;
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
13 _ConnectionConstructor = DBConnectionType.GetConstructor(new Type[] { });
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
14 }
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
15
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
16 public string ConnectionString { get; set; }
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
17
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
18 public virtual IDbConnection GetConnection() {
7
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
19 IDbConnection conn = (IDbConnection)_ConnectionConstructor.Invoke(null);
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
20 conn.ConnectionString = ConnectionString;
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
21 conn.Open();
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
22 return conn;
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
23 }
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
24
85
40715b34d0d3 Added new functions to Database class
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
25 private static IDbCommand PrepareQuery(IDbConnection Connection, string Query, params object[] Parameters) {
7
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
26 IDbCommand Command = Connection.CreateCommand();
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
27 Command.CommandType = CommandType.Text;
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
28 Command.CommandText = Query;
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
29 Command.Parameters.Clear();
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
30 int ParameterI = 0;
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
31 foreach (object Parameter in Parameters) {
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
32 IDbDataParameter DBParameter = Command.CreateParameter();
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
33 DBParameter.Direction = ParameterDirection.Input;
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
34 DBParameter.ParameterName = "?" + ParameterI.ToString();
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
35 DBParameter.Value = Parameter;
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
36 Command.Parameters.Add(DBParameter);
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
37 ParameterI++;
0
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
38 }
7
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
39 if (ParameterI > 0) Command.Prepare();
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
40 return Command;
0
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
41 }
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
42
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
43 public IDbCommand PrepareQuery(string Query, params object[] Parameters) {
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
44 IDbConnection Connection = GetConnection();
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
45 try {
7
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
46 return PrepareQuery(Connection, Query, Parameters);
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
47 } catch (Exception) {
0
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
48 Connection.Close();
7
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
49 throw;
0
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
50 }
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
51 }
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
52
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
53 public int NonQuery(string QueryString, params object[] Parameters) {
7
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
54 using (IDbConnection connection = GetConnection()) {
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
55 using (IDbCommand command = PrepareQuery(connection, QueryString, Parameters)) {
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
56 return command.ExecuteNonQuery();
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
57 }
0
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
58 }
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
59 }
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
60
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
61 public object FetchField(string QueryString, params object[] Parameters) {
7
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
62 using (IDbConnection connection = GetConnection()) {
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
63 using (IDbCommand command = PrepareQuery(connection, QueryString, Parameters)) {
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
64 return command.ExecuteScalar();
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
65 }
0
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
66 }
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
67 }
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
68 public object[] FetchRow(string QueryString, params object[] Parameters) {
7
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
69 using (IDbConnection connection = GetConnection()) {
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
70 using (IDbCommand command = PrepareQuery(connection, QueryString, Parameters)) {
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
71 using (IDataReader Reader = command.ExecuteReader()) {
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
72 if (!Reader.Read()) return null;
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
73 object[] Result = new object[Reader.FieldCount];
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
74 Reader.GetValues(Result);
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
75 return Result;
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
76 }
0
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
77 }
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
78 }
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
79 }
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
80 public object[][] FetchRows(string QueryString, params object[] Parameters) {
7
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
81 using (IDbConnection connection = GetConnection()) {
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
82 using (IDbCommand command = PrepareQuery(connection, QueryString, Parameters)) {
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
83 using (IDataReader Reader = command.ExecuteReader()) {
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
84 List<object[]> Result = new List<object[]>();
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
85 while (Reader.Read()) {
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
86 object[] ResultArray = new object[Reader.FieldCount];
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
87 Reader.GetValues(ResultArray);
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
88 Result.Add(ResultArray);
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
89 }
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
90 return Result.ToArray();
0
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
91 }
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
92 }
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
93 }
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
94 }
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
95 public void ForEachRow(Action<Object[]> f, string QueryString, params object[] Parameters) {
7
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
96 using (IDbConnection connection = GetConnection()) {
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
97 using (IDbCommand command = PrepareQuery(connection, QueryString, Parameters)) {
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
98 using (IDataReader Reader = command.ExecuteReader()) {
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
99 while (Reader.Read()) {
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
100 object[] ResultArray = new object[Reader.FieldCount];
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
101 Reader.GetValues(ResultArray);
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
102 f(ResultArray);
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
103 }
0
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
104 }
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
105 }
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
106 }
7
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
107 }
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
108 public void ForEachRow(Action<IDataRecord> callback, string query, params object[] parameters) {
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
109 using (IDbConnection connection = GetConnection()) {
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
110 using (IDbCommand command = PrepareQuery(connection, query, parameters)) {
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
111 using (IDataReader Reader = command.ExecuteReader()) {
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
112 while (Reader.Read()) callback(Reader);
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
113 }
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
114 }
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 0
diff changeset
115 }
0
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
116 }
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
117
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
118 /*public DBReader GetReader(string QueryString, params object[] Parameters) {
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
119 return new DBReader(PrepareQuery(QueryString, Parameters));
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
120 }*/
85
40715b34d0d3 Added new functions to Database class
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
121
40715b34d0d3 Added new functions to Database class
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
122 public IDataReader ExecuteReader(String QueryString, params Object[] Parameters) {
40715b34d0d3 Added new functions to Database class
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
123 IDbConnection connection = GetConnection();
40715b34d0d3 Added new functions to Database class
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
124 try {
40715b34d0d3 Added new functions to Database class
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
125 using (IDbCommand command = PrepareQuery(connection, QueryString, Parameters)) {
40715b34d0d3 Added new functions to Database class
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
126 return command.ExecuteReader(CommandBehavior.CloseConnection);
40715b34d0d3 Added new functions to Database class
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
127 }
40715b34d0d3 Added new functions to Database class
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
128 } catch {
40715b34d0d3 Added new functions to Database class
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
129 connection.Dispose();
40715b34d0d3 Added new functions to Database class
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
130 throw;
40715b34d0d3 Added new functions to Database class
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
131 }
40715b34d0d3 Added new functions to Database class
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
132 }
40715b34d0d3 Added new functions to Database class
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
133
40715b34d0d3 Added new functions to Database class
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
134 public IEnumerable<IDataRecord> EnumerateRows(String query, params Object[] parameters) {
40715b34d0d3 Added new functions to Database class
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
135 IDbConnection connection = GetConnection();
40715b34d0d3 Added new functions to Database class
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
136 try {
40715b34d0d3 Added new functions to Database class
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
137 return new DataEnumerator(PrepareQuery(connection, query, parameters));
40715b34d0d3 Added new functions to Database class
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
138 } catch {
40715b34d0d3 Added new functions to Database class
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
139 connection.Dispose();
40715b34d0d3 Added new functions to Database class
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
140 throw;
40715b34d0d3 Added new functions to Database class
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
141 }
40715b34d0d3 Added new functions to Database class
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
142 }
40715b34d0d3 Added new functions to Database class
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
143
40715b34d0d3 Added new functions to Database class
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
144 class DataEnumerator : IEnumerable<IDataRecord>, IEnumerator<IDataRecord> {
40715b34d0d3 Added new functions to Database class
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
145 IDbCommand command = null;
40715b34d0d3 Added new functions to Database class
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
146 IDataReader reader = null;
40715b34d0d3 Added new functions to Database class
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
147 public DataEnumerator(IDbCommand command) {
40715b34d0d3 Added new functions to Database class
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
148 this.command = command;
40715b34d0d3 Added new functions to Database class
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
149 try {
40715b34d0d3 Added new functions to Database class
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
150 this.reader = command.ExecuteReader();
40715b34d0d3 Added new functions to Database class
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
151 } catch {
40715b34d0d3 Added new functions to Database class
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
152 Dispose();
40715b34d0d3 Added new functions to Database class
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
153 throw;
40715b34d0d3 Added new functions to Database class
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
154 }
40715b34d0d3 Added new functions to Database class
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
155 }
40715b34d0d3 Added new functions to Database class
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
156 IEnumerator<IDataRecord> IEnumerable<IDataRecord>.GetEnumerator() {
40715b34d0d3 Added new functions to Database class
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
157 return this;
40715b34d0d3 Added new functions to Database class
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
158 }
40715b34d0d3 Added new functions to Database class
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
159 System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
40715b34d0d3 Added new functions to Database class
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
160 return this;
40715b34d0d3 Added new functions to Database class
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
161 }
40715b34d0d3 Added new functions to Database class
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
162 public IDataRecord Current {
40715b34d0d3 Added new functions to Database class
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
163 get { return reader; }
40715b34d0d3 Added new functions to Database class
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
164 }
40715b34d0d3 Added new functions to Database class
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
165 object System.Collections.IEnumerator.Current {
40715b34d0d3 Added new functions to Database class
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
166 get { return reader; }
40715b34d0d3 Added new functions to Database class
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
167 }
40715b34d0d3 Added new functions to Database class
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
168 public bool MoveNext() {
40715b34d0d3 Added new functions to Database class
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
169 return reader.Read();
40715b34d0d3 Added new functions to Database class
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
170 }
40715b34d0d3 Added new functions to Database class
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
171 public void Reset() {
40715b34d0d3 Added new functions to Database class
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
172 throw new NotSupportedException();
40715b34d0d3 Added new functions to Database class
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
173 }
40715b34d0d3 Added new functions to Database class
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
174 public Object[] CurrentRow {
40715b34d0d3 Added new functions to Database class
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
175 get {
40715b34d0d3 Added new functions to Database class
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
176 object[] array = new object[reader.FieldCount];
40715b34d0d3 Added new functions to Database class
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
177 reader.GetValues(array);
40715b34d0d3 Added new functions to Database class
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
178 return array;
40715b34d0d3 Added new functions to Database class
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
179 }
40715b34d0d3 Added new functions to Database class
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
180 }
40715b34d0d3 Added new functions to Database class
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
181 public void Dispose() {
40715b34d0d3 Added new functions to Database class
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
182 if (reader != null) reader.Dispose();
40715b34d0d3 Added new functions to Database class
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
183 if (command != null) {
40715b34d0d3 Added new functions to Database class
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
184 IDbConnection connection = command.Connection;
40715b34d0d3 Added new functions to Database class
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
185 command.Dispose();
40715b34d0d3 Added new functions to Database class
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
186 connection.Dispose();
40715b34d0d3 Added new functions to Database class
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
187 }
40715b34d0d3 Added new functions to Database class
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
188 }
40715b34d0d3 Added new functions to Database class
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
189 }
0
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
190 }
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
191 }