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
)]
48 [DefaultEvent( "RecordsAffected")]
49 public sealed class OleDbCommand
:
51 , ICloneable
, IDbCommand
55 const int DEFAULT_COMMAND_TIMEOUT
= 30;
59 CommandType commandType
;
60 OleDbConnection connection
;
61 OleDbParameterCollection parameters
;
62 OleDbTransaction transaction
;
63 bool designTimeVisible
;
64 OleDbDataReader dataReader
;
65 CommandBehavior behavior
;
67 UpdateRowSource updatedRowSource
;
75 public OleDbCommand ()
77 timeout
= DEFAULT_COMMAND_TIMEOUT
;
78 commandType
= CommandType
.Text
;
79 parameters
= new OleDbParameterCollection ();
80 behavior
= CommandBehavior
.Default
;
81 gdaCommand
= IntPtr
.Zero
;
82 designTimeVisible
= true;
83 this.updatedRowSource
= UpdateRowSource
.Both
;
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")]
109 [EditorAttribute ("Microsoft.VSDesigner.Data.ADO.Design.OleDbCommandTextEditor, "+ Consts
.AssemblyMicrosoft_VSDesigner
, "System.Drawing.Design.UITypeEditor, "+ Consts
.AssemblySystem_Drawing
)]
110 [RefreshPropertiesAttribute (RefreshProperties
.All
)]
115 if (commandText
== null)
135 [DataCategory ("Data")]
136 [DefaultValue ("Text")]
137 [RefreshPropertiesAttribute (RefreshProperties
.All
)]
140 CommandType CommandType
{
149 [DataCategory ("Behavior")]
150 [DefaultValue (null)]
151 [EditorAttribute ("Microsoft.VSDesigner.Data.Design.DbConnectionEditor, "+ Consts
.AssemblyMicrosoft_VSDesigner
, "System.Drawing.Design.UITypeEditor, "+ Consts
.AssemblySystem_Drawing
)]
152 public new OleDbConnection Connection
{
161 [BrowsableAttribute (false)]
162 [DesignOnlyAttribute (true)]
163 [DefaultValue (true)]
164 [EditorBrowsable(EditorBrowsableState
.Never
)]
167 bool DesignTimeVisible
{
169 return designTimeVisible
;
172 designTimeVisible
= value;
176 [DataCategory ("Data")]
177 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility
.Content
)]
178 public new OleDbParameterCollection Parameters
{
179 get { return parameters; }
180 internal set { parameters = value; }
183 [BrowsableAttribute (false)]
184 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility
.Hidden
)]
185 public new OleDbTransaction Transaction
{
194 [DataCategory ("Behavior")]
195 [DefaultValue (UpdateRowSource
.Both
)]
199 UpdateRowSource UpdatedRowSource
{
200 get { return updatedRowSource; }
202 ExceptionHelper
.CheckEnumValue (typeof (UpdateRowSource
), value);
203 updatedRowSource
= value;
207 IDbConnection IDbCommand
.Connection
{
212 Connection
= (OleDbConnection
) value;
216 IDataParameterCollection IDbCommand
.Parameters
{
222 IDbTransaction IDbCommand
.Transaction
{
227 Transaction
= (OleDbTransaction
) value;
231 #endregion // Properties
240 throw new NotImplementedException ();
243 public new OleDbParameter
CreateParameter ()
245 return new OleDbParameter ();
249 protected override void Dispose (bool disposing
)
259 private void SetupGdaCommand ()
263 switch (commandType
) {
264 case CommandType
.TableDirect
:
265 type
= GdaCommandType
.Table
;
267 case CommandType
.StoredProcedure
:
268 type
= GdaCommandType
.Procedure
;
270 case CommandType
.Text
:
272 type
= GdaCommandType
.Sql
;
276 if (gdaCommand
!= IntPtr
.Zero
) {
277 libgda
.gda_command_set_text (gdaCommand
, CommandText
);
278 libgda
.gda_command_set_command_type (gdaCommand
, type
);
280 gdaCommand
= libgda
.gda_command_new (CommandText
, type
, 0);
283 //libgda.gda_command_set_transaction
288 int ExecuteNonQuery ()
290 if (connection
== null)
291 throw new InvalidOperationException ("connection == null");
292 if (connection
.State
== ConnectionState
.Closed
)
293 throw new InvalidOperationException ("State == Closed");
294 // FIXME: a third check is mentioned in .NET docs
296 IntPtr gdaConnection
= connection
.GdaConnection
;
297 IntPtr gdaParameterList
= parameters
.GdaParameterList
;
300 return libgda
.gda_connection_execute_non_query (gdaConnection
,
305 public new OleDbDataReader
ExecuteReader ()
307 return ExecuteReader (behavior
);
310 IDataReader IDbCommand
.ExecuteReader ()
312 return ExecuteReader ();
315 public new OleDbDataReader
ExecuteReader (CommandBehavior behavior
)
317 ArrayList results
= new ArrayList ();
321 if (connection
.State
!= ConnectionState
.Open
)
322 throw new InvalidOperationException ("State != Open");
324 this.behavior
= behavior
;
326 IntPtr gdaConnection
= connection
.GdaConnection
;
327 IntPtr gdaParameterList
= parameters
.GdaParameterList
;
329 /* execute the command */
331 rs_list
= libgda
.gda_connection_execute_command (
335 if (rs_list
!= IntPtr
.Zero
) {
336 glist_node
= (GdaList
) Marshal
.PtrToStructure (rs_list
, typeof (GdaList
));
338 while (glist_node
!= null) {
339 results
.Add (glist_node
.data
);
340 if (glist_node
.next
== IntPtr
.Zero
)
343 glist_node
= (GdaList
) Marshal
.PtrToStructure (glist_node
.next
,
346 dataReader
= new OleDbDataReader (this, results
);
347 dataReader
.NextResult ();
353 IDataReader IDbCommand
.ExecuteReader (CommandBehavior behavior
)
355 return ExecuteReader (behavior
);
360 object ExecuteScalar ()
363 OleDbDataReader reader
= ExecuteReader ();
364 if (reader
== null) {
367 if (!reader
.Read ()) {
371 object o
= reader
.GetValue (0);
377 OleDbCommand
Clone ()
379 OleDbCommand command
= new OleDbCommand ();
380 command
.CommandText
= this.CommandText
;
381 command
.CommandTimeout
= this.CommandTimeout
;
382 command
.CommandType
= this.CommandType
;
383 command
.Connection
= this.Connection
;
384 command
.DesignTimeVisible
= this.DesignTimeVisible
;
385 command
.Parameters
= this.Parameters
;
386 command
.Transaction
= this.Transaction
;
390 object ICloneable
.Clone ()
400 throw new NotImplementedException ();
403 public void ResetCommandTimeout ()
405 timeout
= DEFAULT_COMMAND_TIMEOUT
;
408 protected override DbParameter
CreateDbParameter ()
410 return (DbParameter
) CreateParameter ();
413 protected override DbDataReader
ExecuteDbDataReader (CommandBehavior behavior
)
415 return (DbDataReader
) ExecuteReader (behavior
);
418 protected override DbConnection DbConnection
{
419 get { return Connection; }
420 set { Connection = (OleDbConnection) value; }
423 protected override DbParameterCollection DbParameterCollection
{
424 get { return Parameters; }
427 protected override DbTransaction DbTransaction
{
428 get { return Transaction; }
429 set { Transaction = (OleDbTransaction) value; }