DBParams.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. using System.Data;
  2. using System.Data.Common;
  3. namespace com.cloudsoft.utils
  4. {
  5. /// <summary>
  6. /// Database Parameters class.
  7. /// Allows parameters to be added to a DBHelper.
  8. /// Can only be instatiated from a DBHelper.
  9. /// JF 12/2007 - DBParams can now represent either an DbCommand or an DbCommand. Which one
  10. /// is decided when it is created by DBHelper. Once it is created though the client doesn't need to
  11. /// know which one it is dealing with.
  12. /// </summary>
  13. public class DBParams
  14. {
  15. /// <summary>
  16. /// DataType enumerator.
  17. /// </summary>
  18. /// <remarks>
  19. /// <list type="bullet">
  20. /// <item><term>VarChar</term><description> - Varchar (string)</description></item>
  21. /// <item><term>Long</term><description> - Long integer</description></item>
  22. /// <item><term>Double</term><description> - Double (float)</description></item>
  23. /// <item><term>DateTime</term><description> - Date</description></item>
  24. /// <item><term>LongVarChar</term><description> - Clob</description></item>
  25. /// </list>
  26. /// </remarks>
  27. public enum DataType {
  28. /// <summary>Varchar</summary>
  29. VarChar,
  30. /// <summary>Int</summary>
  31. Int,
  32. /// <summary>Long</summary>
  33. Long,
  34. /// <summary>Double</summary>
  35. Double,
  36. /// <summary>DateTime</summary>
  37. DateTime,
  38. /// <summary>LongVarChar</summary>
  39. LongVarChar,
  40. /// <summary>BLOB</summary>
  41. BLOB,
  42. /// <summary>Clob</summary>
  43. Clob,
  44. /// <summary>Xml</summary>
  45. Xml
  46. };
  47. internal DbCommand Command;
  48. /// <summary>
  49. /// Oracle type equivalent of the oleType function. Returns the SqlDbType from the general
  50. /// DBParams.DataType.
  51. /// </summary>
  52. /// <param name="eDT">A non-provider specific datatype.</param>
  53. /// <returns>An SqlDbType.</returns>
  54. private DbType _ODACType(DataType eDT)
  55. {
  56. switch (eDT)
  57. {
  58. case DataType.DateTime: return DbType.DateTime;
  59. case DataType.Int: return DbType.Int32;
  60. case DataType.Long: return DbType.Int64;
  61. case DataType.Double: return DbType.Decimal;
  62. case DataType.LongVarChar: return DbType.String;
  63. case DataType.BLOB: return DbType.Binary;
  64. case DataType.Clob: return DbType.String;
  65. default: return DbType.String;
  66. }
  67. }
  68. /// <summary>
  69. /// DbCommand based DBParams constructor.
  70. /// </summary>
  71. /// <param name="oCommand">The DbCommand to wrap.</param>
  72. internal DBParams(DbCommand oCommand)
  73. {
  74. Command = oCommand;
  75. }
  76. /// <summary>
  77. /// Add Parameter method for the DbCommand
  78. /// </summary>
  79. /// <param name="zParamName"></param>
  80. /// <param name="eDT"></param>
  81. public void add(String zParamName, DataType eDT, object value)
  82. {
  83. //Command.Parameters.Add(new DbParameter(zParamName, _ODACType(eDT), value, System.Data.ParameterDirection.Input));
  84. }
  85. /// <summary>
  86. /// Adds a Input Parameter to the DBHelper
  87. /// </summary>
  88. /// <param name="zParamName">Parameter Name</param>
  89. /// <param name="eDT">DataType</param>
  90. /// <param name="iSize">Size of the parameter</param>
  91. /// <param name="oValue">Value of the Parameter</param>
  92. public void add(String zParamName, DataType eDT, int iSize, Object oValue)
  93. {
  94. //Set the size to 1 for longvarchars...
  95. if ((eDT == DataType.LongVarChar) && (oValue == null))
  96. iSize = 1;
  97. //Adds the parameter...
  98. DbParameter dbParam = Command.CreateParameter();
  99. dbParam.ParameterName = zParamName;
  100. dbParam.DbType = _ODACType(eDT);
  101. dbParam.Size = iSize;
  102. dbParam.Value = oValue;
  103. Command.Parameters.Add(dbParam);
  104. }
  105. /// <summary>
  106. /// Adds a Input Parameter to the DBHelper without a value
  107. /// </summary>
  108. /// <param name="zParamName">Parameter Name</param>
  109. /// <param name="eDT">DataType</param>
  110. /// <param name="iSize">Size of the parameter</param>
  111. public void add(String zParamName, DataType eDT, int iSize)
  112. {
  113. //Add the parameter...
  114. DbParameter dbParam = Command.CreateParameter();
  115. dbParam.ParameterName = zParamName;
  116. dbParam.DbType = _ODACType(eDT);
  117. dbParam.Size = iSize;
  118. Command.Parameters.Add(dbParam);
  119. }
  120. /// <summary>
  121. /// Adds a Input Parameter to the DBHelper without a value or size
  122. /// </summary>
  123. /// <param name="zParamName">Parameter Name</param>
  124. /// <param name="eDT">DataType</param>
  125. /// <param name="iSize">Size of the parameter</param>
  126. public void add(String zParamName, DataType eDT)
  127. {
  128. //Add the parameter...
  129. var dbParam = Command.CreateParameter();
  130. dbParam.ParameterName = zParamName;
  131. dbParam.DbType = _ODACType(eDT);
  132. Command.Parameters.Add(dbParam);
  133. }
  134. /// <summary>
  135. /// Sets the input parameter value for a given parameter name
  136. /// </summary>
  137. /// <param name="zParamName">Parameter Name</param>
  138. /// <param name="oValue">Value to set the input parameter to</param>
  139. public void set(String zParamName, Object oValue)
  140. {
  141. //Set the parameter value...
  142. Command.Parameters[zParamName].Value = oValue;
  143. }
  144. /// <summary>
  145. /// Sets the input parameter value for a given parameter index
  146. /// </summary>
  147. /// <param name="index">Parameter Index</param>
  148. /// <param name="oValue">Value to set the input parameter to</param>
  149. public void set(int index, Object oValue)
  150. {
  151. //Set the parameter value...
  152. Command.Parameters[index].Value = oValue;
  153. }
  154. public void set(string zParam, Object oValue, DbType iType)
  155. {
  156. DbParameter oParam = null;
  157. if (Command.Parameters.Contains(zParam)) {
  158. oParam = Command.Parameters[zParam];
  159. oParam.Value = oValue;
  160. }
  161. else
  162. {
  163. oParam = Command.CreateParameter();
  164. oParam.ParameterName = zParam;
  165. if (iType != null)
  166. oParam.DbType = iType;
  167. oParam.Value = oValue;
  168. Command.Parameters.Add(oParam);
  169. }
  170. }
  171. /// <summary>
  172. /// Adds an Output Parameter
  173. /// </summary>
  174. /// <param name="zParamName">Parameter Name</param>
  175. /// <param name="eDT">Data Type</param>
  176. /// <param name="iSize">Size of the parameter</param>
  177. public void addOutput(String zParamName, DataType eDT, int iSize)
  178. {
  179. //Add the output parameter...
  180. var dbParam = Command.CreateParameter();
  181. dbParam.ParameterName = zParamName;
  182. dbParam.DbType = _ODACType(eDT);
  183. dbParam.Size = iSize;
  184. dbParam.Direction = ParameterDirection.Output;
  185. }
  186. /// <summary>
  187. /// Exposes the Parameters for the DbCommand.
  188. /// </summary>
  189. /// <returns>DbParameterCollection</returns>
  190. public DbParameterCollection ODACParameters()
  191. {
  192. return Command.Parameters;
  193. }
  194. /// <summary>
  195. /// Closes the appropriate underlying connection.
  196. /// </summary>
  197. public void CloseConnection()
  198. {
  199. Command.Connection.Close();
  200. }
  201. /// <summary>
  202. /// Closes the appropriate underlying command.
  203. /// </summary>
  204. public void CloseCommand()
  205. {
  206. Command.Connection.Dispose();
  207. Command.Dispose();
  208. }
  209. /// <summary>
  210. /// Executes the appropriate underlying command.
  211. /// </summary>
  212. /// <returns>int</returns>
  213. public int ExecuteNonQuery()
  214. {
  215. return Command.ExecuteNonQuery();
  216. }
  217. public DbCommand AsDbCommand()
  218. {
  219. return Command;
  220. }
  221. }
  222. }