2 // System.Data.SqlClient.SqlCommand.cs
5 // Rodrigo Moya (rodrigo@ximian.com)
6 // Daniel Morgan (danmorg@sc.rr.com)
7 // Tim Coleman (tim@timcoleman.com)
8 // Diego Caravana (diego@toth.it)
10 // (C) Ximian, Inc 2002 http://www.ximian.com/
11 // (C) Daniel Morgan, 2002
12 // Copyright (C) Tim Coleman, 2002
16 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
18 // Permission is hereby granted, free of charge, to any person obtaining
19 // a copy of this software and associated documentation files (the
20 // "Software"), to deal in the Software without restriction, including
21 // without limitation the rights to use, copy, modify, merge, publish,
22 // distribute, sublicense, and/or sell copies of the Software, and to
23 // permit persons to whom the Software is furnished to do so, subject to
24 // the following conditions:
26 // The above copyright notice and this permission notice shall be
27 // included in all copies or substantial portions of the Software.
29 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
30 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
31 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
32 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
33 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
34 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
35 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
39 using Mono.Data.Tds.Protocol;
42 using System.Collections;
43 using System.Collections.Specialized;
44 using System.ComponentModel;
46 using System.Data.Common;
48 using System.Data.Sql;
50 using System.Runtime.InteropServices;
54 namespace System.Data.SqlClient {
55 [DesignerAttribute ("Microsoft.VSDesigner.Data.VS.SqlCommandDesigner, "+ Consts.AssemblyMicrosoft_VSDesigner, "System.ComponentModel.Design.IDesigner")]
56 [ToolboxItemAttribute ("System.Drawing.Design.ToolboxItem, "+ Consts.AssemblySystem_Drawing)]
58 [DefaultEventAttribute ("RecordsAffected")]
59 public sealed class SqlCommand : DbCommand, IDbCommand, ICloneable
61 public sealed class SqlCommand : Component, IDbCommand, ICloneable
66 const int DEFAULT_COMMAND_TIMEOUT = 30;
70 bool designTimeVisible;
72 CommandType commandType;
73 SqlConnection connection;
74 SqlTransaction transaction;
75 UpdateRowSource updatedRowSource;
76 CommandBehavior behavior = CommandBehavior.Default;
77 SqlParameterCollection parameters;
78 string preparedStatement;
80 SqlNotificationRequest notification;
81 bool notificationAutoEnlist;
89 : this (String.Empty, null, null)
93 public SqlCommand (string cmdText)
94 : this (cmdText, null, null)
98 public SqlCommand (string cmdText, SqlConnection connection)
99 : this (cmdText, connection, null)
103 public SqlCommand (string cmdText, SqlConnection connection, SqlTransaction transaction)
105 this.commandText = cmdText;
106 this.connection = connection;
107 this.transaction = transaction;
108 this.commandType = CommandType.Text;
109 this.updatedRowSource = UpdateRowSource.Both;
111 this.commandTimeout = DEFAULT_COMMAND_TIMEOUT;
113 notificationAutoEnlist = true;
115 designTimeVisible = true;
116 parameters = new SqlParameterCollection (this);
119 private SqlCommand(string commandText, SqlConnection connection, SqlTransaction transaction, CommandType commandType, UpdateRowSource updatedRowSource, bool designTimeVisible, int commandTimeout, SqlParameterCollection parameters)
121 this.commandText = commandText;
122 this.connection = connection;
123 this.transaction = transaction;
124 this.commandType = commandType;
125 this.updatedRowSource = updatedRowSource;
126 this.designTimeVisible = designTimeVisible;
127 this.commandTimeout = commandTimeout;
128 this.parameters = new SqlParameterCollection(this);
129 for (int i = 0;i < parameters.Count;i++)
130 this.parameters.Add(((ICloneable)parameters[i]).Clone());
133 #endregion // Constructors
137 internal CommandBehavior CommandBehavior {
138 get { return behavior; }
142 [DataSysDescription ("Command text to execute.")]
145 [EditorAttribute ("Microsoft.VSDesigner.Data.SQL.Design.SqlCommandTextEditor, "+ Consts.AssemblyMicrosoft_VSDesigner, "System.Drawing.Design.UITypeEditor, "+ Consts.AssemblySystem_Drawing )]
146 [RefreshProperties (RefreshProperties.All)]
153 if (commandText == null)
158 if (value != commandText && preparedStatement != null)
165 [DataSysDescription ("Time to wait for command to execute.")]
166 [DefaultValue (DEFAULT_COMMAND_TIMEOUT)]
173 get { return commandTimeout; }
177 throw new ArgumentException ("CommandTimeout",
178 "The property value assigned is less than 0.");
180 throw new ArgumentException ("The property value assigned is less than 0.");
182 commandTimeout = value;
187 [DataSysDescription ("How to interpret the CommandText.")]
189 [DefaultValue (CommandType.Text)]
190 [RefreshProperties (RefreshProperties.All)]
195 CommandType CommandType {
196 get { return commandType; }
198 if (value == CommandType.TableDirect)
200 throw new ArgumentOutOfRangeException ("CommandType.TableDirect is not supported " +
201 "by the Mono SqlClient Data Provider.");
203 throw new ArgumentException ("CommandType.TableDirect is not supported by the Mono SqlClient Data Provider.");
206 ExceptionHelper.CheckEnumValue (typeof (CommandType), value);
211 [DefaultValue (null)]
213 [DataSysDescription ("Connection used by the command.")]
215 [EditorAttribute ("Microsoft.VSDesigner.Data.Design.DbConnectionEditor, "+ Consts.AssemblyMicrosoft_VSDesigner, "System.Drawing.Design.UITypeEditor, "+ Consts.AssemblySystem_Drawing )]
220 SqlConnection Connection {
221 get { return connection; }
225 if (connection != null && connection.DataReader != null)
226 throw new InvalidOperationException ("The connection is busy fetching data.");
233 [DefaultValue (true)]
236 [EditorBrowsable (EditorBrowsableState.Never)]
242 bool DesignTimeVisible {
243 get { return designTimeVisible; }
244 set { designTimeVisible = value; }
248 [DataSysDescription ("The parameters collection.")]
250 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
255 SqlParameterCollection Parameters {
256 get { return parameters; }
260 get { return Connection.Tds; }
264 IDbConnection IDbCommand.Connection {
265 get { return Connection; }
267 if (!(value == null || value is SqlConnection))
268 throw new InvalidCastException ("The value was not a valid SqlConnection.");
269 Connection = (SqlConnection) value;
273 IDataParameterCollection IDbCommand.Parameters {
274 get { return Parameters; }
277 IDbTransaction IDbCommand.Transaction {
278 get { return Transaction; }
280 if (!(value == null || value is SqlTransaction))
281 throw new ArgumentException ();
282 Transaction = (SqlTransaction) value;
289 [DataSysDescription ("The transaction used by the command.")]
291 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
292 public new SqlTransaction Transaction {
294 if (transaction != null && !transaction.IsOpen)
301 if (connection != null && connection.DataReader != null)
302 throw new InvalidOperationException ("The connection is busy fetching data.");
309 [DataSysDescription ("When used by a DataAdapter.Update, how command results are applied to the current DataRow.")]
311 [DefaultValue (UpdateRowSource.Both)]
316 UpdateRowSource UpdatedRowSource {
317 get { return updatedRowSource; }
319 ExceptionHelper.CheckEnumValue (typeof (UpdateRowSource), value);
320 updatedRowSource = value;
326 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
327 public SqlNotificationRequest Notification {
328 get { return notification; }
329 set { notification = value; }
332 [DefaultValue (true)]
333 public bool NotificationAutoEnlist {
334 get { return notificationAutoEnlist; }
335 set { notificationAutoEnlist = value; }
348 if (Connection == null || Connection.Tds == null)
350 Connection.Tds.Cancel ();
354 public SqlCommand Clone ()
356 return new SqlCommand (commandText, connection, transaction, commandType, updatedRowSource, designTimeVisible, commandTimeout, parameters);
360 internal void CloseDataReader ()
362 if (Connection != null) {
363 Connection.DataReader = null;
365 if ((behavior & CommandBehavior.CloseConnection) != 0)
369 Tds.SequentialAccess = false;
372 // Reset the behavior
373 behavior = CommandBehavior.Default;
376 public new SqlParameter CreateParameter ()
378 return new SqlParameter ();
381 internal void DeriveParameters ()
383 if (commandType != CommandType.StoredProcedure)
384 throw new InvalidOperationException (String.Format ("SqlCommand DeriveParameters only supports CommandType.StoredProcedure, not CommandType.{0}", commandType));
385 ValidateCommand ("DeriveParameters", false);
387 string procName = CommandText;
388 string schemaName = String.Empty;
389 int dotPosition = procName.IndexOf ('.');
390 if (dotPosition >= 0) {
391 schemaName = procName.Substring (0, dotPosition);
392 procName = procName.Substring (dotPosition + 1);
395 SqlParameterCollection localParameters = new SqlParameterCollection (this);
396 localParameters.Add ("@procedure_name", SqlDbType.NVarChar, procName.Length).Value = procName;
397 if (schemaName.Length > 0)
398 localParameters.Add ("@procedure_schema", SqlDbType.NVarChar, schemaName.Length).Value = schemaName;
400 string sql = "sp_procedure_params_rowset";
403 Connection.Tds.ExecProc (sql, localParameters.MetaParameters, 0, true);
404 } catch (TdsTimeoutException ex) {
405 throw SqlException.FromTdsInternalException ((TdsInternalException) ex);
406 } catch (TdsInternalException ex) {
408 throw SqlException.FromTdsInternalException ((TdsInternalException) ex);
411 SqlDataReader reader = new SqlDataReader (this);
413 object[] dbValues = new object[reader.FieldCount];
415 while (reader.Read ()) {
416 reader.GetValues (dbValues);
417 parameters.Add (new SqlParameter (dbValues));
421 if (parameters.Count == 0)
422 throw new InvalidOperationException ("Stored procedure '" + procName + "' does not exist.");
425 private void Execute (bool wantResults)
428 Connection.Tds.RecordsAffected = -1;
429 TdsMetaParameterCollection parms = Parameters.MetaParameters;
430 foreach (TdsMetaParameter param in parms) {
431 param.Validate (index++);
434 if (preparedStatement == null) {
435 bool schemaOnly = ((behavior & CommandBehavior.SchemaOnly) > 0);
436 bool keyInfo = ((behavior & CommandBehavior.KeyInfo) > 0);
438 StringBuilder sql1 = new StringBuilder ();
439 StringBuilder sql2 = new StringBuilder ();
441 if (schemaOnly || keyInfo)
442 sql1.Append ("SET FMTONLY OFF;");
444 sql1.Append ("SET NO_BROWSETABLE ON;");
445 sql2.Append ("SET NO_BROWSETABLE OFF;");
448 sql1.Append ("SET FMTONLY ON;");
449 sql2.Append ("SET FMTONLY OFF;");
452 switch (CommandType) {
453 case CommandType.StoredProcedure:
455 if (keyInfo || schemaOnly)
456 Connection.Tds.Execute (sql1.ToString ());
457 Connection.Tds.ExecProc (CommandText, parms, CommandTimeout, wantResults);
458 if (keyInfo || schemaOnly)
459 Connection.Tds.Execute (sql2.ToString ());
460 } catch (TdsTimeoutException ex) {
461 // If it is a timeout exception there can be many reasons:
462 // 1) Network is down/server is down/not reachable
463 // 2) Somebody has an exclusive lock on Table/DB
464 // In any of these cases, don't close the connection. Let the user do it
465 throw SqlException.FromTdsInternalException ((TdsInternalException) ex);
466 } catch (TdsInternalException ex) {
468 throw SqlException.FromTdsInternalException ((TdsInternalException) ex);
471 case CommandType.Text:
473 if (sql2.Length > 0) {
474 sql = String.Format ("{0}{1};{2}", sql1.ToString (), CommandText, sql2.ToString ());
476 sql = String.Format ("{0}{1}", sql1.ToString (), CommandText);
479 Connection.Tds.Execute (sql, parms, CommandTimeout, wantResults);
480 } catch (TdsTimeoutException ex) {
481 throw SqlException.FromTdsInternalException ((TdsInternalException) ex);
482 } catch (TdsInternalException ex) {
484 throw SqlException.FromTdsInternalException ((TdsInternalException) ex);
491 Connection.Tds.ExecPrepared (preparedStatement, parms, CommandTimeout, wantResults);
492 } catch (TdsTimeoutException ex) {
493 throw SqlException.FromTdsInternalException ((TdsInternalException) ex);
494 } catch (TdsInternalException ex) {
496 throw SqlException.FromTdsInternalException ((TdsInternalException) ex);
505 int ExecuteNonQuery ()
507 ValidateCommand ("ExecuteNonQuery", false);
509 behavior = CommandBehavior.Default;
513 result = Connection.Tds.RecordsAffected;
514 } catch (TdsTimeoutException e) {
515 throw SqlException.FromTdsInternalException ((TdsInternalException) e);
518 GetOutputParameters ();
522 public new SqlDataReader ExecuteReader ()
524 return ExecuteReader (CommandBehavior.Default);
527 public new SqlDataReader ExecuteReader (CommandBehavior behavior)
529 ValidateCommand ("ExecuteReader", false);
530 this.behavior = behavior;
531 if ((behavior & CommandBehavior.SequentialAccess) != 0)
532 Tds.SequentialAccess = true;
534 Connection.DataReader = new SqlDataReader (this);
536 return Connection.DataReader;
543 object ExecuteScalar ()
546 object result = null;
548 ValidateCommand ("ExecuteScalar", false);
550 ValidateCommand ("ExecuteReader", false);
552 behavior = CommandBehavior.Default;
556 if (Connection.Tds.NextResult () && Connection.Tds.NextRow ())
557 result = Connection.Tds.ColumnValues[0];
559 if (commandType == CommandType.StoredProcedure) {
560 Connection.Tds.SkipToEnd ();
561 GetOutputParameters ();
563 } catch (TdsTimeoutException ex) {
564 throw SqlException.FromTdsInternalException ((TdsInternalException) ex);
565 } catch (TdsInternalException ex) {
567 throw SqlException.FromTdsInternalException ((TdsInternalException) ex);
576 public XmlReader ExecuteXmlReader ()
578 ValidateCommand ("ExecuteXmlReader", false);
579 behavior = CommandBehavior.Default;
582 } catch (TdsTimeoutException e) {
583 throw SqlException.FromTdsInternalException ((TdsInternalException) e);
586 SqlDataReader dataReader = new SqlDataReader (this);
587 SqlXmlTextReader textReader = new SqlXmlTextReader (dataReader);
588 XmlReader xmlReader = new XmlTextReader (textReader);
592 internal void GetOutputParameters ()
594 IList list = Connection.Tds.OutputParameters;
596 if (list != null && list.Count > 0) {
599 foreach (SqlParameter parameter in parameters) {
600 if (parameter.Direction != ParameterDirection.Input &&
601 parameter.Direction != ParameterDirection.ReturnValue) {
602 parameter.Value = list [index];
605 if (index >= list.Count)
611 object ICloneable.Clone ()
613 return new SqlCommand (commandText, connection, transaction, commandType, updatedRowSource, designTimeVisible, commandTimeout, parameters);
618 IDbDataParameter IDbCommand.CreateParameter ()
620 return CreateParameter ();
623 IDataReader IDbCommand.ExecuteReader ()
625 return ExecuteReader ();
628 IDataReader IDbCommand.ExecuteReader (CommandBehavior behavior)
630 return ExecuteReader (behavior);
635 protected override void Dispose (bool disposing)
637 if (disposed) return;
641 base.Dispose (disposing);
653 if (Connection == null)
654 throw new NullReferenceException ();
657 if (CommandType == CommandType.StoredProcedure || CommandType == CommandType.Text && Parameters.Count == 0)
660 ValidateCommand ("Prepare", false);
663 foreach (SqlParameter param in Parameters)
664 param.CheckIfInitialized ();
665 } catch (Exception e) {
666 throw new InvalidOperationException ("SqlCommand.Prepare requires " + e.Message);
669 preparedStatement = Connection.Tds.Prepare (CommandText, Parameters.MetaParameters);
672 public void ResetCommandTimeout ()
674 commandTimeout = DEFAULT_COMMAND_TIMEOUT;
677 private void Unprepare ()
679 Connection.Tds.Unprepare (preparedStatement);
680 preparedStatement = null;
683 private void ValidateCommand (string method, bool async)
685 if (Connection == null)
686 throw new InvalidOperationException (String.Format ("{0}: A Connection object is required to continue.", method));
687 if (Transaction == null && Connection.Transaction != null)
688 throw new InvalidOperationException (String.Format (
689 "{0} requires a transaction if the command's connection is in a pending transaction.",
695 if (Transaction != null && Transaction.Connection != Connection)
696 throw new InvalidOperationException ("The connection does not have the same transaction as the command.");
697 if (Connection.State != ConnectionState.Open)
698 throw new InvalidOperationException (String.Format ("{0} requires an open connection to continue. This connection is closed.", method));
699 if (CommandText.Length == 0)
700 throw new InvalidOperationException ("The command text for this Command has not been set.");
701 if (Connection.DataReader != null)
702 throw new InvalidOperationException ("There is already an open DataReader associated with this Connection which must be closed first.");
703 if (Connection.XmlReader != null)
704 throw new InvalidOperationException ("There is already an open XmlReader associated with this Connection which must be closed first.");
706 if (async && !Connection.AsyncProcessing)
707 throw new InvalidOperationException ("This Connection object is not " +
708 "in Asynchronous mode. Use 'Asynchronous" +
709 " Processing = true' to set it.");
714 protected override DbParameter CreateDbParameter ()
716 return CreateParameter ();
719 protected override DbDataReader ExecuteDbDataReader (CommandBehavior behavior)
721 return ExecuteReader (behavior);
724 protected override DbConnection DbConnection {
725 get { return Connection; }
726 set { Connection = (SqlConnection) value; }
729 protected override DbParameterCollection DbParameterCollection {
730 get { return Parameters; }
733 protected override DbTransaction DbTransaction {
734 get { return Transaction; }
735 set { Transaction = (SqlTransaction) value; }
739 #endregion // Methods
742 #region Asynchronous Methods
744 internal IAsyncResult BeginExecuteInternal (CommandBehavior behavior,
746 AsyncCallback callback,
749 IAsyncResult ar = null;
750 Connection.Tds.RecordsAffected = -1;
751 TdsMetaParameterCollection parms = Parameters.MetaParameters;
752 if (preparedStatement == null) {
753 bool schemaOnly = ((behavior & CommandBehavior.SchemaOnly) > 0);
754 bool keyInfo = ((behavior & CommandBehavior.KeyInfo) > 0);
756 StringBuilder sql1 = new StringBuilder ();
757 StringBuilder sql2 = new StringBuilder ();
759 if (schemaOnly || keyInfo)
760 sql1.Append ("SET FMTONLY OFF;");
762 sql1.Append ("SET NO_BROWSETABLE ON;");
763 sql2.Append ("SET NO_BROWSETABLE OFF;");
766 sql1.Append ("SET FMTONLY ON;");
767 sql2.Append ("SET FMTONLY OFF;");
769 switch (CommandType) {
770 case CommandType.StoredProcedure:
773 if (keyInfo || schemaOnly)
774 prolog = sql1.ToString ();
775 if (keyInfo || schemaOnly)
776 epilog = sql2.ToString ();
778 Connection.Tds.BeginExecuteProcedure (prolog,
785 } catch (TdsTimeoutException ex) {
786 throw SqlException.FromTdsInternalException ((TdsInternalException) ex);
787 } catch (TdsInternalException ex) {
789 throw SqlException.FromTdsInternalException ((TdsInternalException) ex);
792 case CommandType.Text:
793 string sql = String.Format ("{0}{1};{2}", sql1.ToString (), CommandText, sql2.ToString ());
796 ar = Connection.Tds.BeginExecuteQuery (sql, parms, callback, state);
798 ar = Connection.Tds.BeginExecuteNonQuery (sql, parms, callback, state);
799 } catch (TdsTimeoutException ex) {
800 throw SqlException.FromTdsInternalException ((TdsInternalException) ex);
801 } catch (TdsInternalException ex) {
803 throw SqlException.FromTdsInternalException ((TdsInternalException) ex);
810 Connection.Tds.ExecPrepared (preparedStatement, parms, CommandTimeout, wantResults);
811 } catch (TdsTimeoutException ex) {
812 throw SqlException.FromTdsInternalException ((TdsInternalException) ex);
813 } catch (TdsInternalException ex) {
815 throw SqlException.FromTdsInternalException ((TdsInternalException) ex);
821 internal void EndExecuteInternal (IAsyncResult ar)
823 SqlAsyncResult sqlResult = ( (SqlAsyncResult) ar);
824 Connection.Tds.WaitFor (sqlResult.InternalResult);
825 Connection.Tds.CheckAndThrowException (sqlResult.InternalResult);
828 public IAsyncResult BeginExecuteNonQuery ()
830 return BeginExecuteNonQuery (null, null);
833 public IAsyncResult BeginExecuteNonQuery (AsyncCallback callback, object stateObject)
835 ValidateCommand ("BeginExecuteNonQuery", true);
836 SqlAsyncResult ar = new SqlAsyncResult (callback, stateObject);
837 ar.EndMethod = "EndExecuteNonQuery";
838 ar.InternalResult = BeginExecuteInternal (CommandBehavior.Default, false, ar.BubbleCallback, ar);
842 public int EndExecuteNonQuery (IAsyncResult asyncResult)
844 ValidateAsyncResult (asyncResult, "EndExecuteNonQuery");
845 EndExecuteInternal (asyncResult);
847 int ret = Connection.Tds.RecordsAffected;
849 GetOutputParameters ();
850 ((SqlAsyncResult) asyncResult).Ended = true;
854 public IAsyncResult BeginExecuteReader ()
856 return BeginExecuteReader (null, null, CommandBehavior.Default);
859 public IAsyncResult BeginExecuteReader (CommandBehavior behavior)
861 return BeginExecuteReader (null, null, behavior);
864 public IAsyncResult BeginExecuteReader (AsyncCallback callback, object stateObject)
866 return BeginExecuteReader (callback, stateObject, CommandBehavior.Default);
869 public IAsyncResult BeginExecuteReader (AsyncCallback callback, object stateObject, CommandBehavior behavior)
871 ValidateCommand ("BeginExecuteReader", true);
872 this.behavior = behavior;
873 SqlAsyncResult ar = new SqlAsyncResult (callback, stateObject);
874 ar.EndMethod = "EndExecuteReader";
875 IAsyncResult tdsResult = BeginExecuteInternal (behavior, true,
876 ar.BubbleCallback, stateObject);
877 ar.InternalResult = tdsResult;
881 public SqlDataReader EndExecuteReader (IAsyncResult asyncResult)
883 ValidateAsyncResult (asyncResult, "EndExecuteReader");
884 EndExecuteInternal (asyncResult);
885 SqlDataReader reader = null;
887 reader = new SqlDataReader (this);
888 } catch (TdsTimeoutException e) {
889 throw SqlException.FromTdsInternalException ((TdsInternalException) e);
890 } catch (TdsInternalException e) {
891 // if behavior is closeconnection, even if it throws exception
892 // the connection has to be closed.
893 if ((behavior & CommandBehavior.CloseConnection) != 0)
895 throw SqlException.FromTdsInternalException ((TdsInternalException) e);
898 ((SqlAsyncResult) asyncResult).Ended = true;
902 public IAsyncResult BeginExecuteXmlReader (AsyncCallback callback, object stateObject)
904 ValidateCommand ("BeginExecuteXmlReader", true);
905 SqlAsyncResult ar = new SqlAsyncResult (callback, stateObject);
906 ar.EndMethod = "EndExecuteXmlReader";
907 ar.InternalResult = BeginExecuteInternal (behavior, true,
908 ar.BubbleCallback, stateObject);
912 public IAsyncResult BeginExecuteXmlReader ()
914 return BeginExecuteXmlReader (null, null);
918 public XmlReader EndExecuteXmlReader (IAsyncResult asyncResult)
920 ValidateAsyncResult (asyncResult, "EndExecuteXmlReader");
921 EndExecuteInternal (asyncResult);
922 SqlDataReader reader = new SqlDataReader (this);
923 SqlXmlTextReader textReader = new SqlXmlTextReader (reader);
924 XmlReader xmlReader = new XmlTextReader (textReader);
925 ((SqlAsyncResult) asyncResult).Ended = true;
929 internal void ValidateAsyncResult (IAsyncResult ar, string endMethod)
932 throw new ArgumentException ("result passed is null!");
933 if (! (ar is SqlAsyncResult))
934 throw new ArgumentException (String.Format ("cannot test validity of types {0}",
936 SqlAsyncResult result = (SqlAsyncResult) ar;
937 if (result.EndMethod != endMethod)
938 throw new InvalidOperationException (String.Format ("Mismatched {0} called for AsyncResult. " +
939 "Expected call to {1} but {0} is called instead.",
940 endMethod, result.EndMethod));
942 throw new InvalidOperationException (String.Format ("The method {0} cannot be called " +
943 "more than once for the same AsyncResult.", endMethod));
946 #endregion // Asynchronous Methods
948 public event StatementCompletedEventHandler StatementCompleted;