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; }
218 if (transaction != null && connection.Transaction != null && connection.Transaction.IsOpen)
219 throw new InvalidOperationException ("The Connection property was changed while a transaction was in progress.");
226 [DefaultValue (true)]
229 [EditorBrowsable (EditorBrowsableState.Never)]
235 bool DesignTimeVisible {
236 get { return designTimeVisible; }
237 set { designTimeVisible = value; }
241 [DataSysDescription ("The parameters collection.")]
243 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
248 SqlParameterCollection Parameters {
249 get { return parameters; }
253 get { return Connection.Tds; }
257 IDbConnection IDbCommand.Connection {
258 get { return Connection; }
260 if (!(value == null || value is SqlConnection))
261 throw new InvalidCastException ("The value was not a valid SqlConnection.");
262 Connection = (SqlConnection) value;
266 IDataParameterCollection IDbCommand.Parameters {
267 get { return Parameters; }
270 IDbTransaction IDbCommand.Transaction {
271 get { return Transaction; }
273 if (!(value == null || value is SqlTransaction))
274 throw new ArgumentException ();
275 Transaction = (SqlTransaction) value;
282 [DataSysDescription ("The transaction used by the command.")]
284 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
285 public new SqlTransaction Transaction {
286 get { return transaction; }
287 set { transaction = value; }
291 [DataSysDescription ("When used by a DataAdapter.Update, how command results are applied to the current DataRow.")]
293 [DefaultValue (UpdateRowSource.Both)]
298 UpdateRowSource UpdatedRowSource {
299 get { return updatedRowSource; }
301 ExceptionHelper.CheckEnumValue (typeof (UpdateRowSource), value);
302 updatedRowSource = value;
308 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
309 public SqlNotificationRequest Notification {
310 get { return notification; }
311 set { notification = value; }
314 [DefaultValue (true)]
315 public bool NotificationAutoEnlist {
316 get { return notificationAutoEnlist; }
317 set { notificationAutoEnlist = value; }
330 if (Connection == null || Connection.Tds == null)
332 Connection.Tds.Cancel ();
336 public SqlCommand Clone ()
338 return new SqlCommand (commandText, connection, transaction, commandType, updatedRowSource, designTimeVisible, commandTimeout, parameters);
342 internal void CloseDataReader (bool moreResults)
344 Connection.DataReader = null;
346 if ((behavior & CommandBehavior.CloseConnection) != 0)
349 // Reset the behavior
350 behavior = CommandBehavior.Default;
352 Tds.SequentialAccess = false;
355 public new SqlParameter CreateParameter ()
357 return new SqlParameter ();
360 internal void DeriveParameters ()
362 if (commandType != CommandType.StoredProcedure)
363 throw new InvalidOperationException (String.Format ("SqlCommand DeriveParameters only supports CommandType.StoredProcedure, not CommandType.{0}", commandType));
364 ValidateCommand ("DeriveParameters");
366 string procName = CommandText;
367 string schemaName = String.Empty;
368 int dotPosition = procName.IndexOf ('.');
369 if (dotPosition >= 0) {
370 schemaName = procName.Substring (0, dotPosition);
371 procName = procName.Substring (dotPosition + 1);
374 SqlParameterCollection localParameters = new SqlParameterCollection (this);
375 localParameters.Add ("@procedure_name", SqlDbType.NVarChar, procName.Length).Value = procName;
376 if (schemaName.Length > 0)
377 localParameters.Add ("@procedure_schema", SqlDbType.NVarChar, schemaName.Length).Value = schemaName;
379 string sql = "sp_procedure_params_rowset";
382 Connection.Tds.ExecProc (sql, localParameters.MetaParameters, 0, true);
383 } catch (TdsTimeoutException ex) {
384 throw SqlException.FromTdsInternalException ((TdsInternalException) ex);
385 } catch (TdsInternalException ex) {
387 throw SqlException.FromTdsInternalException ((TdsInternalException) ex);
390 SqlDataReader reader = new SqlDataReader (this);
392 object[] dbValues = new object[reader.FieldCount];
394 while (reader.Read ()) {
395 reader.GetValues (dbValues);
396 parameters.Add (new SqlParameter (dbValues));
400 if (parameters.Count == 0)
401 throw new InvalidOperationException ("Stored procedure '" + procName + "' does not exist.");
404 private void Execute (CommandBehavior behavior, bool wantResults)
407 Connection.Tds.RecordsAffected = -1;
408 TdsMetaParameterCollection parms = Parameters.MetaParameters;
409 foreach (TdsMetaParameter param in parms) {
410 param.Validate (index++);
413 if (preparedStatement == null) {
414 bool schemaOnly = ((behavior & CommandBehavior.SchemaOnly) > 0);
415 bool keyInfo = ((behavior & CommandBehavior.KeyInfo) > 0);
417 StringBuilder sql1 = new StringBuilder ();
418 StringBuilder sql2 = new StringBuilder ();
420 if (schemaOnly || keyInfo)
421 sql1.Append ("SET FMTONLY OFF;");
423 sql1.Append ("SET NO_BROWSETABLE ON;");
424 sql2.Append ("SET NO_BROWSETABLE OFF;");
427 sql1.Append ("SET FMTONLY ON;");
428 sql2.Append ("SET FMTONLY OFF;");
431 switch (CommandType) {
432 case CommandType.StoredProcedure:
434 if (keyInfo || schemaOnly)
435 Connection.Tds.Execute (sql1.ToString ());
436 Connection.Tds.ExecProc (CommandText, parms, CommandTimeout, wantResults);
437 if (keyInfo || schemaOnly)
438 Connection.Tds.Execute (sql2.ToString ());
439 } catch (TdsTimeoutException ex) {
440 // If it is a timeout exception there can be many reasons:
441 // 1) Network is down/server is down/not reachable
442 // 2) Somebody has an exclusive lock on Table/DB
443 // In any of these cases, don't close the connection. Let the user do it
444 throw SqlException.FromTdsInternalException ((TdsInternalException) ex);
445 } catch (TdsInternalException ex) {
447 throw SqlException.FromTdsInternalException ((TdsInternalException) ex);
450 case CommandType.Text:
452 if (sql2.Length > 0) {
453 sql = String.Format ("{0}{1};{2}", sql1.ToString (), CommandText, sql2.ToString ());
455 sql = String.Format ("{0}{1}", sql1.ToString (), CommandText);
458 Connection.Tds.Execute (sql, parms, CommandTimeout, wantResults);
459 } catch (TdsTimeoutException ex) {
460 throw SqlException.FromTdsInternalException ((TdsInternalException) ex);
461 } catch (TdsInternalException ex) {
463 throw SqlException.FromTdsInternalException ((TdsInternalException) ex);
470 Connection.Tds.ExecPrepared (preparedStatement, parms, CommandTimeout, wantResults);
471 } catch (TdsTimeoutException ex) {
472 throw SqlException.FromTdsInternalException ((TdsInternalException) ex);
473 } catch (TdsInternalException ex) {
475 throw SqlException.FromTdsInternalException ((TdsInternalException) ex);
484 int ExecuteNonQuery ()
486 ValidateCommand ("ExecuteNonQuery");
488 behavior = CommandBehavior.Default;
491 Execute (CommandBehavior.Default, false);
492 result = Connection.Tds.RecordsAffected;
494 catch (TdsTimeoutException e) {
495 throw SqlException.FromTdsInternalException ((TdsInternalException) e);
498 GetOutputParameters ();
502 public new SqlDataReader ExecuteReader ()
504 return ExecuteReader (CommandBehavior.Default);
507 public new SqlDataReader ExecuteReader (CommandBehavior behavior)
509 ValidateCommand ("ExecuteReader");
510 this.behavior = behavior;
511 if ((behavior & CommandBehavior.SequentialAccess) != 0)
512 Tds.SequentialAccess = true;
513 Execute (behavior, true);
514 Connection.DataReader = new SqlDataReader (this);
516 return Connection.DataReader;
523 object ExecuteScalar ()
526 object result = null;
527 ValidateCommand ("ExecuteScalar");
528 behavior = CommandBehavior.Default;
529 Execute (CommandBehavior.Default, true);
532 if (Connection.Tds.NextResult () && Connection.Tds.NextRow ())
533 result = Connection.Tds.ColumnValues[0];
535 if (commandType == CommandType.StoredProcedure) {
536 Connection.Tds.SkipToEnd ();
537 GetOutputParameters ();
539 } catch (TdsTimeoutException ex) {
540 throw SqlException.FromTdsInternalException ((TdsInternalException) ex);
541 } catch (TdsInternalException ex) {
543 throw SqlException.FromTdsInternalException ((TdsInternalException) ex);
549 CloseDataReader (true);
553 public XmlReader ExecuteXmlReader ()
555 ValidateCommand ("ExecuteXmlReader");
556 behavior = CommandBehavior.Default;
558 Execute (CommandBehavior.Default, true);
559 } catch (TdsTimeoutException e) {
560 throw SqlException.FromTdsInternalException ((TdsInternalException) e);
563 SqlDataReader dataReader = new SqlDataReader (this);
564 SqlXmlTextReader textReader = new SqlXmlTextReader (dataReader);
565 XmlReader xmlReader = new XmlTextReader (textReader);
569 internal void GetOutputParameters ()
571 IList list = Connection.Tds.OutputParameters;
573 if (list != null && list.Count > 0) {
576 foreach (SqlParameter parameter in parameters) {
577 if (parameter.Direction != ParameterDirection.Input) {
578 parameter.Value = list [index];
581 if (index >= list.Count)
587 object ICloneable.Clone ()
589 return new SqlCommand (commandText, connection, transaction, commandType, updatedRowSource, designTimeVisible, commandTimeout, parameters);
594 IDbDataParameter IDbCommand.CreateParameter ()
596 return CreateParameter ();
599 IDataReader IDbCommand.ExecuteReader ()
601 return ExecuteReader ();
604 IDataReader IDbCommand.ExecuteReader (CommandBehavior behavior)
606 return ExecuteReader (behavior);
611 protected override void Dispose (bool disposing)
613 if (disposed) return;
617 base.Dispose (disposing);
628 ValidateCommand ("Prepare");
630 if (CommandType == CommandType.StoredProcedure)
634 foreach (SqlParameter param in Parameters)
635 param.CheckIfInitialized ();
636 } catch (Exception e) {
637 throw new InvalidOperationException ("SqlCommand.Prepare requires " + e.Message);
640 preparedStatement = Connection.Tds.Prepare (CommandText, Parameters.MetaParameters);
643 public void ResetCommandTimeout ()
645 commandTimeout = DEFAULT_COMMAND_TIMEOUT;
648 private void Unprepare ()
650 Connection.Tds.Unprepare (preparedStatement);
651 preparedStatement = null;
654 private void ValidateCommand (string method)
656 if (Connection == null)
658 throw new NullReferenceException (String.Format ("{0} requires a Connection object to continue.", method));
660 throw new InvalidOperationException (String.Format ("{0} requires a Connection object to continue.", method));
662 if (Connection.Transaction != null && transaction != Connection.Transaction)
663 throw new InvalidOperationException ("The Connection object does not have the same transaction as the command object.");
664 if (Connection.State != ConnectionState.Open)
666 throw new NullReferenceException (String.Format ("ExecuteNonQuery requires an open Connection object to continue. This connection is closed.", method));
668 throw new InvalidOperationException (String.Format ("ExecuteNonQuery requires an open Connection object to continue. This connection is closed.", method));
670 if (CommandText.Length == 0)
671 throw new InvalidOperationException ("The command text for this Command has not been set.");
672 if (Connection.DataReader != null)
673 throw new InvalidOperationException ("There is already an open DataReader associated with this Connection which must be closed first.");
674 if (Connection.XmlReader != null)
675 throw new InvalidOperationException ("There is already an open XmlReader associated with this Connection which must be closed first.");
677 if (method.StartsWith ("Begin") && !Connection.AsyncProcessing)
678 throw new InvalidOperationException ("This Connection object is not " +
679 "in Asynchronous mode. Use 'Asynchronous" +
680 " Processing = true' to set it.");
685 protected override DbParameter CreateDbParameter ()
687 return CreateParameter ();
690 protected override DbDataReader ExecuteDbDataReader (CommandBehavior behavior)
692 return ExecuteReader (behavior);
695 protected override DbConnection DbConnection {
696 get { return Connection; }
697 set { Connection = (SqlConnection) value; }
700 protected override DbParameterCollection DbParameterCollection {
701 get { return Parameters; }
704 protected override DbTransaction DbTransaction {
705 get { return Transaction; }
706 set { Transaction = (SqlTransaction) value; }
710 #endregion // Methods
713 #region Asynchronous Methods
715 internal IAsyncResult BeginExecuteInternal (CommandBehavior behavior,
717 AsyncCallback callback,
720 IAsyncResult ar = null;
721 Connection.Tds.RecordsAffected = -1;
722 TdsMetaParameterCollection parms = Parameters.MetaParameters;
723 if (preparedStatement == null) {
724 bool schemaOnly = ((behavior & CommandBehavior.SchemaOnly) > 0);
725 bool keyInfo = ((behavior & CommandBehavior.KeyInfo) > 0);
727 StringBuilder sql1 = new StringBuilder ();
728 StringBuilder sql2 = new StringBuilder ();
730 if (schemaOnly || keyInfo)
731 sql1.Append ("SET FMTONLY OFF;");
733 sql1.Append ("SET NO_BROWSETABLE ON;");
734 sql2.Append ("SET NO_BROWSETABLE OFF;");
737 sql1.Append ("SET FMTONLY ON;");
738 sql2.Append ("SET FMTONLY OFF;");
740 switch (CommandType) {
741 case CommandType.StoredProcedure:
744 if (keyInfo || schemaOnly)
745 prolog = sql1.ToString ();
746 if (keyInfo || schemaOnly)
747 epilog = sql2.ToString ();
749 Connection.Tds.BeginExecuteProcedure (prolog,
756 } catch (TdsTimeoutException ex) {
757 throw SqlException.FromTdsInternalException ((TdsInternalException) ex);
758 } catch (TdsInternalException ex) {
760 throw SqlException.FromTdsInternalException ((TdsInternalException) ex);
763 case CommandType.Text:
764 string sql = String.Format ("{0}{1};{2}", sql1.ToString (), CommandText, sql2.ToString ());
767 ar = Connection.Tds.BeginExecuteQuery (sql, parms, callback, state);
769 ar = Connection.Tds.BeginExecuteNonQuery (sql, parms, callback, state);
770 } catch (TdsTimeoutException ex) {
771 throw SqlException.FromTdsInternalException ((TdsInternalException) ex);
772 } catch (TdsInternalException ex) {
774 throw SqlException.FromTdsInternalException ((TdsInternalException) ex);
781 Connection.Tds.ExecPrepared (preparedStatement, parms, CommandTimeout, wantResults);
782 } catch (TdsTimeoutException ex) {
783 throw SqlException.FromTdsInternalException ((TdsInternalException) ex);
784 } catch (TdsInternalException ex) {
786 throw SqlException.FromTdsInternalException ((TdsInternalException) ex);
792 internal void EndExecuteInternal (IAsyncResult ar)
794 SqlAsyncResult sqlResult = ( (SqlAsyncResult) ar);
795 Connection.Tds.WaitFor (sqlResult.InternalResult);
796 Connection.Tds.CheckAndThrowException (sqlResult.InternalResult);
799 public IAsyncResult BeginExecuteNonQuery ()
801 return BeginExecuteNonQuery (null, null);
804 public IAsyncResult BeginExecuteNonQuery (AsyncCallback callback, object stateObject)
806 ValidateCommand ("BeginExecuteNonQuery");
807 SqlAsyncResult ar = new SqlAsyncResult (callback, stateObject);
808 ar.EndMethod = "EndExecuteNonQuery";
809 ar.InternalResult = BeginExecuteInternal (CommandBehavior.Default, false, ar.BubbleCallback, ar);
813 public int EndExecuteNonQuery (IAsyncResult asyncResult)
815 ValidateAsyncResult (asyncResult, "EndExecuteNonQuery");
816 EndExecuteInternal (asyncResult);
818 int ret = Connection.Tds.RecordsAffected;
820 GetOutputParameters ();
821 ((SqlAsyncResult) asyncResult).Ended = true;
825 public IAsyncResult BeginExecuteReader ()
827 return BeginExecuteReader (null, null, CommandBehavior.Default);
830 public IAsyncResult BeginExecuteReader (CommandBehavior behavior)
832 return BeginExecuteReader (null, null, behavior);
835 public IAsyncResult BeginExecuteReader (AsyncCallback callback, object stateObject)
837 return BeginExecuteReader (callback, stateObject, CommandBehavior.Default);
840 public IAsyncResult BeginExecuteReader (AsyncCallback callback, object stateObject, CommandBehavior behavior)
842 ValidateCommand ("BeginExecuteReader");
843 this.behavior = behavior;
844 SqlAsyncResult ar = new SqlAsyncResult (callback, stateObject);
845 ar.EndMethod = "EndExecuteReader";
846 IAsyncResult tdsResult = BeginExecuteInternal (behavior, true,
847 ar.BubbleCallback, stateObject);
848 ar.InternalResult = tdsResult;
852 public SqlDataReader EndExecuteReader (IAsyncResult asyncResult)
854 ValidateAsyncResult (asyncResult, "EndExecuteReader");
855 EndExecuteInternal (asyncResult);
856 SqlDataReader reader = null;
858 reader = new SqlDataReader (this);
859 } catch (TdsTimeoutException e) {
860 throw SqlException.FromTdsInternalException ((TdsInternalException) e);
861 } catch (TdsInternalException e) {
862 // if behavior is closeconnection, even if it throws exception
863 // the connection has to be closed.
864 if ((behavior & CommandBehavior.CloseConnection) != 0)
866 throw SqlException.FromTdsInternalException ((TdsInternalException) e);
869 ((SqlAsyncResult) asyncResult).Ended = true;
873 public IAsyncResult BeginExecuteXmlReader (AsyncCallback callback, object stateObject)
875 ValidateCommand ("BeginExecuteXmlReader");
876 SqlAsyncResult ar = new SqlAsyncResult (callback, stateObject);
877 ar.EndMethod = "EndExecuteXmlReader";
878 ar.InternalResult = BeginExecuteInternal (behavior, true,
879 ar.BubbleCallback, stateObject);
883 public IAsyncResult BeginExecuteXmlReader ()
885 return BeginExecuteXmlReader (null, null);
889 public XmlReader EndExecuteXmlReader (IAsyncResult asyncResult)
891 ValidateAsyncResult (asyncResult, "EndExecuteXmlReader");
892 EndExecuteInternal (asyncResult);
893 SqlDataReader reader = new SqlDataReader (this);
894 SqlXmlTextReader textReader = new SqlXmlTextReader (reader);
895 XmlReader xmlReader = new XmlTextReader (textReader);
896 ((SqlAsyncResult) asyncResult).Ended = true;
900 internal void ValidateAsyncResult (IAsyncResult ar, string endMethod)
903 throw new ArgumentException ("result passed is null!");
904 if (! (ar is SqlAsyncResult))
905 throw new ArgumentException (String.Format ("cannot test validity of types {0}",
907 SqlAsyncResult result = (SqlAsyncResult) ar;
908 if (result.EndMethod != endMethod)
909 throw new InvalidOperationException (String.Format ("Mismatched {0} called for AsyncResult. " +
910 "Expected call to {1} but {0} is called instead.",
911 endMethod, result.EndMethod));
913 throw new InvalidOperationException (String.Format ("The method {0} cannot be called " +
914 "more than once for the same AsyncResult.", endMethod));
917 #endregion // Asynchronous Methods
919 public event StatementCompletedEventHandler StatementCompleted;