电子药箱通讯服务端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DbString.cs 1.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /*
  2. License: http://www.apache.org/licenses/LICENSE-2.0
  3. Home page: http://code.google.com/p/dapper-dot-net/
  4. Note: to build on C# 3.0 + .NET 3.5, include the CSHARP30 compiler symbol (and yes,
  5. I know the difference between language and runtime versions; this is a compromise).
  6. */
  7. using System;
  8. using System.Collections;
  9. using System.Collections.Generic;
  10. using System.ComponentModel;
  11. using System.Data;
  12. using System.Linq;
  13. using System.Reflection;
  14. using System.Reflection.Emit;
  15. using System.Text;
  16. using System.Threading;
  17. using System.Text.RegularExpressions;
  18. namespace RDH.Data
  19. {
  20. public sealed class DbString
  21. {
  22. public DbString() { Length = -1; }
  23. public bool IsAnsi { get; set; }
  24. public bool IsFixedLength { get; set; }
  25. public int Length { get; set; }
  26. public string Value { get; set; }
  27. public void AddParameter(IDbCommand command, string name)
  28. {
  29. if (IsFixedLength && Length == -1)
  30. {
  31. throw new InvalidOperationException("If specifying IsFixedLength, a Length must also be specified");
  32. }
  33. var param = command.CreateParameter();
  34. param.ParameterName = name;
  35. param.Value = (object)Value ?? DBNull.Value;
  36. if (Length == -1 && Value != null && Value.Length <= 4000)
  37. {
  38. param.Size = 4000;
  39. }
  40. else
  41. {
  42. param.Size = Length;
  43. }
  44. param.DbType = IsAnsi ? (IsFixedLength ? DbType.AnsiStringFixedLength : DbType.AnsiString) : (IsFixedLength ? DbType.StringFixedLength : DbType.String);
  45. command.Parameters.Add(param);
  46. }
  47. }
  48. }