[Web] Fix obvious typo in DELETE FROM WHERE statement.
[mono.git] / mcs / class / System.Web / System.Web.SessionState_2.0 / SessionSQLServerHandler.cs
1 //
2 // System.Web.Compilation.SessionStateItemCollection
3 //
4 // Authors:
5 //   Marek Habersack <mhabersack@novell.com>
6 //
7 // (C) 2009-2010 Novell, Inc (http://novell.com/)
8 //
9
10 // Code based on samples from MSDN
11 //
12 // Database schema found in ../ASPState.sql
13 //
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
21 // 
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 // 
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 //
33 #if NET_2_0
34 using System;
35 using System.Collections.Generic;
36 using System.Collections.Specialized;
37 using System.Configuration.Provider;
38 using System.Data;
39 using System.Data.Common;
40 using System.IO;
41 using System.IO.Compression;
42 using System.Reflection;
43 using System.Web;
44 using System.Web.Configuration;
45 using System.Web.Hosting;
46
47 namespace System.Web.SessionState 
48 {
49         sealed class SessionSQLServerHandler : SessionStateStoreProviderBase
50         {
51                 static readonly string defaultDbFactoryTypeName = "Mono.Data.Sqlite.SqliteFactory, Mono.Data.Sqlite, Version=2.0.0.0, Culture=neutral, PublicKeyToken=0738eb9f132ed756";
52                 
53                 SessionStateSection sessionConfig;
54                 string connectionString;
55                 Type providerFactoryType;
56                 DbProviderFactory providerFactory;
57                 int sqlCommandTimeout;
58                 
59                 DbProviderFactory ProviderFactory {
60                         get {
61                                 if (providerFactory == null) {
62                                         try {
63                                                 providerFactory = Activator.CreateInstance (providerFactoryType) as DbProviderFactory;
64                                         } catch (Exception ex) {
65                                                 throw new ProviderException ("Failure to create database factory instance.", ex);
66                                         }
67                                 }
68
69                                 return providerFactory;
70                         }
71                 }
72                 
73                 public string ApplicationName {
74                         get; private set;
75                 }
76                 
77                 public override void Initialize (string name, NameValueCollection config)
78                 {
79                         if (config == null)
80                                 throw new ArgumentNullException ("config");
81
82                         if (String.IsNullOrEmpty (name))
83                                 name = "SessionSQLServerHandler";
84
85                         if (String.IsNullOrEmpty (config["description"])) {
86                                 config.Remove ("description");
87                                 config.Add ("description", "Mono SQL Session Store Provider");
88                         }
89                         ApplicationName = HostingEnvironment.ApplicationVirtualPath;
90                         
91                         base.Initialize(name, config);
92                         sessionConfig = WebConfigurationManager.GetWebApplicationSection ("system.web/sessionState") as SessionStateSection;
93                         connectionString = sessionConfig.SqlConnectionString;
94                         string dbProviderName;
95                         
96                         if (String.IsNullOrEmpty (connectionString) || String.Compare (connectionString, SessionStateSection.DefaultSqlConnectionString, StringComparison.Ordinal) == 0) {
97                                 connectionString = "Data Source=|DataDirectory|/ASPState.sqlite;Version=3";
98                                 dbProviderName = defaultDbFactoryTypeName;
99                         } else {
100                                 string[] parts = connectionString.Split (';');
101                                 var newCS = new List <string> ();
102                                 dbProviderName = null;
103                                 bool allowDb = sessionConfig.AllowCustomSqlDatabase;
104                                 
105                                 foreach (string p in parts) {
106                                         if (p.Trim ().Length == 0)
107                                                 continue;
108                                         
109                                         if (p.StartsWith ("DbProviderName", StringComparison.OrdinalIgnoreCase)) {
110                                                 int idx = p.IndexOf ('=');
111                                                 if (idx < 0)
112                                                         throw new ProviderException ("Invalid format for the 'DbProviderName' connection string parameter. Expected 'DbProviderName = value'.");
113
114                                                 dbProviderName = p.Substring (idx + 1);
115                                                 continue;
116                                         }
117
118                                         if (!allowDb) {
119                                                 string tmp = p.Trim ();
120                                                 if (tmp.StartsWith ("database", StringComparison.OrdinalIgnoreCase) ||
121                                                     tmp.StartsWith ("initial catalog", StringComparison.OrdinalIgnoreCase))
122                                                         throw new ProviderException ("Specifying a custom database is not allowed. Set the allowCustomSqlDatabase attribute of the <system.web/sessionState> section to 'true' in order to use a custom database name.");
123                                         }
124                                         
125                                         newCS.Add (p);
126                                 }
127
128                                 connectionString = String.Join (";", newCS.ToArray ());
129                                 if (String.IsNullOrEmpty (dbProviderName))
130                                         dbProviderName = defaultDbFactoryTypeName;
131
132                                 
133                         }
134
135                         Exception typeException = null;
136                         
137                         try {   
138                                 providerFactoryType = Type.GetType (dbProviderName, true);
139                         } catch (Exception ex) {
140                                 typeException = ex;
141                                 providerFactoryType = null;
142                         }
143
144                         if (providerFactoryType == null)
145                                 throw new ProviderException ("Unable to find database provider factory type.", typeException);
146
147                         sqlCommandTimeout = (int)sessionConfig.SqlCommandTimeout.TotalSeconds;
148                 }
149
150                 public override void Dispose ()
151                 {
152                 }
153
154                 public override bool SetItemExpireCallback (SessionStateItemExpireCallback expireCallback)
155                 {
156                         return false;
157                 }
158
159                 public override void SetAndReleaseItemExclusive (HttpContext context, string id, SessionStateStoreData item, object lockId, bool newItem)                                           
160                 {
161                         DbCommand cmd;
162                         DbCommand deleteCmd = null;
163                         string sessItems = Serialize((SessionStateItemCollection)item.Items);
164                         DbProviderFactory factory = ProviderFactory;
165                         string appName = ApplicationName;
166                         DbConnection conn = CreateConnection (factory);
167                         DateTime now = DateTime.Now;                    
168                         DbParameterCollection parameters;
169                         
170                         if (newItem) {  
171                                 deleteCmd = CreateCommand (factory, conn, "DELETE FROM Sessions WHERE SessionId = @SessionId AND ApplicationName = @ApplicationName AND Expires < @Expires");
172                                 parameters = deleteCmd.Parameters;                              
173
174                                 parameters.Add (CreateParameter <string> (factory, "@SessionId", id, 80));
175                                 parameters.Add (CreateParameter <string> (factory, "@ApplicationName", appName, 255));
176                                 parameters.Add (CreateParameter <DateTime> (factory, "@Expires", now));
177
178                                 cmd = CreateCommand (factory, conn, "INSERT INTO Sessions (SessionId, ApplicationName, Created, Expires, LockDate, LockId, Timeout, Locked, SessionItems, Flags) Values (@SessionId, @ApplicationName, @Created, @Expires, @LockDate, @LockId , @Timeout, @Locked, @SessionItems, @Flags)");
179                                 parameters = cmd.Parameters;
180                                 
181                                 parameters.Add (CreateParameter <string> (factory, "@SessionId", id, 80));
182                                 parameters.Add (CreateParameter <string> (factory, "@ApplicationName", appName, 255));
183                                 parameters.Add (CreateParameter <DateTime> (factory, "@Created", now));
184                                 parameters.Add (CreateParameter <DateTime> (factory, "@Expires", now.AddMinutes ((double)item.Timeout)));
185                                 parameters.Add (CreateParameter <DateTime> (factory, "@LockDate", now));
186                                 parameters.Add (CreateParameter <int> (factory, "@LockId", 0));
187                                 parameters.Add (CreateParameter <int> (factory, "@Timeout", item.Timeout));
188                                 parameters.Add (CreateParameter <bool> (factory, "@Locked", false));
189                                 parameters.Add (CreateParameter <string> (factory, "@SessionItems", sessItems));
190                                 parameters.Add (CreateParameter <int> (factory, "@Flags", 0));
191                         } else {
192                                 cmd = CreateCommand (factory, conn, "UPDATE Sessions SET Expires = @Expires, SessionItems = @SessionItems, Locked = @Locked WHERE SessionId = @SessionId AND ApplicationName = @ApplicationName AND LockId = @LockId");
193                                 parameters = cmd.Parameters;
194
195                                 parameters.Add (CreateParameter <DateTime> (factory, "@Expires", now.AddMinutes ((double)item.Timeout)));
196                                 parameters.Add (CreateParameter <string> (factory, "@SessionItems", sessItems));
197                                 parameters.Add (CreateParameter <bool> (factory, "@Locked", false));
198                                 parameters.Add (CreateParameter <string> (factory, "@SessionId", id, 80));
199                                 parameters.Add (CreateParameter <string> (factory, "@ApplicationName", appName, 255));
200                                 parameters.Add (CreateParameter <int> (factory, "@Lockid", (int)lockId));
201                         }
202
203                         try
204                         {
205                                 conn.Open();
206                                 if (deleteCmd != null)
207                                         deleteCmd.ExecuteNonQuery();
208
209                                 cmd.ExecuteNonQuery();
210                         } catch (Exception ex) {
211                                 throw new ProviderException ("Failure storing session item in database.", ex);
212                         } finally {
213                                 conn.Close();
214                         }
215                 }
216
217                 public override SessionStateStoreData GetItem (HttpContext context, string id, out bool locked, out TimeSpan lockAge,
218                                                                out object lockId, out SessionStateActions actionFlags)
219                 {
220                         return GetSessionStoreItem (false, context, id, out locked, out lockAge, out lockId, out actionFlags);
221                 }
222
223                 public override SessionStateStoreData GetItemExclusive (HttpContext context, string id, out bool locked,out TimeSpan lockAge,
224                                                                        out object lockId, out SessionStateActions actionFlags)
225                 {
226                         return GetSessionStoreItem (true, context, id, out locked, out lockAge, out lockId, out actionFlags);
227                 }
228
229                 private SessionStateStoreData GetSessionStoreItem (bool lockRecord, HttpContext context, string id, out bool locked,
230                                                                    out TimeSpan lockAge, out object lockId, out SessionStateActions actionFlags)
231                 {
232                         SessionStateStoreData item = null;
233                         lockAge = TimeSpan.Zero;
234                         lockId = null;
235                         locked = false;
236                         actionFlags = 0;
237
238                         DbProviderFactory factory = ProviderFactory;
239                         DbConnection conn = CreateConnection (factory);
240                         string appName = ApplicationName;
241                         DbCommand cmd = null;
242                         DbDataReader reader = null;
243                         DbParameterCollection parameters;
244                         DateTime expires;
245                         string serializedItems = String.Empty;
246                         bool foundRecord = false;
247                         bool deleteData = false;
248                         int timeout = 0;
249                         DateTime now = DateTime.Now;
250
251                         try {
252                                 conn.Open();
253                                 if (lockRecord) {
254                                         cmd = CreateCommand (factory, conn, "UPDATE Sessions SET Locked = @Locked, LockDate = @LockDate WHERE SessionId = @SessionId AND ApplicationName = @ApplicationName AND Expires > @Expires");
255                                         parameters = cmd.Parameters;
256                                         
257                                         parameters.Add (CreateParameter <bool> (factory, "@Locked", true));
258                                         parameters.Add (CreateParameter <DateTime> (factory, "@LockDate", now));
259                                         parameters.Add (CreateParameter <string> (factory, "@SessionId", id, 80));
260                                         parameters.Add (CreateParameter <string> (factory, "@ApplicationName", appName, 255));
261                                         parameters.Add (CreateParameter <DateTime> (factory, "@Expires", now));
262
263                                         if (cmd.ExecuteNonQuery() == 0)
264                                                 locked = true;             
265                                         else
266                                                 locked = false;
267                                 }
268
269                                 cmd = CreateCommand (factory, conn, "SELECT Expires, SessionItems, LockId, LockDate, Flags, Timeout FROM Sessions WHERE SessionId = @SessionId AND ApplicationName = @ApplicationName");
270                                 parameters = cmd.Parameters;
271
272                                 parameters.Add (CreateParameter <string> (factory, "@SessionId", id, 80));
273                                 parameters.Add (CreateParameter <string> (factory, "@ApplicationName", appName, 255));
274                                 
275                                 reader = cmd.ExecuteReader (CommandBehavior.SingleRow);
276                                 while (reader.Read()) {
277                                         expires = reader.GetDateTime (reader.GetOrdinal ("Expires"));
278
279                                         if (expires < now) {
280                                                 locked = false;
281                                                 deleteData = true;
282                                         } else
283                                                 foundRecord = true;
284
285                                         serializedItems = reader.GetString (reader.GetOrdinal ("SessionItems"));
286                                         lockId = reader.GetInt32 (reader.GetOrdinal ("LockId"));
287                                         lockAge = now.Subtract (reader.GetDateTime (reader.GetOrdinal ("LockDate")));
288                                         actionFlags = (SessionStateActions) reader.GetInt32 (reader.GetOrdinal ("Flags"));
289                                         timeout = reader.GetInt32 (reader.GetOrdinal ("Timeout"));
290                                 }
291                                 reader.Close();
292
293                                 if (deleteData) {
294                                         cmd = CreateCommand (factory, conn, "DELETE FROM Sessions WHERE SessionId = @SessionId AND ApplicationName = @ApplicationName");
295                                         parameters = cmd.Parameters;
296
297                                         parameters.Add (CreateParameter <string> (factory, "@SessionId", id, 80));
298                                         parameters.Add (CreateParameter <string> (factory, "@ApplicationName", appName, 255));
299
300                                         cmd.ExecuteNonQuery();
301                                 }
302
303                                 if (!foundRecord)
304                                         locked = false;
305
306                                 if (foundRecord && !locked) {
307                                         lockId = (int)lockId + 1;
308
309                                         cmd = CreateCommand (factory, conn, "UPDATE Sessions SET LockId = @LockId, Flags = 0 WHERE SessionId = @SessionId AND ApplicationName = @ApplicationName");
310                                         parameters = cmd.Parameters;
311
312                                         parameters.Add (CreateParameter <int> (factory, "@LockId", (int)lockId));
313                                         parameters.Add (CreateParameter <string> (factory, "@SessionId", id, 80));
314                                         parameters.Add (CreateParameter <string> (factory, "@ApplicationName", appName, 255));
315                                         
316                                         cmd.ExecuteNonQuery();
317
318                                         if (actionFlags == SessionStateActions.InitializeItem)
319                                                 item = CreateNewStoreData (context, (int)sessionConfig.Timeout.TotalMinutes);
320                                         else
321                                                 item = Deserialize (context, serializedItems, timeout);
322                                 }
323                         } catch (Exception ex) {
324                                 throw new ProviderException ("Unable to retrieve session item from database.", ex);
325                         } finally {
326                                 if (reader != null)
327                                         reader.Close ();
328                                 
329                                 conn.Close();
330                         } 
331
332                         return item;
333                 }
334
335                 string Serialize (SessionStateItemCollection items)
336                 {
337 #if NET_4_0
338                         GZipStream gzip = null;
339 #endif
340                         Stream output;
341                         MemoryStream ms = null;
342                         BinaryWriter writer = null;
343                         
344                         try {
345                                 ms = new MemoryStream ();
346 #if NET_4_0
347                                 if (sessionConfig.CompressionEnabled)
348                                         output = gzip = new GZipStream (ms, CompressionMode.Compress, true);
349                                 else
350 #endif
351                                         output = ms;
352                                 writer = new BinaryWriter (output);
353
354                                 if (items != null)
355                                         items.Serialize (writer);
356 #if NET_4_0
357                                 if (gzip != null)
358                                         gzip.Close ();
359 #endif
360                                 writer.Close ();
361                                 return Convert.ToBase64String (ms.ToArray ());
362                         } finally {
363 #if NET_4_0
364                                 if (writer != null)
365                                         writer.Dispose ();
366                                 if (gzip != null)
367                                         gzip.Dispose ();
368 #else
369                                 if (writer != null)
370                                         ((IDisposable)writer).Dispose ();
371 #endif
372                                 if (ms != null)
373                                         ms.Dispose ();
374                         }
375                 }
376
377                 SessionStateStoreData Deserialize (HttpContext context, string serializedItems, int timeout)
378                 {
379                         MemoryStream ms = null;
380                         Stream input;
381                         BinaryReader reader = null;
382 #if NET_4_0
383                         GZipStream gzip = null;
384 #endif
385                         try {
386                                 ms = new MemoryStream (Convert.FromBase64String (serializedItems));
387                                 var sessionItems = new SessionStateItemCollection ();
388
389                                 if (ms.Length > 0) {
390 #if NET_4_0
391                                         if (sessionConfig.CompressionEnabled)
392                                                 input = gzip = new GZipStream (ms, CompressionMode.Decompress, true);
393                                         else
394 #endif
395                                                 input = ms;
396                                         
397                                         reader = new BinaryReader (input);
398                                         sessionItems = SessionStateItemCollection.Deserialize (reader);
399 #if NET_4_0
400                                         if (gzip != null)
401                                                 gzip.Close ();
402 #endif
403                                         reader.Close ();
404                                 }
405
406                                 return new SessionStateStoreData (sessionItems, SessionStateUtility.GetSessionStaticObjects (context), timeout);
407                         } finally {
408 #if NET_4_0
409                                 if (reader != null)
410                                         reader.Dispose ();
411                                 if (gzip != null)
412                                         gzip.Dispose ();
413 #else
414                                 if (reader != null)
415                                         ((IDisposable)reader).Dispose ();
416 #endif
417                                 if (ms != null)
418                                         ms.Dispose ();
419                         }
420                 }
421
422                 public override void ReleaseItemExclusive (HttpContext context, string id, object lockId)
423                 {
424                         DbProviderFactory factory = ProviderFactory;
425                         DbConnection conn = CreateConnection (factory);
426                         DbCommand cmd = CreateCommand (factory, conn,
427                                                        "UPDATE Sessions SET Locked = 0, Expires = @Expires WHERE SessionId = @SessionId AND ApplicationName = @ApplicationName AND LockId = @LockId");
428
429                         DbParameterCollection parameters = cmd.Parameters;
430                         
431                         parameters.Add (CreateParameter <DateTime> (factory, "@Expires", DateTime.Now.AddMinutes(sessionConfig.Timeout.TotalMinutes)));
432                         parameters.Add (CreateParameter <string> (factory, "@SessionId", id, 80));
433                         parameters.Add (CreateParameter <string> (factory, "@ApplicationName", ApplicationName, 255));
434                         parameters.Add (CreateParameter <int> (factory, "@LockId", (int)lockId));
435
436                         try {
437                                 conn.Open ();
438                                 cmd.ExecuteNonQuery ();
439                         } catch (Exception ex) {
440                                 throw new ProviderException ("Error releasing item in database.", ex);
441                         } finally {
442                                 conn.Close();
443                         }      
444                 }
445
446                 public override void RemoveItem (HttpContext context, string id, object lockId, SessionStateStoreData item)
447                 {
448                         DbProviderFactory factory = ProviderFactory;
449                         DbConnection conn = CreateConnection (factory);
450                         DbCommand cmd = CreateCommand (factory, conn,
451                                                        "DELETE FROM Sessions WHERE SessionId = @SessionId AND ApplicationName = @ApplicationName AND LockId = @LockId");
452
453                         DbParameterCollection parameters = cmd.Parameters;
454                         parameters.Add (CreateParameter <string> (factory, "@SessionId", id, 80));
455                         parameters.Add (CreateParameter <string> (factory, "@ApplicationName", ApplicationName, 255));
456                         parameters.Add (CreateParameter <int> (factory, "@LockId", (int)lockId));
457
458                         try {
459                                 conn.Open ();
460                                 cmd.ExecuteNonQuery ();
461                         } catch (Exception ex) {
462                                 throw new ProviderException ("Error removing item from database.", ex);
463                         } finally {
464                                 conn.Close();
465                         } 
466                 }
467
468                 public override void CreateUninitializedItem (HttpContext context, string id, int timeout)
469                 {
470                         DbProviderFactory factory = ProviderFactory;
471                         DbConnection conn = CreateConnection (factory);
472                         DbCommand cmd = CreateCommand (factory, conn,
473                                                        "INSERT INTO Sessions (SessionId, ApplicationName, Created, Expires, LockDate, LockId, Timeout, Locked, SessionItems, Flags) Values (@SessionId, @ApplicationName, @Created, @Expires, @LockDate, @LockId , @Timeout, @Locked, @SessionItems, @Flags)");
474
475                         DateTime now = DateTime.Now;
476                         DbParameterCollection parameters = cmd.Parameters;
477                         parameters.Add (CreateParameter <string> (factory, "@SessionId", id, 80));
478                         parameters.Add (CreateParameter <string> (factory, "@ApplicationName", ApplicationName, 255));
479                         parameters.Add (CreateParameter <DateTime> (factory, "@Created", now));
480                         parameters.Add (CreateParameter <DateTime> (factory, "@Expires", now.AddMinutes ((double)timeout)));
481                         parameters.Add (CreateParameter <DateTime> (factory, "@LockDate", now));
482                         parameters.Add (CreateParameter <int> (factory, "@LockId", 0));
483                         parameters.Add (CreateParameter <int> (factory, "@Timeout", timeout));
484                         parameters.Add (CreateParameter <bool> (factory, "@Locked", false));
485                         parameters.Add (CreateParameter <string> (factory, "@SessionItems", String.Empty));
486                         parameters.Add (CreateParameter <int> (factory, "@Flags", 1));
487                                 
488                         try {
489                                 conn.Open ();
490                                 cmd.ExecuteNonQuery ();
491                         } catch (Exception ex) {
492                                 throw new ProviderException ("Error creating uninitialized session item in the database.", ex);
493                         } finally {
494                                 conn.Close();
495                         }
496                 }
497
498                 public override SessionStateStoreData CreateNewStoreData (HttpContext context, int timeout)
499                 {
500                         return new SessionStateStoreData (new SessionStateItemCollection (), SessionStateUtility.GetSessionStaticObjects (context), timeout);
501                 }
502
503                 public override void ResetItemTimeout (HttpContext context, string id)
504                 {
505                         DbProviderFactory factory = ProviderFactory;
506                         DbConnection conn = CreateConnection (factory);
507                         DbCommand cmd = CreateCommand (factory, conn,
508                                                        "UPDATE Sessions SET Expires = @Expires WHERE SessionId = @SessionId AND ApplicationName = @ApplicationName");
509
510                         DbParameterCollection parameters = cmd.Parameters;
511                         parameters.Add (CreateParameter <DateTime> (factory, "@Expires", DateTime.Now.AddMinutes (sessionConfig.Timeout.TotalMinutes)));
512                         parameters.Add (CreateParameter <string> (factory, "@SessionId", id, 80));
513                         parameters.Add (CreateParameter <string> (factory, "@ApplicationName", ApplicationName, 255));
514
515                         try {
516                                 conn.Open ();
517                                 cmd.ExecuteNonQuery ();
518                         } catch (Exception ex) {
519                                 throw new ProviderException ("Error resetting session item timeout in the database.", ex);
520                         } finally {
521                                 conn.Close();
522                         }
523                 }
524
525                 public override void InitializeRequest (HttpContext context)
526                 {
527                 }
528
529                 public override void EndRequest(HttpContext context)
530                 {
531                 }
532
533                 DbConnection CreateConnection (DbProviderFactory factory)
534                 {
535                         DbConnection conn = factory.CreateConnection ();
536                         conn.ConnectionString = connectionString;
537
538                         return conn;
539                 }
540
541                 DbCommand CreateCommand (DbProviderFactory factory, DbConnection conn, string commandText)
542                 {
543                         DbCommand cmd = factory.CreateCommand ();
544                         cmd.CommandTimeout = sqlCommandTimeout;
545                         cmd.Connection = conn;
546                         cmd.CommandText = commandText;
547
548                         return cmd;
549                 }
550                 
551                 DbParameter CreateParameter <ValueType> (DbProviderFactory factory, string name, ValueType value)
552                 {
553                         return CreateParameter <ValueType> (factory, name, value, -1);
554                 }
555                 
556                 DbParameter CreateParameter <ValueType> (DbProviderFactory factory, string name, ValueType value, int size)
557                 {
558                         DbParameter param = factory.CreateParameter ();
559                         param.ParameterName = name;
560                         Type vt = typeof (ValueType);
561                         
562                         if (vt == typeof (string))
563                                 param.DbType = DbType.String;
564                         else if (vt == typeof (int))
565                                 param.DbType = DbType.Int32;
566                         else if (vt == typeof (bool))
567                                 param.DbType = DbType.Boolean;
568                         else if (vt == typeof (DateTime))
569                                 param.DbType = DbType.DateTime;
570
571                         if (size > -1)
572                                 param.Size = size;
573                         
574                         param.Value = value;
575
576                         return param;
577                 }
578                 
579         }
580 }
581 #endif