2 // Mono.Data.TdsClient.TdsCommand.cs
5 // Tim Coleman (tim@timcoleman.com)
7 // Copyright (C) 2002 Tim Coleman
10 using Mono.Data.TdsClient.Internal;
12 using System.ComponentModel;
15 namespace Mono.Data.TdsClient {
16 public class TdsCommand : Component, ICloneable, IDbCommand
22 CommandType commandType;
23 TdsConnection connection;
24 TdsParameterCollection parameters;
25 TdsTransaction transaction;
32 : this (String.Empty, null, null)
36 public TdsCommand (string commandText)
37 : this (commandText, null, null)
41 public TdsCommand (string commandText, TdsConnection connection)
42 : this (commandText, connection, null)
46 public TdsCommand (string commandText, TdsConnection connection, TdsTransaction transaction)
48 this.commandText = commandText;
49 this.transaction = transaction;
50 this.commandType = CommandType.Text;
51 this.connection = connection;
54 #endregion // Constructors
58 public string CommandText {
59 get { return commandText; }
60 set { commandText = value; }
63 public int CommandTimeout {
64 get { return commandTimeout; }
65 set { commandTimeout = value; }
68 public CommandType CommandType {
69 get { return commandType; }
70 set { commandType = value; }
73 public TdsConnection Connection {
74 get { return connection; }
76 if (transaction != null && connection.Transaction != null && connection.Transaction.IsOpen)
77 throw new InvalidOperationException ("The Connection property was changed while a transaction was in progress.");
83 IDbConnection IDbCommand.Connection {
84 get { return Connection; }
86 if (!(value is TdsConnection))
87 throw new ArgumentException ();
88 Connection = (TdsConnection) value;
92 IDataParameterCollection IDbCommand.Parameters {
93 get { return Parameters; }
96 IDbTransaction IDbCommand.Transaction {
97 get { return Transaction; }
99 if (!(value is TdsTransaction))
100 throw new ArgumentException ();
101 Transaction = (TdsTransaction) value;
105 public TdsParameterCollection Parameters {
106 get { return parameters; }
110 get { return connection.Tds; }
113 public TdsTransaction Transaction {
114 get { return transaction; }
115 set { transaction = value; }
119 public UpdateRowSource UpdatedRowSource {
120 get { throw new NotImplementedException (); }
121 set { throw new NotImplementedException (); }
124 #endregion // Properties
129 public void Cancel ()
131 throw new NotImplementedException ();
135 TdsParameter CreateParameter ()
137 throw new NotImplementedException ();
140 public int ExecuteNonQuery ()
142 ValidateCommand ("ExecuteNonQuery");
143 return connection.Tds.ExecuteNonQuery (FormatQuery (commandText, commandType));
146 public TdsDataReader ExecuteReader ()
148 return ExecuteReader (CommandBehavior.Default);
151 public TdsDataReader ExecuteReader (CommandBehavior behavior)
153 ValidateCommand ("ExecuteReader");
154 connection.DataReader = new TdsDataReader (this);
155 return connection.DataReader;
159 public object ExecuteScalar ()
161 throw new NotImplementedException ();
164 private static string FormatQuery (string commandText, CommandType commandType)
166 switch (commandType) {
167 case CommandType.Text :
169 case CommandType.TableDirect :
170 return String.Format ("select * from {0}", commandText);
171 case CommandType.StoredProcedure :
172 return String.Format ("exec {0}", commandText);
174 throw new InvalidOperationException ("Invalid command type");
178 object ICloneable.Clone()
180 throw new NotImplementedException ();
183 IDbDataParameter IDbCommand.CreateParameter ()
185 return CreateParameter ();
188 IDataReader IDbCommand.ExecuteReader ()
190 return ExecuteReader ();
193 IDataReader IDbCommand.ExecuteReader (CommandBehavior behavior)
195 return ExecuteReader (behavior);
199 public void Prepare ()
201 throw new NotImplementedException ();
204 private void ValidateCommand (string method)
206 if (connection == null)
207 throw new InvalidOperationException (String.Format ("{0} requires a Connection object to continue.", method));
208 if (connection.Transaction != null && transaction != connection.Transaction)
209 throw new InvalidOperationException ("The Connection object does not have the same transaction as the command object.");
210 if (connection.State != ConnectionState.Open)
211 throw new InvalidOperationException (String.Format ("ExecuteNonQuery requires an open Connection object to continue. This connection is closed.", method));
212 if (commandText == String.Empty || commandText == null)
213 throw new InvalidOperationException ("The command text for this Command has not been set.");
214 if (connection.DataReader != null)
215 throw new InvalidOperationException ("There is already an open DataReader associated with this Connection which must be closed first.");
218 #endregion // Methods