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; }
176 throw new ArgumentException ("The property value assigned is less than 0.");
177 commandTimeout = value;
182 [DataSysDescription ("How to interpret the CommandText.")]
184 [DefaultValue (CommandType.Text)]
185 [RefreshProperties (RefreshProperties.All)]
190 CommandType CommandType {
191 get { return commandType; }
193 if (value == CommandType.TableDirect)
195 throw new ArgumentOutOfRangeException ("CommandType.TableDirect is not supported " +
196 "by the Mono SqlClient Data Provider.");
198 throw new ArgumentException ("CommandType.TableDirect is not supported by the Mono SqlClient Data Provider.");
201 ExceptionHelper.CheckEnumValue (typeof (CommandType), value);
206 [DefaultValue (null)]
208 [DataSysDescription ("Connection used by the command.")]
210 [EditorAttribute ("Microsoft.VSDesigner.Data.Design.DbConnectionEditor, "+ Consts.AssemblyMicrosoft_VSDesigner, "System.Drawing.Design.UITypeEditor, "+ Consts.AssemblySystem_Drawing )]
215 SqlConnection Connection {
216 get { return connection; }
220 if (connection != null && connection.DataReader != null)
221 throw new InvalidOperationException ("The connection is busy fetching data.");
228 [DefaultValue (true)]
231 [EditorBrowsable (EditorBrowsableState.Never)]
237 bool DesignTimeVisible {
238 get { return designTimeVisible; }
239 set { designTimeVisible = value; }
243 [DataSysDescription ("The parameters collection.")]
245 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
250 SqlParameterCollection Parameters {
251 get { return parameters; }
255 get { return Connection.Tds; }
259 IDbConnection IDbCommand.Connection {
260 get { return Connection; }
262 if (!(value == null || value is SqlConnection))
263 throw new InvalidCastException ("The value was not a valid SqlConnection.");
264 Connection = (SqlConnection) value;
268 IDataParameterCollection IDbCommand.Parameters {
269 get { return Parameters; }
272 IDbTransaction IDbCommand.Transaction {
273 get { return Transaction; }
275 if (!(value == null || value is SqlTransaction))
276 throw new ArgumentException ();
277 Transaction = (SqlTransaction) value;
284 [DataSysDescription ("The transaction used by the command.")]
286 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
287 public new SqlTransaction Transaction {
289 if (transaction != null && !transaction.IsOpen)
296 if (connection != null && connection.DataReader != null)
297 throw new InvalidOperationException ("The connection is busy fetching data.");
304 [DataSysDescription ("When used by a DataAdapter.Update, how command results are applied to the current DataRow.")]
306 [DefaultValue (UpdateRowSource.Both)]
311 UpdateRowSource UpdatedRowSource {
312 get { return updatedRowSource; }
314 ExceptionHelper.CheckEnumValue (typeof (UpdateRowSource), value);
315 updatedRowSource = value;
321 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
322 public SqlNotificationRequest Notification {
323 get { return notification; }
324 set { notification = value; }
327 [DefaultValue (true)]
328 public bool NotificationAutoEnlist {
329 get { return notificationAutoEnlist; }
330 set { notificationAutoEnlist = value; }
343 if (Connection == null || Connection.Tds == null)
345 Connection.Tds.Cancel ();
349 public SqlCommand Clone ()
351 return new SqlCommand (commandText, connection, transaction, commandType, updatedRowSource, designTimeVisible, commandTimeout, parameters);
355 internal void CloseDataReader (bool moreResults)
357 Connection.DataReader = null;
359 if ((behavior & CommandBehavior.CloseConnection) != 0)
362 // Reset the behavior
363 behavior = CommandBehavior.Default;
365 Tds.SequentialAccess = false;
368 public new SqlParameter CreateParameter ()
370 return new SqlParameter ();
373 internal void DeriveParameters ()
375 if (commandType != CommandType.StoredProcedure)
376 throw new InvalidOperationException (String.Format ("SqlCommand DeriveParameters only supports CommandType.StoredProcedure, not CommandType.{0}", commandType));
377 ValidateCommand ("DeriveParameters");
379 string procName = CommandText;
380 string schemaName = String.Empty;
381 int dotPosition = procName.IndexOf ('.');
382 if (dotPosition >= 0) {
383 schemaName = procName.Substring (0, dotPosition);
384 procName = procName.Substring (dotPosition + 1);
387 SqlParameterCollection localParameters = new SqlParameterCollection (this);
388 localParameters.Add ("@procedure_name", SqlDbType.NVarChar, procName.Length).Value = procName;
389 if (schemaName.Length > 0)
390 localParameters.Add ("@procedure_schema", SqlDbType.NVarChar, schemaName.Length).Value = schemaName;
392 string sql = "sp_procedure_params_rowset";
395 Connection.Tds.ExecProc (sql, localParameters.MetaParameters, 0, true);
396 } catch (TdsTimeoutException ex) {
397 throw SqlException.FromTdsInternalException ((TdsInternalException) ex);
398 } catch (TdsInternalException ex) {
400 throw SqlException.FromTdsInternalException ((TdsInternalException) ex);
403 SqlDataReader reader = new SqlDataReader (this);
405 object[] dbValues = new object[reader.FieldCount];
407 while (reader.Read ()) {
408 reader.GetValues (dbValues);
409 parameters.Add (new SqlParameter (dbValues));
413 if (parameters.Count == 0)
414 throw new InvalidOperationException ("Stored procedure '" + procName + "' does not exist.");
417 private void Execute (CommandBehavior behavior, bool wantResults)
420 Connection.Tds.RecordsAffected = -1;
421 TdsMetaParameterCollection parms = Parameters.MetaParameters;
422 foreach (TdsMetaParameter param in parms) {
423 param.Validate (index++);
426 if (preparedStatement == null) {
427 bool schemaOnly = ((behavior & CommandBehavior.SchemaOnly) > 0);
428 bool keyInfo = ((behavior & CommandBehavior.KeyInfo) > 0);
430 StringBuilder sql1 = new StringBuilder ();
431 StringBuilder sql2 = new StringBuilder ();
433 if (schemaOnly || keyInfo)
434 sql1.Append ("SET FMTONLY OFF;");
436 sql1.Append ("SET NO_BROWSETABLE ON;");
437 sql2.Append ("SET NO_BROWSETABLE OFF;");
440 sql1.Append ("SET FMTONLY ON;");
441 sql2.Append ("SET FMTONLY OFF;");
444 switch (CommandType) {
445 case CommandType.StoredProcedure:
447 if (keyInfo || schemaOnly)
448 Connection.Tds.Execute (sql1.ToString ());
449 Connection.Tds.ExecProc (CommandText, parms, CommandTimeout, wantResults);
450 if (keyInfo || schemaOnly)
451 Connection.Tds.Execute (sql2.ToString ());
452 } catch (TdsTimeoutException ex) {
453 // If it is a timeout exception there can be many reasons:
454 // 1) Network is down/server is down/not reachable
455 // 2) Somebody has an exclusive lock on Table/DB
456 // In any of these cases, don't close the connection. Let the user do it
457 throw SqlException.FromTdsInternalException ((TdsInternalException) ex);
458 } catch (TdsInternalException ex) {
460 throw SqlException.FromTdsInternalException ((TdsInternalException) ex);
463 case CommandType.Text:
465 if (sql2.Length > 0) {
466 sql = String.Format ("{0}{1};{2}", sql1.ToString (), CommandText, sql2.ToString ());
468 sql = String.Format ("{0}{1}", sql1.ToString (), CommandText);
471 Connection.Tds.Execute (sql, parms, CommandTimeout, wantResults);
472 } catch (TdsTimeoutException ex) {
473 throw SqlException.FromTdsInternalException ((TdsInternalException) ex);
474 } catch (TdsInternalException ex) {
476 throw SqlException.FromTdsInternalException ((TdsInternalException) ex);
483 Connection.Tds.ExecPrepared (preparedStatement, parms, CommandTimeout, wantResults);
484 } catch (TdsTimeoutException ex) {
485 throw SqlException.FromTdsInternalException ((TdsInternalException) ex);
486 } catch (TdsInternalException ex) {
488 throw SqlException.FromTdsInternalException ((TdsInternalException) ex);
497 int ExecuteNonQuery ()
499 ValidateCommand ("ExecuteNonQuery");
501 behavior = CommandBehavior.Default;
504 Execute (CommandBehavior.Default, false);
505 result = Connection.Tds.RecordsAffected;
507 catch (TdsTimeoutException e) {
508 throw SqlException.FromTdsInternalException ((TdsInternalException) e);
511 GetOutputParameters ();
515 public new SqlDataReader ExecuteReader ()
517 return ExecuteReader (CommandBehavior.Default);
520 public new SqlDataReader ExecuteReader (CommandBehavior behavior)
522 ValidateCommand ("ExecuteReader");
523 this.behavior = behavior;
524 if ((behavior & CommandBehavior.SequentialAccess) != 0)
525 Tds.SequentialAccess = true;
526 Execute (behavior, true);
527 Connection.DataReader = new SqlDataReader (this);
529 return Connection.DataReader;
536 object ExecuteScalar ()
539 object result = null;
540 ValidateCommand ("ExecuteScalar");
541 behavior = CommandBehavior.Default;
542 Execute (CommandBehavior.Default, true);
545 if (Connection.Tds.NextResult () && Connection.Tds.NextRow ())
546 result = Connection.Tds.ColumnValues[0];
548 if (commandType == CommandType.StoredProcedure) {
549 Connection.Tds.SkipToEnd ();
550 GetOutputParameters ();
552 } catch (TdsTimeoutException ex) {
553 throw SqlException.FromTdsInternalException ((TdsInternalException) ex);
554 } catch (TdsInternalException ex) {
556 throw SqlException.FromTdsInternalException ((TdsInternalException) ex);
562 CloseDataReader (true);
566 public XmlReader ExecuteXmlReader ()
568 ValidateCommand ("ExecuteXmlReader");
569 behavior = CommandBehavior.Default;
571 Execute (CommandBehavior.Default, true);
572 } catch (TdsTimeoutException e) {
573 throw SqlException.FromTdsInternalException ((TdsInternalException) e);
576 SqlDataReader dataReader = new SqlDataReader (this);
577 SqlXmlTextReader textReader = new SqlXmlTextReader (dataReader);
578 XmlReader xmlReader = new XmlTextReader (textReader);
582 internal void GetOutputParameters ()
584 IList list = Connection.Tds.OutputParameters;
586 if (list != null && list.Count > 0) {
589 foreach (SqlParameter parameter in parameters) {
590 if (parameter.Direction != ParameterDirection.Input &&
591 parameter.Direction != ParameterDirection.ReturnValue) {
592 parameter.Value = list [index];
595 if (index >= list.Count)
601 object ICloneable.Clone ()
603 return new SqlCommand (commandText, connection, transaction, commandType, updatedRowSource, designTimeVisible, commandTimeout, parameters);
608 IDbDataParameter IDbCommand.CreateParameter ()
610 return CreateParameter ();
613 IDataReader IDbCommand.ExecuteReader ()
615 return ExecuteReader ();
618 IDataReader IDbCommand.ExecuteReader (CommandBehavior behavior)
620 return ExecuteReader (behavior);
625 protected override void Dispose (bool disposing)
627 if (disposed) return;
631 base.Dispose (disposing);
642 ValidateCommand ("Prepare");
644 if (CommandType == CommandType.StoredProcedure)
648 foreach (SqlParameter param in Parameters)
649 param.CheckIfInitialized ();
650 } catch (Exception e) {
651 throw new InvalidOperationException ("SqlCommand.Prepare requires " + e.Message);
654 preparedStatement = Connection.Tds.Prepare (CommandText, Parameters.MetaParameters);
657 public void ResetCommandTimeout ()
659 commandTimeout = DEFAULT_COMMAND_TIMEOUT;
662 private void Unprepare ()
664 Connection.Tds.Unprepare (preparedStatement);
665 preparedStatement = null;
668 private void ValidateCommand (string method)
670 if (Connection == null)
672 throw new NullReferenceException (String.Format ("{0} requires a Connection object to continue.", method));
674 throw new InvalidOperationException (String.Format ("{0} requires a Connection object to continue.", method));
676 if (Connection.Transaction != null && transaction != Connection.Transaction)
677 throw new InvalidOperationException ("The Connection object does not have the same transaction as the command object.");
678 if (Connection.State != ConnectionState.Open)
680 throw new NullReferenceException (String.Format ("ExecuteNonQuery requires an open Connection object to continue. This connection is closed.", method));
682 throw new InvalidOperationException (String.Format ("ExecuteNonQuery requires an open Connection object to continue. This connection is closed.", method));
684 if (CommandText.Length == 0)
685 throw new InvalidOperationException ("The command text for this Command has not been set.");
686 if (Connection.DataReader != null)
687 throw new InvalidOperationException ("There is already an open DataReader associated with this Connection which must be closed first.");
688 if (Connection.XmlReader != null)
689 throw new InvalidOperationException ("There is already an open XmlReader associated with this Connection which must be closed first.");
691 if (method.StartsWith ("Begin") && !Connection.AsyncProcessing)
692 throw new InvalidOperationException ("This Connection object is not " +
693 "in Asynchronous mode. Use 'Asynchronous" +
694 " Processing = true' to set it.");
699 protected override DbParameter CreateDbParameter ()
701 return CreateParameter ();
704 protected override DbDataReader ExecuteDbDataReader (CommandBehavior behavior)
706 return ExecuteReader (behavior);
709 protected override DbConnection DbConnection {
710 get { return Connection; }
711 set { Connection = (SqlConnection) value; }
714 protected override DbParameterCollection DbParameterCollection {
715 get { return Parameters; }
718 protected override DbTransaction DbTransaction {
719 get { return Transaction; }
720 set { Transaction = (SqlTransaction) value; }
724 #endregion // Methods
727 #region Asynchronous Methods
729 internal IAsyncResult BeginExecuteInternal (CommandBehavior behavior,
731 AsyncCallback callback,
734 IAsyncResult ar = null;
735 Connection.Tds.RecordsAffected = -1;
736 TdsMetaParameterCollection parms = Parameters.MetaParameters;
737 if (preparedStatement == null) {
738 bool schemaOnly = ((behavior & CommandBehavior.SchemaOnly) > 0);
739 bool keyInfo = ((behavior & CommandBehavior.KeyInfo) > 0);
741 StringBuilder sql1 = new StringBuilder ();
742 StringBuilder sql2 = new StringBuilder ();
744 if (schemaOnly || keyInfo)
745 sql1.Append ("SET FMTONLY OFF;");
747 sql1.Append ("SET NO_BROWSETABLE ON;");
748 sql2.Append ("SET NO_BROWSETABLE OFF;");
751 sql1.Append ("SET FMTONLY ON;");
752 sql2.Append ("SET FMTONLY OFF;");
754 switch (CommandType) {
755 case CommandType.StoredProcedure:
758 if (keyInfo || schemaOnly)
759 prolog = sql1.ToString ();
760 if (keyInfo || schemaOnly)
761 epilog = sql2.ToString ();
763 Connection.Tds.BeginExecuteProcedure (prolog,
770 } catch (TdsTimeoutException ex) {
771 throw SqlException.FromTdsInternalException ((TdsInternalException) ex);
772 } catch (TdsInternalException ex) {
774 throw SqlException.FromTdsInternalException ((TdsInternalException) ex);
777 case CommandType.Text:
778 string sql = String.Format ("{0}{1};{2}", sql1.ToString (), CommandText, sql2.ToString ());
781 ar = Connection.Tds.BeginExecuteQuery (sql, parms, callback, state);
783 ar = Connection.Tds.BeginExecuteNonQuery (sql, parms, callback, state);
784 } catch (TdsTimeoutException ex) {
785 throw SqlException.FromTdsInternalException ((TdsInternalException) ex);
786 } catch (TdsInternalException ex) {
788 throw SqlException.FromTdsInternalException ((TdsInternalException) ex);
795 Connection.Tds.ExecPrepared (preparedStatement, parms, CommandTimeout, wantResults);
796 } catch (TdsTimeoutException ex) {
797 throw SqlException.FromTdsInternalException ((TdsInternalException) ex);
798 } catch (TdsInternalException ex) {
800 throw SqlException.FromTdsInternalException ((TdsInternalException) ex);
806 internal void EndExecuteInternal (IAsyncResult ar)
808 SqlAsyncResult sqlResult = ( (SqlAsyncResult) ar);
809 Connection.Tds.WaitFor (sqlResult.InternalResult);
810 Connection.Tds.CheckAndThrowException (sqlResult.InternalResult);
813 public IAsyncResult BeginExecuteNonQuery ()
815 return BeginExecuteNonQuery (null, null);
818 public IAsyncResult BeginExecuteNonQuery (AsyncCallback callback, object stateObject)
820 ValidateCommand ("BeginExecuteNonQuery");
821 SqlAsyncResult ar = new SqlAsyncResult (callback, stateObject);
822 ar.EndMethod = "EndExecuteNonQuery";
823 ar.InternalResult = BeginExecuteInternal (CommandBehavior.Default, false, ar.BubbleCallback, ar);
827 public int EndExecuteNonQuery (IAsyncResult asyncResult)
829 ValidateAsyncResult (asyncResult, "EndExecuteNonQuery");
830 EndExecuteInternal (asyncResult);
832 int ret = Connection.Tds.RecordsAffected;
834 GetOutputParameters ();
835 ((SqlAsyncResult) asyncResult).Ended = true;
839 public IAsyncResult BeginExecuteReader ()
841 return BeginExecuteReader (null, null, CommandBehavior.Default);
844 public IAsyncResult BeginExecuteReader (CommandBehavior behavior)
846 return BeginExecuteReader (null, null, behavior);
849 public IAsyncResult BeginExecuteReader (AsyncCallback callback, object stateObject)
851 return BeginExecuteReader (callback, stateObject, CommandBehavior.Default);
854 public IAsyncResult BeginExecuteReader (AsyncCallback callback, object stateObject, CommandBehavior behavior)
856 ValidateCommand ("BeginExecuteReader");
857 this.behavior = behavior;
858 SqlAsyncResult ar = new SqlAsyncResult (callback, stateObject);
859 ar.EndMethod = "EndExecuteReader";
860 IAsyncResult tdsResult = BeginExecuteInternal (behavior, true,
861 ar.BubbleCallback, stateObject);
862 ar.InternalResult = tdsResult;
866 public SqlDataReader EndExecuteReader (IAsyncResult asyncResult)
868 ValidateAsyncResult (asyncResult, "EndExecuteReader");
869 EndExecuteInternal (asyncResult);
870 SqlDataReader reader = null;
872 reader = new SqlDataReader (this);
873 } catch (TdsTimeoutException e) {
874 throw SqlException.FromTdsInternalException ((TdsInternalException) e);
875 } catch (TdsInternalException e) {
876 // if behavior is closeconnection, even if it throws exception
877 // the connection has to be closed.
878 if ((behavior & CommandBehavior.CloseConnection) != 0)
880 throw SqlException.FromTdsInternalException ((TdsInternalException) e);
883 ((SqlAsyncResult) asyncResult).Ended = true;
887 public IAsyncResult BeginExecuteXmlReader (AsyncCallback callback, object stateObject)
889 ValidateCommand ("BeginExecuteXmlReader");
890 SqlAsyncResult ar = new SqlAsyncResult (callback, stateObject);
891 ar.EndMethod = "EndExecuteXmlReader";
892 ar.InternalResult = BeginExecuteInternal (behavior, true,
893 ar.BubbleCallback, stateObject);
897 public IAsyncResult BeginExecuteXmlReader ()
899 return BeginExecuteXmlReader (null, null);
903 public XmlReader EndExecuteXmlReader (IAsyncResult asyncResult)
905 ValidateAsyncResult (asyncResult, "EndExecuteXmlReader");
906 EndExecuteInternal (asyncResult);
907 SqlDataReader reader = new SqlDataReader (this);
908 SqlXmlTextReader textReader = new SqlXmlTextReader (reader);
909 XmlReader xmlReader = new XmlTextReader (textReader);
910 ((SqlAsyncResult) asyncResult).Ended = true;
914 internal void ValidateAsyncResult (IAsyncResult ar, string endMethod)
917 throw new ArgumentException ("result passed is null!");
918 if (! (ar is SqlAsyncResult))
919 throw new ArgumentException (String.Format ("cannot test validity of types {0}",
921 SqlAsyncResult result = (SqlAsyncResult) ar;
922 if (result.EndMethod != endMethod)
923 throw new InvalidOperationException (String.Format ("Mismatched {0} called for AsyncResult. " +
924 "Expected call to {1} but {0} is called instead.",
925 endMethod, result.EndMethod));
927 throw new InvalidOperationException (String.Format ("The method {0} cannot be called " +
928 "more than once for the same AsyncResult.", endMethod));
931 #endregion // Asynchronous Methods
933 public event StatementCompletedEventHandler StatementCompleted;