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 public sealed class OleDbCommand
: Component
, ICloneable
, IDbCommand
54 CommandType commandType
;
55 OleDbConnection connection
;
56 OleDbParameterCollection parameters
;
57 OleDbTransaction transaction
;
58 bool designTimeVisible
;
59 OleDbDataReader dataReader
;
60 CommandBehavior behavior
;
67 public OleDbCommand ()
69 commandText
= String
.Empty
;
70 timeout
= 30; // default timeout per .NET
71 commandType
= CommandType
.Text
;
73 parameters
= new OleDbParameterCollection ();
75 designTimeVisible
= false;
77 behavior
= CommandBehavior
.Default
;
78 gdaCommand
= IntPtr
.Zero
;
81 public OleDbCommand (string cmdText
) : this ()
83 CommandText
= cmdText
;
86 public OleDbCommand (string cmdText
, OleDbConnection connection
)
89 Connection
= connection
;
92 public OleDbCommand (string cmdText
,
93 OleDbConnection connection
,
94 OleDbTransaction transaction
) : this (cmdText
, connection
)
96 this.transaction
= transaction
;
99 #endregion // Constructors
103 [DataCategory ("Data")]
105 [DataSysDescriptionAttribute ("Command text to execute")]
106 [EditorAttribute ("Microsoft.VSDesigner.Data.ADO.Design.OleDbCommandTextEditor, "+ Consts
.AssemblyMicrosoft_VSDesigner
, "System.Drawing.Design.UITypeEditor, "+ Consts
.AssemblySystem_Drawing
)]
107 [RefreshPropertiesAttribute (RefreshProperties
.All
)]
108 public string CommandText
118 [DataSysDescriptionAttribute ("Time to wait for command to execute")]
120 public int CommandTimeout
{
129 [DataCategory ("Data")]
130 [DefaultValue ("Text")]
131 [DataSysDescriptionAttribute ("How to interpret the CommandText")]
132 [RefreshPropertiesAttribute (RefreshProperties
.All
)]
133 public CommandType CommandType
{
142 [DataCategory ("Behavior")]
143 [DataSysDescriptionAttribute ("Connection used by the command")]
144 [DefaultValue (null)]
145 [EditorAttribute ("Microsoft.VSDesigner.Data.Design.DbConnectionEditor, "+ Consts
.AssemblyMicrosoft_VSDesigner
, "System.Drawing.Design.UITypeEditor, "+ Consts
.AssemblySystem_Drawing
)]
146 public OleDbConnection Connection
{
155 [BrowsableAttribute (false)]
156 [DesignOnlyAttribute (true)]
157 [DefaultValue (true)]
158 public bool DesignTimeVisible
{
160 return designTimeVisible
;
163 designTimeVisible
= value;
167 [DataCategory ("Data")]
168 [DataSysDescriptionAttribute ("The parameters collection")]
169 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility
.Content
)]
170 public OleDbParameterCollection Parameters
{
176 [BrowsableAttribute (false)]
177 [DataSysDescriptionAttribute ("The transaction used by the command")]
178 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility
.Hidden
)]
179 public OleDbTransaction Transaction
{
188 [DataCategory ("Behavior")]
189 [DefaultValue (UpdateRowSource
.Both
)]
190 [DataSysDescriptionAttribute ("When used by a DataAdapter.Update, how command results are applied to the current DataRow")]
191 public UpdateRowSource UpdatedRowSource
{
194 throw new NotImplementedException ();
198 throw new NotImplementedException ();
202 IDbConnection IDbCommand
.Connection
{
207 Connection
= (OleDbConnection
) value;
211 IDataParameterCollection IDbCommand
.Parameters
{
217 IDbTransaction IDbCommand
.Transaction
{
222 Transaction
= (OleDbTransaction
) value;
226 #endregion // Properties
231 public void Cancel ()
233 throw new NotImplementedException ();
236 public OleDbParameter
CreateParameter ()
238 return new OleDbParameter ();
241 IDbDataParameter IDbCommand
.CreateParameter ()
243 return CreateParameter ();
247 protected override void Dispose (bool disposing
)
249 throw new NotImplementedException ();
252 private void SetupGdaCommand ()
256 switch (commandType
) {
257 case CommandType
.TableDirect
:
258 type
= GdaCommandType
.Table
;
260 case CommandType
.StoredProcedure
:
261 type
= GdaCommandType
.Procedure
;
263 case CommandType
.Text
:
265 type
= GdaCommandType
.Sql
;
269 if (gdaCommand
!= IntPtr
.Zero
) {
270 libgda
.gda_command_set_text (gdaCommand
, commandText
);
271 libgda
.gda_command_set_command_type (gdaCommand
, type
);
273 gdaCommand
= libgda
.gda_command_new (commandText
, type
, 0);
276 //libgda.gda_command_set_transaction
279 public int ExecuteNonQuery ()
281 if (connection
== null)
282 throw new InvalidOperationException ("connection == null");
283 if (connection
.State
== ConnectionState
.Closed
)
284 throw new InvalidOperationException ("State == Closed");
285 // FIXME: a third check is mentioned in .NET docs
287 IntPtr gdaConnection
= connection
.GdaConnection
;
288 IntPtr gdaParameterList
= parameters
.GdaParameterList
;
291 return libgda
.gda_connection_execute_non_query (gdaConnection
,
296 public OleDbDataReader
ExecuteReader ()
298 return ExecuteReader (CommandBehavior
.Default
);
301 IDataReader IDbCommand
.ExecuteReader ()
303 return ExecuteReader ();
306 public OleDbDataReader
ExecuteReader (CommandBehavior behavior
)
308 ArrayList results
= new ArrayList ();
312 if (connection
.State
!= ConnectionState
.Open
)
313 throw new InvalidOperationException ("State != Open");
315 this.behavior
= behavior
;
317 IntPtr gdaConnection
= connection
.GdaConnection
;
318 IntPtr gdaParameterList
= parameters
.GdaParameterList
;
320 /* execute the command */
322 rs_list
= libgda
.gda_connection_execute_command (
326 if (rs_list
!= IntPtr
.Zero
) {
327 glist_node
= (GdaList
) Marshal
.PtrToStructure (rs_list
, typeof (GdaList
));
329 while (glist_node
!= null) {
330 results
.Add (glist_node
.data
);
331 if (glist_node
.next
== IntPtr
.Zero
)
334 glist_node
= (GdaList
) Marshal
.PtrToStructure (glist_node
.next
,
337 dataReader
= new OleDbDataReader (this, results
);
338 dataReader
.NextResult ();
344 IDataReader IDbCommand
.ExecuteReader (CommandBehavior behavior
)
346 return ExecuteReader (behavior
);
349 public object ExecuteScalar ()
352 OleDbDataReader reader
= ExecuteReader ();
353 if (reader
== null) {
356 if (!reader
.Read ()) {
360 object o
= reader
.GetValue (0);
366 object ICloneable
.Clone ()
368 throw new NotImplementedException ();
372 public void Prepare ()
374 throw new NotImplementedException ();
377 public void ResetCommandTimeout ()