SqlCommand.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. //
  2. // System.Data.SqlClient.SqlCommand.cs
  3. //
  4. // Author:
  5. // Rodrigo Moya ([email protected])
  6. // Daniel Morgan ([email protected])
  7. // Tim Coleman ([email protected])
  8. //
  9. // (C) Ximian, Inc 2002 http://www.ximian.com/
  10. // (C) Daniel Morgan, 2002
  11. // Copyright (C) Tim Coleman, 2002
  12. //
  13. using Mono.Data.TdsClient.Internal;
  14. using System;
  15. using System.Collections;
  16. using System.ComponentModel;
  17. using System.Data;
  18. using System.Data.Common;
  19. using System.Runtime.InteropServices;
  20. using System.Text;
  21. using System.Xml;
  22. namespace System.Data.SqlClient {
  23. public sealed class SqlCommand : Component, IDbCommand, ICloneable
  24. {
  25. #region Fields
  26. int commandTimeout;
  27. bool designTimeVisible;
  28. string commandText;
  29. CommandType commandType;
  30. SqlConnection connection;
  31. SqlTransaction transaction;
  32. SqlParameterCollection parameters = new SqlParameterCollection ();
  33. // SqlDataReader state data for ExecuteReader()
  34. private SqlDataReader dataReader = null;
  35. private string[] queries = null;
  36. private int currentQuery = -1;
  37. private CommandBehavior cmdBehavior = CommandBehavior.Default;
  38. #endregion // Fields
  39. #region Constructors
  40. public SqlCommand()
  41. : this (String.Empty, null, null)
  42. {
  43. }
  44. public SqlCommand (string commandText)
  45. : this (commandText, null, null)
  46. {
  47. commandText = commandText;
  48. }
  49. public SqlCommand (string commandText, SqlConnection connection)
  50. : this (commandText, connection, null)
  51. {
  52. Connection = connection;
  53. }
  54. public SqlCommand (string commandText, SqlConnection connection, SqlTransaction transaction)
  55. {
  56. this.commandText = commandText;
  57. this.connection = connection;
  58. this.transaction = transaction;
  59. this.commandType = CommandType.Text;
  60. this.designTimeVisible = false;
  61. this.commandTimeout = 30;
  62. }
  63. #endregion // Constructors
  64. #region Properties
  65. public string CommandText {
  66. get { return CommandText; }
  67. set { commandText = value; }
  68. }
  69. public int CommandTimeout {
  70. get { return commandTimeout; }
  71. set {
  72. if (commandTimeout < 0)
  73. throw new ArgumentException ("The property value assigned is less than 0.");
  74. commandTimeout = value;
  75. }
  76. }
  77. public CommandType CommandType {
  78. get { return commandType; }
  79. [MonoTODO ("Validate")]
  80. set { commandType = value; }
  81. }
  82. public SqlConnection Connection {
  83. get { return connection; }
  84. set {
  85. if (transaction != null && connection.Transaction != null && connection.Transaction.IsOpen)
  86. throw new InvalidOperationException ("The Connection property was changed while a transaction was in progress.");
  87. transaction = null;
  88. connection = value;
  89. }
  90. }
  91. public bool DesignTimeVisible {
  92. get { return designTimeVisible; }
  93. set { designTimeVisible = value; }
  94. }
  95. public SqlParameterCollection Parameters {
  96. get { return parameters; }
  97. }
  98. internal ITds Tds {
  99. get { return connection.Tds; }
  100. }
  101. IDbConnection IDbCommand.Connection {
  102. get { return Connection; }
  103. set {
  104. if (!(value is SqlConnection))
  105. throw new InvalidCastException ("The value was not a valid SqlConnection.");
  106. Connection = (SqlConnection) value;
  107. }
  108. }
  109. IDataParameterCollection IDbCommand.Parameters {
  110. get { return Parameters; }
  111. }
  112. IDbTransaction IDbCommand.Transaction {
  113. get { return Transaction; }
  114. set {
  115. if (!(value is SqlTransaction))
  116. throw new ArgumentException ();
  117. Transaction = (SqlTransaction) value;
  118. }
  119. }
  120. public SqlTransaction Transaction {
  121. get { return transaction; }
  122. set { transaction = value; }
  123. }
  124. [MonoTODO]
  125. public UpdateRowSource UpdatedRowSource {
  126. get { throw new NotImplementedException (); }
  127. set { throw new NotImplementedException (); }
  128. }
  129. #endregion // Fields
  130. #region Methods
  131. public void Cancel ()
  132. {
  133. if (connection == null || connection.Tds == null)
  134. return;
  135. connection.Tds.Cancel ();
  136. }
  137. public SqlParameter CreateParameter ()
  138. {
  139. return new SqlParameter ();
  140. }
  141. public int ExecuteNonQuery ()
  142. {
  143. if (connection == null)
  144. throw new InvalidOperationException ("ExecuteNonQuery requires a Connection object to continue.");
  145. if (connection.Transaction != null && transaction != connection.Transaction)
  146. throw new InvalidOperationException ("The Connection object does not have the same transaction as the command object.");
  147. if (connection.State != ConnectionState.Open)
  148. throw new InvalidOperationException ("ExecuteNonQuery requires an open Connection object to continue. This connection is closed.");
  149. if (commandText == String.Empty || commandText == null)
  150. throw new InvalidOperationException ("The command text for this Command has not been set.");
  151. return connection.Tds.ExecuteNonQuery (FormatQuery (commandText, commandType));
  152. }
  153. public SqlDataReader ExecuteReader ()
  154. {
  155. return ExecuteReader (CommandBehavior.Default);
  156. }
  157. public SqlDataReader ExecuteReader (CommandBehavior behavior)
  158. {
  159. if (connection == null)
  160. throw new InvalidOperationException ("ExecuteReader requires a Connection object to continue.");
  161. if (connection.Transaction != null && transaction != connection.Transaction)
  162. throw new InvalidOperationException ("The Connection object does not have the same transaction as the command object.");
  163. if (connection.State != ConnectionState.Open)
  164. throw new InvalidOperationException ("ExecuteReader requires an open Connection object to continue. This connection is closed.");
  165. if (commandText == String.Empty || commandText == null)
  166. throw new InvalidOperationException ("The command text for this Command has not been set.");
  167. connection.Tds.ExecuteQuery (FormatQuery (commandText, commandType));
  168. connection.DataReaderOpen = true;
  169. return new SqlDataReader (this);
  170. }
  171. [MonoTODO]
  172. public object ExecuteScalar ()
  173. {
  174. throw new NotImplementedException ();
  175. }
  176. [MonoTODO]
  177. public XmlReader ExecuteXmlReader ()
  178. {
  179. throw new NotImplementedException ();
  180. }
  181. static string FormatQuery (string commandText, CommandType commandType)
  182. {
  183. switch (commandType) {
  184. case CommandType.Text :
  185. return commandText;
  186. case CommandType.TableDirect :
  187. return String.Format ("SELECT * FROM {0}", commandText);
  188. case CommandType.StoredProcedure :
  189. return String.Format ("EXEC {0}", commandText);
  190. default:
  191. throw new InvalidOperationException ("The CommandType was not recognized.");
  192. }
  193. }
  194. [MonoTODO]
  195. object ICloneable.Clone ()
  196. {
  197. throw new NotImplementedException ();
  198. }
  199. IDbDataParameter IDbCommand.CreateParameter ()
  200. {
  201. return CreateParameter ();
  202. }
  203. IDataReader IDbCommand.ExecuteReader ()
  204. {
  205. return ExecuteReader ();
  206. }
  207. IDataReader IDbCommand.ExecuteReader (CommandBehavior behavior)
  208. {
  209. return ExecuteReader (behavior);
  210. }
  211. void IDisposable.Dispose ()
  212. {
  213. Dispose (true);
  214. }
  215. [MonoTODO]
  216. public void Prepare ()
  217. {
  218. throw new NotImplementedException ();
  219. }
  220. public void ResetCommandTimeout ()
  221. {
  222. commandTimeout = 30;
  223. }
  224. #endregion // Methods
  225. }
  226. }