Mercurial > hg > ucis.core
comparison Database.cs @ 0:3ab940a0c7a0
Initial commit
author | Ivo Smits <Ivo@UCIS.nl> |
---|---|
date | Tue, 11 Sep 2012 16:28:53 +0200 |
parents | |
children | 4b78cc5f116b |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:3ab940a0c7a0 |
---|---|
1 using System; | |
2 using System.Reflection; | |
3 using System.Data; | |
4 using System.Data.Common; | |
5 using System.Collections.Generic; | |
6 | |
7 namespace UCIS { | |
8 public class Database { | |
9 private delegate object ConstructorDelegate(); | |
10 private ConstructorInfo _ConnectionConstructor; | |
11 | |
12 public Database(Type DBConnectionType, string connectionString) { | |
13 this.ConnectionString = connectionString; | |
14 _ConnectionConstructor = DBConnectionType.GetConstructor(new Type[] { }); | |
15 } | |
16 | |
17 public string ConnectionString { get; set; } | |
18 | |
19 public virtual IDbConnection GetConnection() { | |
20 lock (_ConnectionConstructor) { | |
21 IDbConnection conn = (IDbConnection)_ConnectionConstructor.Invoke(null); | |
22 conn.ConnectionString = ConnectionString; | |
23 conn.Open(); | |
24 return conn; | |
25 } | |
26 } | |
27 | |
28 public IDbCommand PrepareQuery(string Query, params object[] Parameters) { | |
29 int ParameterI = 0; | |
30 IDbConnection Connection = GetConnection(); | |
31 try { | |
32 IDbCommand Command = Connection.CreateCommand(); | |
33 Command.CommandType = CommandType.Text; | |
34 Command.CommandText = Query; | |
35 Command.Parameters.Clear(); | |
36 foreach (object Parameter in Parameters) { | |
37 IDbDataParameter DBParameter = Command.CreateParameter(); | |
38 DBParameter.Direction = ParameterDirection.Input; | |
39 DBParameter.ParameterName = "?" + ParameterI.ToString(); | |
40 DBParameter.Value = Parameter; | |
41 Command.Parameters.Add(DBParameter); | |
42 ParameterI++; | |
43 } | |
44 if (ParameterI > 0) Command.Prepare(); | |
45 return Command; | |
46 } catch (Exception ex) { | |
47 Connection.Close(); | |
48 throw ex; | |
49 } | |
50 } | |
51 | |
52 public int NonQuery(string QueryString, params object[] Parameters) { | |
53 IDbCommand Command = PrepareQuery(QueryString, Parameters); | |
54 try { | |
55 return Command.ExecuteNonQuery(); | |
56 } finally { | |
57 Command.Connection.Close(); | |
58 } | |
59 } | |
60 | |
61 public object FetchField(string QueryString, params object[] Parameters) { | |
62 IDbCommand Command = PrepareQuery(QueryString, Parameters); | |
63 try { | |
64 return Command.ExecuteScalar(); | |
65 } finally { | |
66 Command.Connection.Close(); | |
67 } | |
68 } | |
69 public object[] FetchRow(string QueryString, params object[] Parameters) { | |
70 IDbCommand Command = PrepareQuery(QueryString, Parameters); | |
71 try { | |
72 IDataReader Reader = Command.ExecuteReader(); | |
73 try { | |
74 if (!Reader.Read()) return null; | |
75 object[] Result = new object[Reader.FieldCount]; | |
76 Reader.GetValues(Result); | |
77 return Result; | |
78 } finally { | |
79 Reader.Close(); | |
80 } | |
81 } finally { | |
82 Command.Connection.Close(); | |
83 } | |
84 } | |
85 public object[][] FetchRows(string QueryString, params object[] Parameters) { | |
86 IDbCommand Command = PrepareQuery(QueryString, Parameters); | |
87 try { | |
88 IDataReader Reader = Command.ExecuteReader(); | |
89 try { | |
90 List<object[]> Result = new List<object[]>(); | |
91 while (Reader.Read()) { | |
92 object[] ResultArray = new object[Reader.FieldCount]; | |
93 Reader.GetValues(ResultArray); | |
94 Result.Add(ResultArray); | |
95 } | |
96 return Result.ToArray(); | |
97 } finally { | |
98 Reader.Close(); | |
99 } | |
100 } finally { | |
101 Command.Connection.Close(); | |
102 } | |
103 } | |
104 public void ForEachRow(Action<Object[]> f, string QueryString, params object[] Parameters) { | |
105 IDbCommand Command = PrepareQuery(QueryString, Parameters); | |
106 try { | |
107 IDataReader Reader = Command.ExecuteReader(); | |
108 try { | |
109 while (Reader.Read()) { | |
110 object[] ResultArray = new object[Reader.FieldCount]; | |
111 Reader.GetValues(ResultArray); | |
112 f(ResultArray); | |
113 } | |
114 } finally { | |
115 Reader.Close(); | |
116 } | |
117 } finally { | |
118 Command.Connection.Close(); | |
119 } | |
120 | |
121 } | |
122 | |
123 /*public DBReader GetReader(string QueryString, params object[] Parameters) { | |
124 return new DBReader(PrepareQuery(QueryString, Parameters)); | |
125 }*/ | |
126 } | |
127 } |