2 // System.Data.OleDb.OleDbCommand
5 // Rodrigo Moya (rodrigo@ximian.com)
6 // Tim Coleman (tim@timcoleman.com)
8 // Copyright (C) Rodrigo Moya, 2002
9 // Copyright (C) Tim Coleman, 2002
12 using System.ComponentModel;
14 using System.Data.Common;
15 using System.Collections;
16 using System.Runtime.InteropServices;
18 namespace System.Data.OleDb
21 /// Represents an SQL statement or stored procedure to execute against a data source.
23 public sealed class OleDbCommand : Component, ICloneable, IDbCommand
29 CommandType commandType;
30 OleDbConnection connection;
31 OleDbParameterCollection parameters;
32 OleDbTransaction transaction;
33 bool designTimeVisible;
34 OleDbDataReader dataReader;
35 CommandBehavior behavior;
42 public OleDbCommand ()
44 commandText = String.Empty;
45 timeout = 30; // default timeout per .NET
46 commandType = CommandType.Text;
48 parameters = new OleDbParameterCollection ();
50 designTimeVisible = false;
52 behavior = CommandBehavior.Default;
53 gdaCommand = IntPtr.Zero;
56 public OleDbCommand (string cmdText) : this ()
58 CommandText = cmdText;
61 public OleDbCommand (string cmdText, OleDbConnection connection)
64 Connection = connection;
67 public OleDbCommand (string cmdText,
68 OleDbConnection connection,
69 OleDbTransaction transaction) : this (cmdText, connection)
71 this.transaction = transaction;
74 #endregion // Constructors
78 public string CommandText
88 public int CommandTimeout {
97 public CommandType CommandType {
106 public OleDbConnection Connection {
115 public bool DesignTimeVisible {
117 return designTimeVisible;
120 designTimeVisible = value;
124 public OleDbParameterCollection Parameters {
133 public OleDbTransaction Transaction {
142 public UpdateRowSource UpdatedRowSource {
145 throw new NotImplementedException ();
149 throw new NotImplementedException ();
153 IDbConnection IDbCommand.Connection {
158 Connection = (OleDbConnection) value;
162 IDataParameterCollection IDbCommand.Parameters {
168 IDbTransaction IDbCommand.Transaction {
173 Transaction = (OleDbTransaction) value;
177 #endregion // Properties
182 public void Cancel ()
184 throw new NotImplementedException ();
187 public OleDbParameter CreateParameter ()
189 return new OleDbParameter ();
192 IDbDataParameter IDbCommand.CreateParameter ()
194 return CreateParameter ();
198 protected override void Dispose (bool disposing)
200 throw new NotImplementedException ();
203 private void SetupGdaCommand ()
207 switch (commandType) {
208 case CommandType.TableDirect :
209 type = GdaCommandType.Table;
211 case CommandType.StoredProcedure :
212 type = GdaCommandType.Procedure;
214 case CommandType.Text :
216 type = GdaCommandType.Sql;
220 if (gdaCommand != IntPtr.Zero) {
221 libgda.gda_command_set_text (gdaCommand, commandText);
222 libgda.gda_command_set_command_type (gdaCommand, type);
224 gdaCommand = libgda.gda_command_new (commandText, type, 0);
227 //libgda.gda_command_set_transaction
230 public int ExecuteNonQuery ()
232 if (connection == null)
233 throw new InvalidOperationException ("connection == null");
234 if (connection.State == ConnectionState.Closed)
235 throw new InvalidOperationException ("State == Closed");
236 // FIXME: a third check is mentioned in .NET docs
238 IntPtr gdaConnection = connection.GdaConnection;
239 IntPtr gdaParameterList = parameters.GdaParameterList;
242 return libgda.gda_connection_execute_non_query (gdaConnection,
247 public OleDbDataReader ExecuteReader ()
249 return ExecuteReader (CommandBehavior.Default);
252 IDataReader IDbCommand.ExecuteReader ()
254 return ExecuteReader ();
257 public OleDbDataReader ExecuteReader (CommandBehavior behavior)
259 ArrayList results = new ArrayList ();
263 if (connection.State != ConnectionState.Open)
264 throw new InvalidOperationException ("State != Open");
266 this.behavior = behavior;
268 IntPtr gdaConnection = connection.GdaConnection;
269 IntPtr gdaParameterList = parameters.GdaParameterList;
271 /* execute the command */
273 rs_list = libgda.gda_connection_execute_command (
277 if (rs_list != IntPtr.Zero) {
278 glist_node = (GdaList) Marshal.PtrToStructure (rs_list, typeof (GdaList));
280 while (glist_node != null) {
281 results.Add (glist_node.data);
282 if (glist_node.next == IntPtr.Zero)
285 glist_node = (GdaList) Marshal.PtrToStructure (glist_node.next,
288 dataReader = new OleDbDataReader (this, results);
289 dataReader.NextResult ();
295 IDataReader IDbCommand.ExecuteReader (CommandBehavior behavior)
297 return ExecuteReader (behavior);
300 public object ExecuteScalar ()
303 OleDbDataReader reader = ExecuteReader ();
304 if (reader == null) {
307 if (!reader.Read ()) {
311 object o = reader.GetValue (0);
317 object ICloneable.Clone ()
319 throw new NotImplementedException ();
323 public void Prepare ()
325 throw new NotImplementedException ();
328 public void ResetCommandTimeout ()