SqlCommand.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. //
  2. // System.Data.SqlClient.SqlCommand
  3. //
  4. // Authors:
  5. // Konstantin Triger <[email protected]>
  6. // Boris Kirzner <[email protected]>
  7. //
  8. // (C) 2005 Mainsoft Corporation (http://www.mainsoft.com)
  9. //
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System;
  31. using System.Collections;
  32. using System.Text;
  33. using System.Text.RegularExpressions;
  34. using System.Data;
  35. using System.Data.Common;
  36. using System.Data.ProviderBase;
  37. using java.sql;
  38. namespace System.Data.SqlClient
  39. {
  40. public class SqlCommand : AbstractDbCommand, IDbCommand, IDisposable, ICloneable
  41. {
  42. #region Fields
  43. #endregion // Fields
  44. #region Constructors
  45. // Initializes a new instance of the SqlCommand class.
  46. // The base constructor initializes all fields to their default values.
  47. // The following table shows initial property values for an instance of SqlCommand.
  48. public SqlCommand() : this(null, null, null)
  49. {
  50. }
  51. public SqlCommand(SqlConnection connection) : this(null, connection, null)
  52. {
  53. }
  54. // Initializes a new instance of the SqlCommand class with the text of the query.
  55. public SqlCommand(String cmdText) : this(cmdText, null, null)
  56. {
  57. }
  58. // Initializes a new instance of the SqlCommand class with the text of the query and a SqlConnection.
  59. public SqlCommand(String cmdText, SqlConnection connection) : this(cmdText, connection, null)
  60. {
  61. }
  62. // Initializes a new instance of the SqlCommand class with the text of the query, a SqlConnection, and the Transaction.
  63. public SqlCommand(
  64. String cmdText,
  65. SqlConnection connection,
  66. SqlTransaction transaction)
  67. : base(cmdText, connection, transaction)
  68. {
  69. }
  70. #endregion // Constructors
  71. #region Properties
  72. public new SqlConnection Connection
  73. {
  74. get { return (SqlConnection)base.Connection; }
  75. set { base.Connection = value; }
  76. }
  77. public new SqlParameterCollection Parameters
  78. {
  79. get {
  80. if (_parameters == null) {
  81. _parameters = CreateParameterCollection(this);
  82. }
  83. return (SqlParameterCollection)_parameters;
  84. }
  85. }
  86. public new SqlTransaction Transaction
  87. {
  88. get { return (SqlTransaction)base.Transaction; }
  89. set { base.Transaction = value; }
  90. }
  91. #if USE_DOTNET_REGEX
  92. protected override Regex StoredProcedureRegExp
  93. #else
  94. protected override java.util.regex.Pattern StoredProcedureRegExp {
  95. #endif
  96. get { return SqlStatementsHelper.NamedParameterStoredProcedureRegExp; }
  97. }
  98. protected override SimpleRegex ParameterRegExp
  99. {
  100. get { return SqlStatementsHelper.NamedParameterRegExp; }
  101. }
  102. #endregion // Properties
  103. #region Methods
  104. public new SqlDataReader ExecuteReader()
  105. {
  106. return (SqlDataReader)ExecuteReader(CommandBehavior.Default);
  107. }
  108. public new SqlDataReader ExecuteReader(CommandBehavior behavior)
  109. {
  110. return (SqlDataReader)base.ExecuteReader(behavior);
  111. }
  112. public new SqlParameter CreateParameter()
  113. {
  114. return (SqlParameter)CreateParameterInternal();
  115. }
  116. protected sealed override void CheckParameters()
  117. {
  118. // do nothing
  119. }
  120. protected override AbstractDbParameter GetUserParameter(string parameterName, IList userParametersList, int userParametersListPosition/*,int userParametersListStart,int userParameterListCount*/)
  121. {
  122. // Match match = SqlStatementsHelper.NamedParameterRegExp.Match(parameterName);
  123. // parameterName = match.Result("${USERPARAM}");
  124. // if (parameterName.Length == 0)
  125. // return null;
  126. for(int i=0; i < userParametersList.Count; i++) {
  127. AbstractDbParameter userParameter = (AbstractDbParameter)userParametersList[i];
  128. if (String.Compare(parameterName, userParameter.ParameterName.Trim(), true) == 0) {
  129. return userParameter;
  130. }
  131. }
  132. return null;
  133. }
  134. protected override AbstractDbParameter GetReturnParameter (IList userParametersList)
  135. {
  136. for(int i=0; i < userParametersList.Count; i++) {
  137. AbstractDbParameter userParameter = (AbstractDbParameter)userParametersList[i];
  138. if (userParameter.Direction == ParameterDirection.ReturnValue) {
  139. return userParameter;
  140. }
  141. }
  142. return null;
  143. }
  144. protected sealed override DbParameter CreateParameterInternal()
  145. {
  146. return new SqlParameter();
  147. }
  148. protected sealed override DbDataReader CreateReader()
  149. {
  150. return new SqlDataReader(this);
  151. }
  152. protected sealed override DbParameterCollection CreateParameterCollection(AbstractDbCommand parent)
  153. {
  154. return new SqlParameterCollection((SqlCommand)parent);
  155. }
  156. public object Clone()
  157. {
  158. SqlCommand clone = new SqlCommand();
  159. CopyTo(clone);
  160. return clone;
  161. }
  162. protected internal sealed override SystemException CreateException(SQLException e)
  163. {
  164. return new SqlException(e, Connection);
  165. }
  166. #endregion // Methods
  167. }
  168. }