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 [DesignerAttribute ("Microsoft.VSDesigner.Data.VS.OleDbCommandDesigner, "+ Consts.AssemblyMicrosoft_VSDesigner, "System.ComponentModel.Design.IDesigner")]
24 [ToolboxItemAttribute ("System.Drawing.Design.ToolboxItem, "+ Consts.AssemblySystem_Drawing)]
25 public sealed class OleDbCommand : Component, ICloneable, IDbCommand
31 CommandType commandType;
32 OleDbConnection connection;
33 OleDbParameterCollection parameters;
34 OleDbTransaction transaction;
35 bool designTimeVisible;
36 OleDbDataReader dataReader;
37 CommandBehavior behavior;
44 public OleDbCommand ()
46 commandText = String.Empty;
47 timeout = 30; // default timeout per .NET
48 commandType = CommandType.Text;
50 parameters = new OleDbParameterCollection ();
52 designTimeVisible = false;
54 behavior = CommandBehavior.Default;
55 gdaCommand = IntPtr.Zero;
58 public OleDbCommand (string cmdText) : this ()
60 CommandText = cmdText;
63 public OleDbCommand (string cmdText, OleDbConnection connection)
66 Connection = connection;
69 public OleDbCommand (string cmdText,
70 OleDbConnection connection,
71 OleDbTransaction transaction) : this (cmdText, connection)
73 this.transaction = transaction;
76 #endregion // Constructors
80 [DataCategory ("Data")]
82 [DataSysDescriptionAttribute ("Command text to execute")]
83 [EditorAttribute ("Microsoft.VSDesigner.Data.ADO.Design.OleDbCommandTextEditor, "+ Consts.AssemblyMicrosoft_VSDesigner, "System.Drawing.Design.UITypeEditor, "+ Consts.AssemblySystem_Drawing )]
84 [RefreshPropertiesAttribute (RefreshProperties.All)]
85 public string CommandText
95 [DataSysDescriptionAttribute ("Time to wait for command to execute")]
97 public int CommandTimeout {
106 [DataCategory ("Data")]
107 [DefaultValue ("Text")]
108 [DataSysDescriptionAttribute ("How to interpret the CommandText")]
109 [RefreshPropertiesAttribute (RefreshProperties.All)]
110 public CommandType CommandType {
119 [DataCategory ("Behavior")]
120 [DataSysDescriptionAttribute ("Connection used by the command")]
121 [DefaultValue (null)]
122 [EditorAttribute ("Microsoft.VSDesigner.Data.Design.DbConnectionEditor, "+ Consts.AssemblyMicrosoft_VSDesigner, "System.Drawing.Design.UITypeEditor, "+ Consts.AssemblySystem_Drawing )]
123 public OleDbConnection Connection {
132 [BrowsableAttribute (false)]
133 [DesignOnlyAttribute (true)]
134 [DefaultValue (true)]
135 public bool DesignTimeVisible {
137 return designTimeVisible;
140 designTimeVisible = value;
144 [DataCategory ("Data")]
145 [DataSysDescriptionAttribute ("The parameters collection")]
146 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Content)]
147 public OleDbParameterCollection Parameters {
153 [BrowsableAttribute (false)]
154 [DataSysDescriptionAttribute ("The transaction used by the command")]
155 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
156 public OleDbTransaction Transaction {
165 [DataCategory ("Behavior")]
166 [DefaultValue (UpdateRowSource.Both)]
167 [DataSysDescriptionAttribute ("When used by a DataAdapter.Update, how command results are applied to the current DataRow")]
168 public UpdateRowSource UpdatedRowSource {
171 throw new NotImplementedException ();
175 throw new NotImplementedException ();
179 IDbConnection IDbCommand.Connection {
184 Connection = (OleDbConnection) value;
188 IDataParameterCollection IDbCommand.Parameters {
194 IDbTransaction IDbCommand.Transaction {
199 Transaction = (OleDbTransaction) value;
203 #endregion // Properties
208 public void Cancel ()
210 throw new NotImplementedException ();
213 public OleDbParameter CreateParameter ()
215 return new OleDbParameter ();
218 IDbDataParameter IDbCommand.CreateParameter ()
220 return CreateParameter ();
224 protected override void Dispose (bool disposing)
226 throw new NotImplementedException ();
229 private void SetupGdaCommand ()
233 switch (commandType) {
234 case CommandType.TableDirect :
235 type = GdaCommandType.Table;
237 case CommandType.StoredProcedure :
238 type = GdaCommandType.Procedure;
240 case CommandType.Text :
242 type = GdaCommandType.Sql;
246 if (gdaCommand != IntPtr.Zero) {
247 libgda.gda_command_set_text (gdaCommand, commandText);
248 libgda.gda_command_set_command_type (gdaCommand, type);
250 gdaCommand = libgda.gda_command_new (commandText, type, 0);
253 //libgda.gda_command_set_transaction
256 public int ExecuteNonQuery ()
258 if (connection == null)
259 throw new InvalidOperationException ("connection == null");
260 if (connection.State == ConnectionState.Closed)
261 throw new InvalidOperationException ("State == Closed");
262 // FIXME: a third check is mentioned in .NET docs
264 IntPtr gdaConnection = connection.GdaConnection;
265 IntPtr gdaParameterList = parameters.GdaParameterList;
268 return libgda.gda_connection_execute_non_query (gdaConnection,
273 public OleDbDataReader ExecuteReader ()
275 return ExecuteReader (CommandBehavior.Default);
278 IDataReader IDbCommand.ExecuteReader ()
280 return ExecuteReader ();
283 public OleDbDataReader ExecuteReader (CommandBehavior behavior)
285 ArrayList results = new ArrayList ();
289 if (connection.State != ConnectionState.Open)
290 throw new InvalidOperationException ("State != Open");
292 this.behavior = behavior;
294 IntPtr gdaConnection = connection.GdaConnection;
295 IntPtr gdaParameterList = parameters.GdaParameterList;
297 /* execute the command */
299 rs_list = libgda.gda_connection_execute_command (
303 if (rs_list != IntPtr.Zero) {
304 glist_node = (GdaList) Marshal.PtrToStructure (rs_list, typeof (GdaList));
306 while (glist_node != null) {
307 results.Add (glist_node.data);
308 if (glist_node.next == IntPtr.Zero)
311 glist_node = (GdaList) Marshal.PtrToStructure (glist_node.next,
314 dataReader = new OleDbDataReader (this, results);
315 dataReader.NextResult ();
321 IDataReader IDbCommand.ExecuteReader (CommandBehavior behavior)
323 return ExecuteReader (behavior);
326 public object ExecuteScalar ()
329 OleDbDataReader reader = ExecuteReader ();
330 if (reader == null) {
333 if (!reader.Read ()) {
337 object o = reader.GetValue (0);
343 object ICloneable.Clone ()
345 throw new NotImplementedException ();
349 public void Prepare ()
351 throw new NotImplementedException ();
354 public void ResetCommandTimeout ()