SqlCommand.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 System.Xml;
  38. using java.sql;
  39. namespace System.Data.SqlClient
  40. {
  41. public class SqlCommand : AbstractDbCommand
  42. {
  43. #region Fields
  44. #endregion // Fields
  45. #region Constructors
  46. // Initializes a new instance of the SqlCommand class.
  47. // The base constructor initializes all fields to their default values.
  48. // The following table shows initial property values for an instance of SqlCommand.
  49. public SqlCommand() : this(null, null, null)
  50. {
  51. }
  52. public SqlCommand(SqlConnection connection) : this(null, connection, null)
  53. {
  54. }
  55. // Initializes a new instance of the SqlCommand class with the text of the query.
  56. public SqlCommand(String cmdText) : this(cmdText, null, null)
  57. {
  58. }
  59. // Initializes a new instance of the SqlCommand class with the text of the query and a SqlConnection.
  60. public SqlCommand(String cmdText, SqlConnection connection) : this(cmdText, connection, null)
  61. {
  62. }
  63. // Initializes a new instance of the SqlCommand class with the text of the query, a SqlConnection, and the Transaction.
  64. public SqlCommand(
  65. String cmdText,
  66. SqlConnection connection,
  67. SqlTransaction transaction)
  68. : base(cmdText, connection, transaction)
  69. {
  70. }
  71. #endregion // Constructors
  72. #region Properties
  73. public new SqlConnection Connection
  74. {
  75. get { return (SqlConnection)base.Connection; }
  76. set { base.Connection = value; }
  77. }
  78. public new SqlParameterCollection Parameters
  79. {
  80. get {
  81. return (SqlParameterCollection)base.Parameters;
  82. }
  83. }
  84. public new SqlTransaction Transaction
  85. {
  86. get { return (SqlTransaction)base.Transaction; }
  87. set { base.Transaction = value; }
  88. }
  89. #if USE_DOTNET_REGEX
  90. protected override Regex StoredProcedureRegExp
  91. #else
  92. protected override java.util.regex.Pattern StoredProcedureRegExp {
  93. #endif
  94. get { return SqlStatementsHelper.NamedParameterStoredProcedureRegExp; }
  95. }
  96. protected override SimpleRegex ParameterRegExp
  97. {
  98. get { return SqlStatementsHelper.NamedParameterRegExp; }
  99. }
  100. #endregion // Properties
  101. #region Methods
  102. public XmlReader ExecuteXmlReader() {
  103. return SqlXmlTextReader.Create(ExecuteReader(CommandBehavior.SequentialAccess));
  104. }
  105. public new SqlDataReader ExecuteReader()
  106. {
  107. return (SqlDataReader)ExecuteReader(CommandBehavior.Default);
  108. }
  109. public new SqlDataReader ExecuteReader(CommandBehavior behavior)
  110. {
  111. return (SqlDataReader)base.ExecuteReader(behavior);
  112. }
  113. public new SqlParameter CreateParameter()
  114. {
  115. return (SqlParameter)CreateParameterInternal();
  116. }
  117. protected sealed override void CheckParameters()
  118. {
  119. // do nothing
  120. }
  121. protected override AbstractDbParameter GetUserParameter(string parameterName, IList userParametersList, int userParametersListPosition/*,int userParametersListStart,int userParameterListCount*/)
  122. {
  123. // Match match = SqlStatementsHelper.NamedParameterRegExp.Match(parameterName);
  124. // parameterName = match.Result("${USERPARAM}");
  125. // if (parameterName.Length == 0)
  126. // return null;
  127. for(int i=0; i < userParametersList.Count; i++) {
  128. AbstractDbParameter userParameter = (AbstractDbParameter)userParametersList[i];
  129. if (String.Compare(parameterName, userParameter.ParameterName.Trim(), true) == 0) {
  130. return userParameter;
  131. }
  132. }
  133. return null;
  134. }
  135. protected override AbstractDbParameter GetReturnParameter (IList userParametersList)
  136. {
  137. for(int i=0; i < userParametersList.Count; i++) {
  138. AbstractDbParameter userParameter = (AbstractDbParameter)userParametersList[i];
  139. if (userParameter.Direction == ParameterDirection.ReturnValue) {
  140. return userParameter;
  141. }
  142. }
  143. return null;
  144. }
  145. protected sealed override DbParameter CreateParameterInternal()
  146. {
  147. return new SqlParameter();
  148. }
  149. protected sealed override DbDataReader CreateReader()
  150. {
  151. return new SqlDataReader(this);
  152. }
  153. protected sealed override DbParameterCollection CreateParameterCollection(AbstractDbCommand parent)
  154. {
  155. return new SqlParameterCollection((SqlCommand)parent);
  156. }
  157. protected internal sealed override SystemException CreateException(SQLException e)
  158. {
  159. return new SqlException(e, Connection);
  160. }
  161. #endregion // Methods
  162. }
  163. }