Не сте регистриран! Регистрирайте се БЕЗПЛАТНО, за да използвате услугите на сайта!

 C# Database wrapper
Автор  YordanGeorg (03.04.2008 09:04) съобщение до автора
Погледнат  793 пъти добави към любими
Оценка добави коментар
Гласове  -- изпрати на приятел
Коментари  (0) абонирай се за C-Cplusplus
     
YordanGeorg
     
 

CODE
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
//Courtesy of http://www.codoxide.com/post/My-Favorite-Database-Wrapper-for-C.aspx
using System
;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Common;

namespace DbNamespace
{
  
//comm -- / <summary>
   
//comm -- / Abstract base class for encapsulating provider independant database interactin logic.
   
//comm -- / </summary>
  
//comm -- / <typeparam name="CONNECTION_TYPE"><see cref="DbConnection"/> derived Connection type.</typeparam>
  
//comm -- / <typeparam name="COMMAND_TYPE"><see cref="DbCommand"/> derived Command type.</typeparam>
  
//comm -- / <typeparam name="ADAPTER_TYPE"><see cref="DbDataAdapater"/> derived Data Adapter type.</typeparam>
   
public abstract class AbstractDatabase<CONNECTION_TYPE, COMMAND_TYPE, ADAPTER_TYPE> : IDisposable
        where CONNECTION_TYPE
: DbConnection, new()
       
where COMMAND_TYPE : DbCommand
       
where ADAPTER_TYPE : DbDataAdapter, new()
    {
       
#region : Connection :

      
//comm -- / <summary>Gets the Connection object associated with the current instance.</summary>
       
public DbConnection Connection
       
{
           
get
           
{
               
if (internal_currentConnection == null)
                {
                   
internal_currentConnection = new CONNECTION_TYPE();
                   
internal_currentConnection.ConnectionString = GetConnectionString();
               
}
               
return internal_currentConnection;
           
}
        }
       
private DbConnection internal_currentConnection;

      
//comm -- / <summary>When overridden in derived classes returns the connection string for the database.</summary>
      
//comm -- / <returns>The connection string for the database.</returns>
       
protected abstract string GetConnectionString();

       
#endregion

        #region : Commands :

      
//comm -- / <summary>Gets a DbCommand object with the specified <see cref="DbCommand.CommandText"/>.</summary>
      
//comm -- / <param name="sqlString">The SQL string.</param>
      
//comm -- / <returns>A DbCommand object with the specified <see cref="DbCommand.CommandText"/>.</returns>
       
public DbCommand GetSqlStringCommand(string sqlString)
        {
           
if (this.Connection.State != ConnectionState.Open)
               
this.Connection.Open();

           
DbCommand cmd  = this.Connection.CreateCommand();
           
cmd.CommandType = CommandType.Text;
           
cmd.CommandText = sqlString;
           
return cmd;
       
}

      
//comm -- / <summary>Gets a DbCommand object with the specified <see cref="DbCommand.CommandText"/>.</summary>
      
//comm -- / <param name="sqlStringFormat">The SQL string format.</param>
      
//comm -- / <param name="args">The format arguments.</param>
      
//comm -- / <returns>A DbCommand object with the specified <see cref="DbCommand.CommandText"/>.</returns>
       
public DbCommand GetSqlStringCommand(string sqlStringFormat, params object[] args)
        {
           
return GetSqlStringCommand(string.Format(sqlStringFormat, args));
       
}

      
//comm -- / <summary>Gets a DbCommand object for the specified Stored Procedure.</summary>
      
//comm -- / <param name="storedProcName">The name of the stored procedure.</param>
      
//comm -- / <returns>A DbCommand object for the specified Stored Procedure.</returns>
       
public DbCommand GetStoredProcedureCommand(string storedProcName)
        {
           
if (this.Connection.State != ConnectionState.Open)
               
this.Connection.Open();

           
DbCommand cmd    = this.Connection.CreateCommand();
           
cmd.CommandType   = CommandType.StoredProcedure;
           
cmd.CommandText   = storedProcName;
           
return cmd;
       
}

       
#region : Parameters :

      
//comm -- / <summary>Adds an input parameter to the given <see cref="DbCommand"/>.</summary>
      
//comm -- / <param name="cmd">The command object the parameter should be added to.</param>
      
//comm -- / <param name="paramName">The identifier of the parameter.</param>
      
//comm -- / <param name="paramType">The type of the parameter.</param>
      
//comm -- / <param name="value">The value of the parameter.</param>
      
//comm -- / <returns>The <see cref="DbParameter"/> that was created.</returns>
       
public DbParameter AddInParameter(DbCommand cmd, string paramName, DbType paramType, object value)
        {
           
DbParameter param       = cmd.CreateParameter();
           
param.DbType            = paramType;
           
param.ParameterName     = paramName;
           
param.Value             = value;
           
param.Direction         = ParameterDirection.Input;
           
cmd.Parameters.Add(param);
           
return param;
       
}

      
//comm -- / <summary>Adds an input parameter to the given <see cref="DbCommand"/>.</summary>
      
//comm -- / <param name="cmd">The command object the parameter should be added to.</param>
      
//comm -- / <param name="paramName">The identifier of the parameter.</param>
      
//comm -- / <param name="paramType">The type of the parameter.</param>
      
//comm -- / <param name="size">The maximum size in bytes, of the data table column to be affected.</param>
      
//comm -- / <param name="value">The value of the parameter.</param>
      
//comm -- / <returns>The <see cref="DbParameter"/> that was created.</returns>
       
public DbParameter AddInParameter(DbCommand cmd, string paramName, DbType paramType, int size, object value)
        {
           
DbParameter param       = cmd.CreateParameter();
           
param.DbType            = paramType;
           
param.ParameterName     = paramName;
           
param.Size              = size;
           
param.Value             = value;
           
param.Direction         = ParameterDirection.Input;
           
cmd.Parameters.Add(param);
           
return param;
       
}

       
public DbParameter AddInOutParameter ( DbCommand cmd, string paramName, DbType paramType, int size, object value )
            {
           
DbParameter param = cmd.CreateParameter ( );
           
param.DbType = paramType;
           
param.ParameterName = paramName;
           
param.Size = size;
           
param.Value = value;
           
param.Direction = ParameterDirection.Output;
           
cmd.Parameters.Add ( param );
           
return param;
           
}


       
public DbParameter AddInOutParameter ( DbCommand cmd, string paramName, DbType paramType, object value )
            {
           
DbParameter param = cmd.CreateParameter ( );
           
param.DbType = paramType;
           
param.ParameterName = paramName;
           
param.Value = value;
           
param.Direction = ParameterDirection.Output;
           
cmd.Parameters.Add ( param );
           
return param;
           
}

       
#endregion

        #region : Executes :

      
//comm -- / <summary>Executes the specified command against the current connection.</summary>
      
//comm -- / <param name="cmd">The command to be executed.</param>
      
//comm -- / <returns>Result returned by the database engine.</returns>
       
public int ExecuteNonQuery(DbCommand cmd)
        {
           
if (this.Connection.State != ConnectionState.Open)
               
this.Connection.Open();

           
return cmd.ExecuteNonQuery();
       
}

      
//comm -- / <summary>Executes the specified command against the current connection.</summary>
      
//comm -- / <param name="cmd">The command to be executed.</param>
      
//comm -- / <param name="txn">The database transaction inside which the command should be executed.</param>
      
//comm -- / <returns>Result returned by the database engine.</returns>
       
public int ExecuteNonQuery(DbCommand cmd, DbTransaction txn)
        {
           
if (this.Connection.State != ConnectionState.Open)
               
this.Connection.Open();

           
cmd.Transaction = txn;
           
return cmd.ExecuteNonQuery();
       
}

      
//comm -- / <summary>Executes the specified command against the current connection.</summary>
      
//comm -- / <param name="cmd">The command to be executed.</param>
      
//comm -- / <returns>Result returned by the database engine.</returns>
       
public DbDataReader ExecuteReader(DbCommand cmd)
        {
           
if (this.Connection.State != ConnectionState.Open)
               
this.Connection.Open();

           
return cmd.ExecuteReader();
       
}

      
//comm -- / <summary>Executes the specified command against the current connection.</summary>
      
//comm -- / <param name="cmd">The command to be executed.</param>
      
//comm -- / <param name="behavior">One of the <see cref="System.Data.CommandBehavior"/> values.</param>
      
//comm -- / <returns>Result returned by the database engine.</returns>
       
public DbDataReader ExecuteReader(DbCommand cmdCommandBehavior behavior )
        {
           
if (this.Connection.State != ConnectionState.Open)
               
this.Connection.Open();

           
return cmd.ExecuteReader(behavior);
       
}

      
//comm -- / <summary>Executes the specified command against the current connection.</summary>
      
//comm -- / <param name="cmd">The command to be executed.</param>
      
//comm -- / <returns>Result returned by the database engine.</returns>
       
public T ExecuteScalar<T>(DbCommand cmd, T defaultValue)
        {
           
if (this.Connection.State != ConnectionState.Open)
               
this.Connection.Open();

           
object retVal = cmd.ExecuteScalar();
           
if (null == retVal || DBNull.Value == retVal)
               
return defaultValue;
           
else
                return
(T)retVal;
       
}

      
//comm -- / <summary>Executes the specified command against the current connection.</summary>
      
//comm -- / <param name="cmd">The command to be executed.</param>
      
//comm -- / <returns>Result returned by the database engine.</returns>
       
public DataSet ExecuteDataSet(DbCommand cmd)
        {
           
ADAPTER_TYPE adapter  = new ADAPTER_TYPE();
           
adapter.SelectCommand = (COMMAND_TYPE)cmd;

           
DataSet retVal = new DataSet();
           
adapter.Fill(retVal);
           
return retVal;
       
}

       
#endregion

        #endregion

      
//comm -- / <summary>Begins a transaction.</summary>
      
//comm -- / <returns>Created transaction.</returns>
       
public DbTransaction BeginTransaction()
        {
           
if (this.Connection.State != ConnectionState.Open)
               
this.Connection.Open();
           
return Connection.BeginTransaction();
       
}

       
#region : Construction / Destruction :

      
//comm -- / <summary>Disposes the resources associated with the current database connection.</summary>
        ~
AbstractDatabase()
        {
           
Dispose();
       
}

       
#region IDisposable Members

      
//comm -- / <summary>Disposes the resources associated with the current database connection.</summary>
       
public void Dispose()
        {
           
if (null != internal_currentConnection)
            {
               
internal_currentConnection.Dispose();
               
internal_currentConnection = null;
           
}
        }

       
#endregion

        #endregion

   
} //eof public abstract class AbstractDatabase
} //eof namespace DbNamespace



Ключови думи: c# database база данни клас wrapper




 1 посетител чете този скрипт (0 потребители и 1 гост)  
Активни потребители: ---
   
  

Еmail  
 

 

 
  • Интересно от Софтуер
 
  • Интересно от myLINKS
 
 
 
 



IT-PLACE.NET © 2004 - 2008