2 // System.Data.Odbc.OdbcCommand
\r
5 // Brian Ritchie (brianlritchie@hotmail.com)
\r
7 // Copyright (C) Brian Ritchie, 2002
\r
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System.ComponentModel;
\r
35 using System.Data.Common;
\r
36 using System.Collections;
\r
37 using System.Runtime.InteropServices;
\r
39 namespace System.Data.Odbc
\r
42 /// Represents an SQL statement or stored procedure to execute against a data source.
\r
44 [DesignerAttribute ("Microsoft.VSDesigner.Data.VS.OdbcCommandDesigner, "+ Consts.AssemblyMicrosoft_VSDesigner, "System.ComponentModel.Design.IDesigner")]
\r
45 [ToolboxItemAttribute ("System.Drawing.Design.ToolboxItem, "+ Consts.AssemblySystem_Drawing)]
\r
46 public sealed class OdbcCommand : Component, ICloneable, IDbCommand
\r
52 CommandType commandType;
\r
53 OdbcConnection connection;
\r
54 OdbcParameterCollection parameters;
\r
55 OdbcTransaction transaction;
\r
56 bool designTimeVisible;
\r
57 bool prepared=false;
\r
58 OdbcDataReader dataReader;
\r
61 #endregion // Fields
\r
63 #region Constructors
\r
65 public OdbcCommand ()
\r
67 commandText = String.Empty;
\r
68 timeout = 30; // default timeout
\r
69 commandType = CommandType.Text;
\r
71 parameters = new OdbcParameterCollection ();
\r
73 designTimeVisible = false;
\r
77 public OdbcCommand (string cmdText) : this ()
\r
79 CommandText = cmdText;
\r
82 public OdbcCommand (string cmdText, OdbcConnection connection)
\r
85 Connection = connection;
\r
88 public OdbcCommand (string cmdText,
\r
89 OdbcConnection connection,
\r
90 OdbcTransaction transaction) : this (cmdText, connection)
\r
92 this.transaction = transaction;
\r
95 #endregion // Constructors
\r
99 internal IntPtr hStmt
\r
101 get { return hstmt; }
\r
105 [OdbcCategory ("Data")]
\r
106 [DefaultValue ("")]
\r
107 [OdbcDescriptionAttribute ("Command text to execute")]
\r
108 [EditorAttribute ("Microsoft.VSDesigner.Data.Odbc.Design.OdbcCommandTextEditor, "+ Consts.AssemblyMicrosoft_VSDesigner, "System.Drawing.Design.UITypeEditor, "+ Consts.AssemblySystem_Drawing )]
\r
109 [RefreshPropertiesAttribute (RefreshProperties.All)]
\r
110 public string CommandText
\r
113 return commandText;
\r
117 commandText = value;
\r
122 [OdbcDescriptionAttribute ("Time to wait for command to execute")]
\r
123 [DefaultValue (30)]
\r
124 public int CommandTimeout {
\r
133 [OdbcCategory ("Data")]
\r
134 [DefaultValue ("Text")]
\r
135 [OdbcDescriptionAttribute ("How to interpret the CommandText")]
\r
136 [RefreshPropertiesAttribute (RefreshProperties.All)]
\r
137 public CommandType CommandType {
\r
139 return commandType;
\r
142 commandType = value;
\r
146 [OdbcCategory ("Behavior")]
\r
147 [OdbcDescriptionAttribute ("Connection used by the command")]
\r
148 [DefaultValue (null)]
\r
149 [EditorAttribute ("Microsoft.VSDesigner.Data.Design.DbConnectionEditor, "+ Consts.AssemblyMicrosoft_VSDesigner, "System.Drawing.Design.UITypeEditor, "+ Consts.AssemblySystem_Drawing )]
\r
150 public OdbcConnection Connection {
\r
155 connection = value;
\r
159 [BrowsableAttribute (false)]
\r
160 [DesignOnlyAttribute (true)]
\r
161 [DefaultValue (true)]
\r
162 public bool DesignTimeVisible {
\r
164 return designTimeVisible;
\r
167 designTimeVisible = value;
\r
171 [OdbcCategory ("Data")]
\r
172 [OdbcDescriptionAttribute ("The parameters collection")]
\r
173 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Content)]
\r
174 public OdbcParameterCollection Parameters {
\r
180 [BrowsableAttribute (false)]
\r
181 [OdbcDescriptionAttribute ("The transaction used by the command")]
\r
182 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
\r
183 public OdbcTransaction Transaction {
\r
185 return transaction;
\r
188 transaction = value;
\r
192 [OdbcCategory ("Behavior")]
\r
193 [DefaultValue (UpdateRowSource.Both)]
\r
194 [OdbcDescriptionAttribute ("When used by a DataAdapter.Update, how command results are applied to the current DataRow")]
\r
195 public UpdateRowSource UpdatedRowSource {
\r
198 throw new NotImplementedException ();
\r
202 throw new NotImplementedException ();
\r
206 IDbConnection IDbCommand.Connection {
\r
211 Connection = (OdbcConnection) value;
\r
215 IDataParameterCollection IDbCommand.Parameters {
\r
221 IDbTransaction IDbCommand.Transaction {
\r
223 return (IDbTransaction) Transaction;
\r
226 if (value is OdbcTransaction)
228 Transaction = (OdbcTransaction)value;
232 throw new ArgumentException ();
237 #endregion // Properties
\r
241 public void Cancel ()
\r
243 if (hstmt!=IntPtr.Zero)
\r
245 OdbcReturn Ret=libodbc.SQLCancel(hstmt);
\r
246 if ((Ret!=OdbcReturn.Success) && (Ret!=OdbcReturn.SuccessWithInfo))
\r
247 throw new OdbcException(new OdbcError("SQLCancel",OdbcHandleType.Stmt,hstmt));
\r
250 throw new InvalidOperationException();
\r
253 public OdbcParameter CreateParameter ()
\r
255 return new OdbcParameter ();
\r
258 IDbDataParameter IDbCommand.CreateParameter ()
\r
260 return CreateParameter ();
\r
264 protected override void Dispose (bool disposing)
\r
268 private void ExecSQL(string sql)
\r
272 if ((parameters.Count>0) && !prepared)
\r
277 ret=libodbc.SQLExecute(hstmt);
\r
278 if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
\r
279 throw new OdbcException(new OdbcError("SQLExecute",OdbcHandleType.Stmt,hstmt));
\r
283 ret=libodbc.SQLAllocHandle(OdbcHandleType.Stmt, Connection.hDbc, ref hstmt);
\r
284 if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
\r
285 throw new OdbcException(new OdbcError("SQLAllocHandle",OdbcHandleType.Dbc,Connection.hDbc));
\r
287 ret=libodbc.SQLExecDirect(hstmt, sql, sql.Length);
\r
288 if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
\r
289 throw new OdbcException(new OdbcError("SQLExecDirect",OdbcHandleType.Stmt,hstmt));
\r
293 public int ExecuteNonQuery ()
\r
295 if (connection == null)
\r
296 throw new InvalidOperationException ();
\r
297 if (connection.State == ConnectionState.Closed)
\r
298 throw new InvalidOperationException ();
\r
299 // FIXME: a third check is mentioned in .NET docs
\r
301 ExecSQL(CommandText);
\r
304 // libodbc.SQLFreeHandle( (ushort) OdbcHandleType.Stmt, hstmt);
\r
308 public void Prepare()
\r
310 OdbcReturn ret=libodbc.SQLAllocHandle(OdbcHandleType.Stmt, Connection.hDbc, ref hstmt);
\r
311 if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
\r
312 throw new OdbcException(new OdbcError("SQLAllocHandle",OdbcHandleType.Dbc,Connection.hDbc));
\r
314 ret=libodbc.SQLPrepare(hstmt, CommandText, CommandText.Length);
\r
315 if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
\r
316 throw new OdbcException(new OdbcError("SQLPrepare",OdbcHandleType.Stmt,hstmt));
\r
319 foreach (OdbcParameter p in parameters)
\r
328 public OdbcDataReader ExecuteReader ()
\r
330 return ExecuteReader (CommandBehavior.Default);
\r
333 IDataReader IDbCommand.ExecuteReader ()
\r
335 return ExecuteReader ();
\r
338 public OdbcDataReader ExecuteReader (CommandBehavior behavior)
\r
341 dataReader=new OdbcDataReader(this,behavior);
\r
345 IDataReader IDbCommand.ExecuteReader (CommandBehavior behavior)
\r
347 return ExecuteReader (behavior);
\r
350 public object ExecuteScalar ()
\r
353 OdbcDataReader reader=ExecuteReader();
\r
356 if (reader.Read ())
\r
367 object ICloneable.Clone ()
\r
369 throw new NotImplementedException ();
\r
372 public void ResetCommandTimeout ()
\r