comparison Database.cs @ 85:40715b34d0d3

Added new functions to Database class
author Ivo Smits <Ivo@UCIS.nl>
date Thu, 05 Jun 2014 00:21:23 +0200
parents 4b78cc5f116b
children
comparison
equal deleted inserted replaced
84:146a8d224d86 85:40715b34d0d3
20 conn.ConnectionString = ConnectionString; 20 conn.ConnectionString = ConnectionString;
21 conn.Open(); 21 conn.Open();
22 return conn; 22 return conn;
23 } 23 }
24 24
25 private IDbCommand PrepareQuery(IDbConnection Connection, string Query, params object[] Parameters) { 25 private static IDbCommand PrepareQuery(IDbConnection Connection, string Query, params object[] Parameters) {
26 IDbCommand Command = Connection.CreateCommand(); 26 IDbCommand Command = Connection.CreateCommand();
27 Command.CommandType = CommandType.Text; 27 Command.CommandType = CommandType.Text;
28 Command.CommandText = Query; 28 Command.CommandText = Query;
29 Command.Parameters.Clear(); 29 Command.Parameters.Clear();
30 int ParameterI = 0; 30 int ParameterI = 0;
116 } 116 }
117 117
118 /*public DBReader GetReader(string QueryString, params object[] Parameters) { 118 /*public DBReader GetReader(string QueryString, params object[] Parameters) {
119 return new DBReader(PrepareQuery(QueryString, Parameters)); 119 return new DBReader(PrepareQuery(QueryString, Parameters));
120 }*/ 120 }*/
121
122 public IDataReader ExecuteReader(String QueryString, params Object[] Parameters) {
123 IDbConnection connection = GetConnection();
124 try {
125 using (IDbCommand command = PrepareQuery(connection, QueryString, Parameters)) {
126 return command.ExecuteReader(CommandBehavior.CloseConnection);
127 }
128 } catch {
129 connection.Dispose();
130 throw;
131 }
132 }
133
134 public IEnumerable<IDataRecord> EnumerateRows(String query, params Object[] parameters) {
135 IDbConnection connection = GetConnection();
136 try {
137 return new DataEnumerator(PrepareQuery(connection, query, parameters));
138 } catch {
139 connection.Dispose();
140 throw;
141 }
142 }
143
144 class DataEnumerator : IEnumerable<IDataRecord>, IEnumerator<IDataRecord> {
145 IDbCommand command = null;
146 IDataReader reader = null;
147 public DataEnumerator(IDbCommand command) {
148 this.command = command;
149 try {
150 this.reader = command.ExecuteReader();
151 } catch {
152 Dispose();
153 throw;
154 }
155 }
156 IEnumerator<IDataRecord> IEnumerable<IDataRecord>.GetEnumerator() {
157 return this;
158 }
159 System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
160 return this;
161 }
162 public IDataRecord Current {
163 get { return reader; }
164 }
165 object System.Collections.IEnumerator.Current {
166 get { return reader; }
167 }
168 public bool MoveNext() {
169 return reader.Read();
170 }
171 public void Reset() {
172 throw new NotSupportedException();
173 }
174 public Object[] CurrentRow {
175 get {
176 object[] array = new object[reader.FieldCount];
177 reader.GetValues(array);
178 return array;
179 }
180 }
181 public void Dispose() {
182 if (reader != null) reader.Dispose();
183 if (command != null) {
184 IDbConnection connection = command.Connection;
185 command.Dispose();
186 connection.Dispose();
187 }
188 }
189 }
121 } 190 }
122 } 191 }