SessionSQLServerHandler.cs 21 KB

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