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;
26 bool readerIsOpen = false;
33 : this (String.Empty, null, null)
37 public TdsCommand (string commandText)
38 : this (commandText, null, null)
42 public TdsCommand (string commandText, TdsConnection connection)
43 : this (commandText, connection, null)
47 public TdsCommand (string commandText, TdsConnection connection, TdsTransaction transaction)
49 this.commandText = commandText;
50 this.transaction = transaction;
51 this.commandType = CommandType.Text;
52 this.connection = connection;
55 #endregion // Constructors
59 public string CommandText {
60 get { return commandText; }
61 set { commandText = value; }
64 public int CommandTimeout {
65 get { return commandTimeout; }
66 set { commandTimeout = value; }
69 public CommandType CommandType {
70 get { return commandType; }
71 set { commandType = value; }
74 public TdsConnection Connection {
75 get { return connection; }
76 set { connection = value; }
79 IDbConnection IDbCommand.Connection {
80 get { return Connection; }
82 if (!(value is TdsConnection))
83 throw new ArgumentException ();
84 Connection = (TdsConnection) value;
88 IDataParameterCollection IDbCommand.Parameters {
89 get { return Parameters; }
92 IDbTransaction IDbCommand.Transaction {
93 get { return Transaction; }
95 if (!(value is TdsTransaction))
96 throw new ArgumentException ();
97 Transaction = (TdsTransaction) value;
101 public TdsParameterCollection Parameters {
102 get { return parameters; }
106 get { return connection.Tds; }
109 public TdsTransaction Transaction {
110 get { return transaction; }
111 set { transaction = value; }
115 public UpdateRowSource UpdatedRowSource {
116 get { throw new NotImplementedException (); }
117 set { throw new NotImplementedException (); }
120 #endregion // Properties
125 public void Cancel ()
127 throw new NotImplementedException ();
131 TdsParameter CreateParameter ()
133 throw new NotImplementedException ();
136 public int ExecuteNonQuery ()
138 if (connection == null)
139 throw new TdsException ("The connection is not set.");
140 if (connection.State != ConnectionState.Open)
141 throw new TdsException ("The connection is closed.");
142 if (commandText == String.Empty || commandText == null)
143 throw new TdsException ("The command text is not set.");
145 return connection.Tds.ExecuteNonQuery (FormatQuery (commandText, commandType));
148 public TdsDataReader ExecuteReader ()
150 return ExecuteReader (CommandBehavior.Default);
154 public TdsDataReader ExecuteReader (CommandBehavior behavior)
156 if (connection == null)
157 throw new TdsException ("The connection is not set.");
158 if (connection.State != ConnectionState.Open)
159 throw new TdsException ("The connection is closed.");
160 if (commandText == String.Empty || commandText == null)
161 throw new TdsException ("The command text is not set.");
162 connection.Tds.ExecuteQuery (FormatQuery (commandText, commandType));
164 return new TdsDataReader (this);
168 public object ExecuteScalar ()
170 throw new NotImplementedException ();
173 private static string FormatQuery (string commandText, CommandType commandType)
175 switch (commandType) {
176 case CommandType.Text :
178 case CommandType.TableDirect :
179 return String.Format ("select * from {0}", commandText);
180 case CommandType.StoredProcedure :
181 return String.Format ("exec {0}", commandText);
183 throw new TdsException ("Invalid command type");
187 object ICloneable.Clone()
189 throw new NotImplementedException ();
192 IDbDataParameter IDbCommand.CreateParameter ()
194 return CreateParameter ();
197 IDataReader IDbCommand.ExecuteReader ()
199 return ExecuteReader ();
202 IDataReader IDbCommand.ExecuteReader (CommandBehavior behavior)
204 return ExecuteReader (behavior);
208 public void Prepare ()
210 throw new NotImplementedException ();
214 internal void SkipToEnd ()
217 while (GetMoreResults (tds, false) || updateCount != 1);
221 #endregion // Methods