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
13 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
15 // Permission is hereby granted, free of charge, to any person obtaining
16 // a copy of this software and associated documentation files (the
17 // "Software"), to deal in the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 using System.ComponentModel;
37 using System.Data.Common;
38 using System.Collections;
39 using System.Runtime.InteropServices;
41 namespace System.Data.OleDb
44 /// Represents an SQL statement or stored procedure to execute against a data source.
46 [DesignerAttribute ("Microsoft.VSDesigner.Data.VS.OleDbCommandDesigner, "+ Consts.AssemblyMicrosoft_VSDesigner, "System.ComponentModel.Design.IDesigner")]
47 [ToolboxItemAttribute ("System.Drawing.Design.ToolboxItem, "+ Consts.AssemblySystem_Drawing)]
49 [DefaultEvent( "RecordsAffected")]
51 public sealed class OleDbCommand :
57 , ICloneable, IDbCommand
63 CommandType commandType;
64 OleDbConnection connection;
65 OleDbParameterCollection parameters;
66 OleDbTransaction transaction;
67 bool designTimeVisible;
68 OleDbDataReader dataReader;
69 CommandBehavior behavior;
76 public OleDbCommand ()
78 commandText = String.Empty;
79 timeout = 30; // default timeout per .NET
80 commandType = CommandType.Text;
81 parameters = new OleDbParameterCollection ();
82 behavior = CommandBehavior.Default;
83 gdaCommand = IntPtr.Zero;
86 public OleDbCommand (string cmdText) : this ()
88 CommandText = cmdText;
91 public OleDbCommand (string cmdText, OleDbConnection connection)
94 Connection = connection;
97 public OleDbCommand (string cmdText, OleDbConnection connection,
98 OleDbTransaction transaction) : this (cmdText, connection)
100 this.transaction = transaction;
103 #endregion // Constructors
107 [DataCategory ("Data")]
110 [DataSysDescriptionAttribute ("Command text to execute.")]
112 [EditorAttribute ("Microsoft.VSDesigner.Data.ADO.Design.OleDbCommandTextEditor, "+ Consts.AssemblyMicrosoft_VSDesigner, "System.Drawing.Design.UITypeEditor, "+ Consts.AssemblySystem_Drawing)]
113 [RefreshPropertiesAttribute (RefreshProperties.All)]
129 [DataSysDescriptionAttribute ("Time to wait for command to execute.")]
145 [DataCategory ("Data")]
146 [DefaultValue ("Text")]
148 [DataSysDescriptionAttribute ("How to interpret the CommandText.")]
150 [RefreshPropertiesAttribute (RefreshProperties.All)]
155 CommandType CommandType {
164 [DataCategory ("Behavior")]
166 [DataSysDescriptionAttribute ("Connection used by the command.")]
168 [DefaultValue (null)]
169 [EditorAttribute ("Microsoft.VSDesigner.Data.Design.DbConnectionEditor, "+ Consts.AssemblyMicrosoft_VSDesigner, "System.Drawing.Design.UITypeEditor, "+ Consts.AssemblySystem_Drawing )]
170 public new OleDbConnection Connection {
179 [BrowsableAttribute (false)]
180 [DesignOnlyAttribute (true)]
181 [DefaultValue (true)]
183 [EditorBrowsable(EditorBrowsableState.Never)]
189 bool DesignTimeVisible {
191 return designTimeVisible;
194 designTimeVisible = value;
198 [DataCategory ("Data")]
200 [DataSysDescriptionAttribute ("The parameters collection.")]
202 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Content)]
203 public new OleDbParameterCollection Parameters {
209 [BrowsableAttribute (false)]
211 [DataSysDescriptionAttribute ("The transaction used by the command.")]
213 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
214 public new OleDbTransaction Transaction {
223 [DataCategory ("Behavior")]
224 [DefaultValue (UpdateRowSource.Both)]
226 [DataSysDescriptionAttribute ("When used by a DataAdapter.Update, how command results are applied to the current DataRow.")]
232 UpdateRowSource UpdatedRowSource {
235 throw new NotImplementedException ();
239 throw new NotImplementedException ();
243 IDbConnection IDbCommand.Connection {
248 Connection = (OleDbConnection) value;
252 IDataParameterCollection IDbCommand.Parameters {
258 IDbTransaction IDbCommand.Transaction {
263 Transaction = (OleDbTransaction) value;
267 #endregion // Properties
278 throw new NotImplementedException ();
281 public new OleDbParameter CreateParameter ()
283 return new OleDbParameter ();
287 IDbDataParameter IDbCommand.CreateParameter ()
289 return CreateParameter ();
294 protected override void Dispose (bool disposing)
296 throw new NotImplementedException ();
299 private void SetupGdaCommand ()
303 switch (commandType) {
304 case CommandType.TableDirect :
305 type = GdaCommandType.Table;
307 case CommandType.StoredProcedure :
308 type = GdaCommandType.Procedure;
310 case CommandType.Text :
312 type = GdaCommandType.Sql;
316 if (gdaCommand != IntPtr.Zero) {
317 libgda.gda_command_set_text (gdaCommand, commandText);
318 libgda.gda_command_set_command_type (gdaCommand, type);
320 gdaCommand = libgda.gda_command_new (commandText, type, 0);
323 //libgda.gda_command_set_transaction
330 int ExecuteNonQuery ()
332 if (connection == null)
333 throw new InvalidOperationException ("connection == null");
334 if (connection.State == ConnectionState.Closed)
335 throw new InvalidOperationException ("State == Closed");
336 // FIXME: a third check is mentioned in .NET docs
338 IntPtr gdaConnection = connection.GdaConnection;
339 IntPtr gdaParameterList = parameters.GdaParameterList;
342 return libgda.gda_connection_execute_non_query (gdaConnection,
347 public new OleDbDataReader ExecuteReader ()
349 return ExecuteReader (behavior);
352 IDataReader IDbCommand.ExecuteReader ()
354 return ExecuteReader ();
357 public new OleDbDataReader ExecuteReader (CommandBehavior behavior)
359 ArrayList results = new ArrayList ();
363 if (connection.State != ConnectionState.Open)
364 throw new InvalidOperationException ("State != Open");
366 this.behavior = behavior;
368 IntPtr gdaConnection = connection.GdaConnection;
369 IntPtr gdaParameterList = parameters.GdaParameterList;
371 /* execute the command */
373 rs_list = libgda.gda_connection_execute_command (
377 if (rs_list != IntPtr.Zero) {
378 glist_node = (GdaList) Marshal.PtrToStructure (rs_list, typeof (GdaList));
380 while (glist_node != null) {
381 results.Add (glist_node.data);
382 if (glist_node.next == IntPtr.Zero)
385 glist_node = (GdaList) Marshal.PtrToStructure (glist_node.next,
388 dataReader = new OleDbDataReader (this, results);
389 dataReader.NextResult ();
395 IDataReader IDbCommand.ExecuteReader (CommandBehavior behavior)
397 return ExecuteReader (behavior);
404 object ExecuteScalar ()
407 OleDbDataReader reader = ExecuteReader ();
408 if (reader == null) {
411 if (!reader.Read ()) {
415 object o = reader.GetValue (0);
422 public OleDbCommand Clone ()
424 throw new NotImplementedException ();
429 object ICloneable.Clone ()
431 throw new NotImplementedException ();
441 throw new NotImplementedException ();
444 public void ResetCommandTimeout ()
451 protected override DbParameter CreateDbParameter ()
453 throw new NotImplementedException ();
457 protected override DbDataReader ExecuteDbDataReader (CommandBehavior behavior)
459 throw new NotImplementedException ();
463 protected override DbConnection DbConnection {
464 get { throw new NotImplementedException (); }
465 set { throw new NotImplementedException (); }
469 protected override DbParameterCollection DbParameterCollection {
470 get { throw new NotImplementedException (); }
474 protected override DbTransaction DbTransaction {
475 get { throw new NotImplementedException (); }
476 set { throw new NotImplementedException (); }