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;
69 bool designTimeVisible;
71 CommandType commandType;
72 SqlConnection connection;
73 SqlTransaction transaction;
74 UpdateRowSource updatedRowSource;
75 CommandBehavior behavior = CommandBehavior.Default;
76 SqlParameterCollection parameters;
77 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 ("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 private string EscapeProcName (string name, bool schema)
384 string tmpProcName = name.Trim ();
385 int procNameLen = tmpProcName.Length;
386 char[] brkts = new char [] {'[', ']'};
387 bool foundMatching = false;
388 int start = 0, count = procNameLen;
389 int sindex = -1, eindex = -1;
391 // We try to match most of the "brackets" combination here, however
392 // there could be other combinations that may generate a different type
393 // of exception in MS.NET
395 if (procNameLen > 1) {
396 if ((sindex = tmpProcName.IndexOf ('[')) <= 0)
397 foundMatching = true;
399 foundMatching = false;
401 if (foundMatching == true && sindex > -1) {
402 eindex = tmpProcName.IndexOf (']');
403 if (sindex > eindex && eindex != -1) {
404 foundMatching = false;
405 } else if (eindex == procNameLen-1) {
406 if (tmpProcName.IndexOfAny (brkts, 1, procNameLen-2) != -1) {
407 foundMatching = false;
410 count = procNameLen - 2;
412 } else if (eindex == -1 && schema) {
413 foundMatching = true;
415 foundMatching = false;
420 procName = tmpProcName.Substring (start, count);
422 throw new ArgumentException (String.Format ("SqlCommand.CommandText property value is an invalid multipart name {0}, incorrect usage of quotes", CommandText));
424 procName = tmpProcName;
429 internal void DeriveParameters ()
431 if (commandType != CommandType.StoredProcedure)
432 throw new InvalidOperationException (String.Format ("SqlCommand DeriveParameters only supports CommandType.StoredProcedure, not CommandType.{0}", commandType));
433 ValidateCommand ("DeriveParameters", false);
435 string procName = CommandText;
436 string schemaName = String.Empty;
437 int dotPosition = procName.IndexOf ('.');
438 if (dotPosition >= 0) {
439 schemaName = procName.Substring (0, dotPosition);
440 procName = procName.Substring (dotPosition + 1);
443 procName = EscapeProcName (procName, false);
444 schemaName = EscapeProcName (schemaName, true);
446 SqlParameterCollection localParameters = new SqlParameterCollection (this);
447 localParameters.Add ("@procedure_name", SqlDbType.NVarChar, procName.Length).Value = procName;
448 if (schemaName.Length > 0)
449 localParameters.Add ("@procedure_schema", SqlDbType.NVarChar, schemaName.Length).Value = schemaName;
451 string sql = "sp_procedure_params_rowset";
454 Connection.Tds.ExecProc (sql, localParameters.MetaParameters, 0, true);
455 } catch (TdsTimeoutException ex) {
456 Connection.Tds.Reset ();
457 throw SqlException.FromTdsInternalException ((TdsInternalException) ex);
458 } catch (TdsInternalException ex) {
460 throw SqlException.FromTdsInternalException ((TdsInternalException) ex);
463 SqlDataReader reader = new SqlDataReader (this);
465 object[] dbValues = new object[reader.FieldCount];
467 while (reader.Read ()) {
468 reader.GetValues (dbValues);
469 parameters.Add (new SqlParameter (dbValues));
473 if (parameters.Count == 0)
474 throw new InvalidOperationException ("Stored procedure '" + procName + "' does not exist.");
477 private void Execute (bool wantResults)
480 Connection.Tds.RecordsAffected = -1;
481 TdsMetaParameterCollection parms = Parameters.MetaParameters;
482 foreach (TdsMetaParameter param in parms) {
483 param.Validate (index++);
486 if (preparedStatement == null) {
487 bool schemaOnly = ((behavior & CommandBehavior.SchemaOnly) > 0);
488 bool keyInfo = ((behavior & CommandBehavior.KeyInfo) > 0);
490 StringBuilder sql1 = new StringBuilder ();
491 StringBuilder sql2 = new StringBuilder ();
493 if (schemaOnly || keyInfo)
494 sql1.Append ("SET FMTONLY OFF;");
496 sql1.Append ("SET NO_BROWSETABLE ON;");
497 sql2.Append ("SET NO_BROWSETABLE OFF;");
500 sql1.Append ("SET FMTONLY ON;");
501 sql2.Append ("SET FMTONLY OFF;");
504 switch (CommandType) {
505 case CommandType.StoredProcedure:
507 if (keyInfo || schemaOnly)
508 Connection.Tds.Execute (sql1.ToString ());
509 Connection.Tds.ExecProc (CommandText, parms, CommandTimeout, wantResults);
510 if (keyInfo || schemaOnly)
511 Connection.Tds.Execute (sql2.ToString ());
512 } catch (TdsTimeoutException ex) {
513 // If it is a timeout exception there can be many reasons:
514 // 1) Network is down/server is down/not reachable
515 // 2) Somebody has an exclusive lock on Table/DB
516 // In any of these cases, don't close the connection. Let the user do it
517 throw SqlException.FromTdsInternalException ((TdsInternalException) ex);
518 Connection.Tds.Reset ();
519 } catch (TdsInternalException ex) {
521 throw SqlException.FromTdsInternalException ((TdsInternalException) ex);
524 case CommandType.Text:
526 if (sql2.Length > 0) {
527 sql = String.Format ("{0}{1};{2}", sql1.ToString (), CommandText, sql2.ToString ());
529 sql = String.Format ("{0}{1}", sql1.ToString (), CommandText);
532 Connection.Tds.Execute (sql, parms, CommandTimeout, wantResults);
533 } catch (TdsTimeoutException ex) {
534 Connection.Tds.Reset ();
535 throw SqlException.FromTdsInternalException ((TdsInternalException) ex);
536 } catch (TdsInternalException ex) {
538 throw SqlException.FromTdsInternalException ((TdsInternalException) ex);
545 Connection.Tds.ExecPrepared (preparedStatement, parms, CommandTimeout, wantResults);
546 } catch (TdsTimeoutException ex) {
547 Connection.Tds.Reset ();
548 throw SqlException.FromTdsInternalException ((TdsInternalException) ex);
549 } catch (TdsInternalException ex) {
551 throw SqlException.FromTdsInternalException ((TdsInternalException) ex);
560 int ExecuteNonQuery ()
562 ValidateCommand ("ExecuteNonQuery", false);
564 behavior = CommandBehavior.Default;
568 result = Connection.Tds.RecordsAffected;
569 } catch (TdsTimeoutException e) {
570 Connection.Tds.Reset ();
571 throw SqlException.FromTdsInternalException ((TdsInternalException) e);
574 GetOutputParameters ();
578 public new SqlDataReader ExecuteReader ()
580 return ExecuteReader (CommandBehavior.Default);
583 public new SqlDataReader ExecuteReader (CommandBehavior behavior)
585 ValidateCommand ("ExecuteReader", false);
586 if ((behavior & CommandBehavior.SingleRow) != 0)
587 behavior |= CommandBehavior.SingleResult;
588 this.behavior = behavior;
589 if ((behavior & CommandBehavior.SequentialAccess) != 0)
590 Tds.SequentialAccess = true;
592 Connection.DataReader = new SqlDataReader (this);
594 return Connection.DataReader;
601 object ExecuteScalar ()
604 object result = null;
606 ValidateCommand ("ExecuteScalar", false);
608 ValidateCommand ("ExecuteReader", false);
610 behavior = CommandBehavior.Default;
614 if (Connection.Tds.NextResult () && Connection.Tds.NextRow ())
615 result = Connection.Tds.ColumnValues[0];
617 if (commandType == CommandType.StoredProcedure) {
618 Connection.Tds.SkipToEnd ();
619 GetOutputParameters ();
621 } catch (TdsTimeoutException ex) {
622 Connection.Tds.Reset ();
623 throw SqlException.FromTdsInternalException ((TdsInternalException) ex);
624 } catch (TdsInternalException ex) {
626 throw SqlException.FromTdsInternalException ((TdsInternalException) ex);
635 public XmlReader ExecuteXmlReader ()
637 ValidateCommand ("ExecuteXmlReader", false);
638 behavior = CommandBehavior.Default;
641 } catch (TdsTimeoutException e) {
642 Connection.Tds.Reset ();
643 throw SqlException.FromTdsInternalException ((TdsInternalException) e);
646 SqlDataReader dataReader = new SqlDataReader (this);
647 SqlXmlTextReader textReader = new SqlXmlTextReader (dataReader);
648 XmlReader xmlReader = new XmlTextReader (textReader);
652 internal void GetOutputParameters ()
654 IList list = Connection.Tds.OutputParameters;
656 if (list != null && list.Count > 0) {
659 foreach (SqlParameter parameter in parameters) {
660 if (parameter.Direction != ParameterDirection.Input &&
661 parameter.Direction != ParameterDirection.ReturnValue) {
662 parameter.Value = list [index];
665 if (index >= list.Count)
671 object ICloneable.Clone ()
673 return new SqlCommand (commandText, connection, transaction, commandType, updatedRowSource, designTimeVisible, commandTimeout, parameters);
678 IDbDataParameter IDbCommand.CreateParameter ()
680 return CreateParameter ();
683 IDataReader IDbCommand.ExecuteReader ()
685 return ExecuteReader ();
688 IDataReader IDbCommand.ExecuteReader (CommandBehavior behavior)
690 return ExecuteReader (behavior);
695 protected override void Dispose (bool disposing)
697 if (disposed) return;
701 base.Dispose (disposing);
713 if (Connection == null)
714 throw new NullReferenceException ();
717 if (CommandType == CommandType.StoredProcedure || CommandType == CommandType.Text && Parameters.Count == 0)
720 ValidateCommand ("Prepare", false);
723 foreach (SqlParameter param in Parameters)
724 param.CheckIfInitialized ();
725 } catch (Exception e) {
726 throw new InvalidOperationException ("SqlCommand.Prepare requires " + e.Message);
729 preparedStatement = Connection.Tds.Prepare (CommandText, Parameters.MetaParameters);
732 public void ResetCommandTimeout ()
734 commandTimeout = DEFAULT_COMMAND_TIMEOUT;
737 private void Unprepare ()
739 Connection.Tds.Unprepare (preparedStatement);
740 preparedStatement = null;
743 private void ValidateCommand (string method, bool async)
745 if (Connection == null)
746 throw new InvalidOperationException (String.Format ("{0}: A Connection object is required to continue.", method));
747 if (Transaction == null && Connection.Transaction != null)
748 throw new InvalidOperationException (String.Format (
749 "{0} requires a transaction if the command's connection is in a pending transaction.",
755 if (Transaction != null && Transaction.Connection != Connection)
756 throw new InvalidOperationException ("The connection does not have the same transaction as the command.");
757 if (Connection.State != ConnectionState.Open)
758 throw new InvalidOperationException (String.Format ("{0} requires an open connection to continue. This connection is closed.", method));
759 if (CommandText.Length == 0)
760 throw new InvalidOperationException ("The command text for this Command has not been set.");
761 if (Connection.DataReader != null)
762 throw new InvalidOperationException ("There is already an open DataReader associated with this Connection which must be closed first.");
763 if (Connection.XmlReader != null)
764 throw new InvalidOperationException ("There is already an open XmlReader associated with this Connection which must be closed first.");
766 if (async && !Connection.AsyncProcessing)
767 throw new InvalidOperationException ("This Connection object is not " +
768 "in Asynchronous mode. Use 'Asynchronous" +
769 " Processing = true' to set it.");
774 protected override DbParameter CreateDbParameter ()
776 return CreateParameter ();
779 protected override DbDataReader ExecuteDbDataReader (CommandBehavior behavior)
781 return ExecuteReader (behavior);
784 protected override DbConnection DbConnection {
785 get { return Connection; }
786 set { Connection = (SqlConnection) value; }
789 protected override DbParameterCollection DbParameterCollection {
790 get { return Parameters; }
793 protected override DbTransaction DbTransaction {
794 get { return Transaction; }
795 set { Transaction = (SqlTransaction) value; }
799 #endregion // Methods
802 #region Asynchronous Methods
804 internal IAsyncResult BeginExecuteInternal (CommandBehavior behavior,
806 AsyncCallback callback,
809 IAsyncResult ar = null;
810 Connection.Tds.RecordsAffected = -1;
811 TdsMetaParameterCollection parms = Parameters.MetaParameters;
812 if (preparedStatement == null) {
813 bool schemaOnly = ((behavior & CommandBehavior.SchemaOnly) > 0);
814 bool keyInfo = ((behavior & CommandBehavior.KeyInfo) > 0);
816 StringBuilder sql1 = new StringBuilder ();
817 StringBuilder sql2 = new StringBuilder ();
819 if (schemaOnly || keyInfo)
820 sql1.Append ("SET FMTONLY OFF;");
822 sql1.Append ("SET NO_BROWSETABLE ON;");
823 sql2.Append ("SET NO_BROWSETABLE OFF;");
826 sql1.Append ("SET FMTONLY ON;");
827 sql2.Append ("SET FMTONLY OFF;");
829 switch (CommandType) {
830 case CommandType.StoredProcedure:
833 if (keyInfo || schemaOnly)
834 prolog = sql1.ToString ();
835 if (keyInfo || schemaOnly)
836 epilog = sql2.ToString ();
838 Connection.Tds.BeginExecuteProcedure (prolog,
845 } catch (TdsTimeoutException ex) {
846 Connection.Tds.Reset ();
847 throw SqlException.FromTdsInternalException ((TdsInternalException) ex);
848 } catch (TdsInternalException ex) {
850 throw SqlException.FromTdsInternalException ((TdsInternalException) ex);
853 case CommandType.Text:
854 string sql = String.Format ("{0}{1};{2}", sql1.ToString (), CommandText, sql2.ToString ());
857 ar = Connection.Tds.BeginExecuteQuery (sql, parms, callback, state);
859 ar = Connection.Tds.BeginExecuteNonQuery (sql, parms, callback, state);
860 } catch (TdsTimeoutException ex) {
861 Connection.Tds.Reset ();
862 throw SqlException.FromTdsInternalException ((TdsInternalException) ex);
863 } catch (TdsInternalException ex) {
865 throw SqlException.FromTdsInternalException ((TdsInternalException) ex);
872 Connection.Tds.ExecPrepared (preparedStatement, parms, CommandTimeout, wantResults);
873 } catch (TdsTimeoutException ex) {
874 Connection.Tds.Reset ();
875 throw SqlException.FromTdsInternalException ((TdsInternalException) ex);
876 } catch (TdsInternalException ex) {
878 throw SqlException.FromTdsInternalException ((TdsInternalException) ex);
884 internal void EndExecuteInternal (IAsyncResult ar)
886 SqlAsyncResult sqlResult = ( (SqlAsyncResult) ar);
887 Connection.Tds.WaitFor (sqlResult.InternalResult);
888 Connection.Tds.CheckAndThrowException (sqlResult.InternalResult);
891 public IAsyncResult BeginExecuteNonQuery ()
893 return BeginExecuteNonQuery (null, null);
896 public IAsyncResult BeginExecuteNonQuery (AsyncCallback callback, object stateObject)
898 ValidateCommand ("BeginExecuteNonQuery", true);
899 SqlAsyncResult ar = new SqlAsyncResult (callback, stateObject);
900 ar.EndMethod = "EndExecuteNonQuery";
901 ar.InternalResult = BeginExecuteInternal (CommandBehavior.Default, false, ar.BubbleCallback, ar);
905 public int EndExecuteNonQuery (IAsyncResult asyncResult)
907 ValidateAsyncResult (asyncResult, "EndExecuteNonQuery");
908 EndExecuteInternal (asyncResult);
910 int ret = Connection.Tds.RecordsAffected;
912 GetOutputParameters ();
913 ((SqlAsyncResult) asyncResult).Ended = true;
917 public IAsyncResult BeginExecuteReader ()
919 return BeginExecuteReader (null, null, CommandBehavior.Default);
922 public IAsyncResult BeginExecuteReader (CommandBehavior behavior)
924 return BeginExecuteReader (null, null, behavior);
927 public IAsyncResult BeginExecuteReader (AsyncCallback callback, object stateObject)
929 return BeginExecuteReader (callback, stateObject, CommandBehavior.Default);
932 public IAsyncResult BeginExecuteReader (AsyncCallback callback, object stateObject, CommandBehavior behavior)
934 ValidateCommand ("BeginExecuteReader", true);
935 this.behavior = behavior;
936 SqlAsyncResult ar = new SqlAsyncResult (callback, stateObject);
937 ar.EndMethod = "EndExecuteReader";
938 IAsyncResult tdsResult = BeginExecuteInternal (behavior, true,
939 ar.BubbleCallback, stateObject);
940 ar.InternalResult = tdsResult;
944 public SqlDataReader EndExecuteReader (IAsyncResult asyncResult)
946 ValidateAsyncResult (asyncResult, "EndExecuteReader");
947 EndExecuteInternal (asyncResult);
948 SqlDataReader reader = null;
950 reader = new SqlDataReader (this);
951 } catch (TdsTimeoutException e) {
952 throw SqlException.FromTdsInternalException ((TdsInternalException) e);
953 } catch (TdsInternalException e) {
954 // if behavior is closeconnection, even if it throws exception
955 // the connection has to be closed.
956 if ((behavior & CommandBehavior.CloseConnection) != 0)
958 throw SqlException.FromTdsInternalException ((TdsInternalException) e);
961 ((SqlAsyncResult) asyncResult).Ended = true;
965 public IAsyncResult BeginExecuteXmlReader (AsyncCallback callback, object stateObject)
967 ValidateCommand ("BeginExecuteXmlReader", true);
968 SqlAsyncResult ar = new SqlAsyncResult (callback, stateObject);
969 ar.EndMethod = "EndExecuteXmlReader";
970 ar.InternalResult = BeginExecuteInternal (behavior, true,
971 ar.BubbleCallback, stateObject);
975 public IAsyncResult BeginExecuteXmlReader ()
977 return BeginExecuteXmlReader (null, null);
981 public XmlReader EndExecuteXmlReader (IAsyncResult asyncResult)
983 ValidateAsyncResult (asyncResult, "EndExecuteXmlReader");
984 EndExecuteInternal (asyncResult);
985 SqlDataReader reader = new SqlDataReader (this);
986 SqlXmlTextReader textReader = new SqlXmlTextReader (reader);
987 XmlReader xmlReader = new XmlTextReader (textReader);
988 ((SqlAsyncResult) asyncResult).Ended = true;
992 internal void ValidateAsyncResult (IAsyncResult ar, string endMethod)
995 throw new ArgumentException ("result passed is null!");
996 if (! (ar is SqlAsyncResult))
997 throw new ArgumentException (String.Format ("cannot test validity of types {0}",
999 SqlAsyncResult result = (SqlAsyncResult) ar;
1000 if (result.EndMethod != endMethod)
1001 throw new InvalidOperationException (String.Format ("Mismatched {0} called for AsyncResult. " +
1002 "Expected call to {1} but {0} is called instead.",
1003 endMethod, result.EndMethod));
1005 throw new InvalidOperationException (String.Format ("The method {0} cannot be called " +
1006 "more than once for the same AsyncResult.", endMethod));
1009 #endregion // Asynchronous Methods
1011 public event StatementCompletedEventHandler StatementCompleted;