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 throw new NotImplementedException ();
\r
230 #endregion // Properties
\r
234 public void Cancel ()
\r
236 if (hstmt!=IntPtr.Zero)
\r
238 OdbcReturn Ret=libodbc.SQLCancel(hstmt);
\r
239 if ((Ret!=OdbcReturn.Success) && (Ret!=OdbcReturn.SuccessWithInfo))
\r
240 throw new OdbcException(new OdbcError("SQLCancel",OdbcHandleType.Stmt,hstmt));
\r
243 throw new InvalidOperationException();
\r
246 public OdbcParameter CreateParameter ()
\r
248 return new OdbcParameter ();
\r
251 IDbDataParameter IDbCommand.CreateParameter ()
\r
253 return CreateParameter ();
\r
257 protected override void Dispose (bool disposing)
\r
261 private void ExecSQL(string sql)
\r
265 if ((parameters.Count>0) && !prepared)
\r
270 ret=libodbc.SQLExecute(hstmt);
\r
271 if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
\r
272 throw new OdbcException(new OdbcError("SQLExecute",OdbcHandleType.Stmt,hstmt));
\r
276 ret=libodbc.SQLAllocHandle(OdbcHandleType.Stmt, Connection.hDbc, ref hstmt);
\r
277 if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
\r
278 throw new OdbcException(new OdbcError("SQLAllocHandle",OdbcHandleType.Dbc,Connection.hDbc));
\r
280 ret=libodbc.SQLExecDirect(hstmt, sql, sql.Length);
\r
281 if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
\r
282 throw new OdbcException(new OdbcError("SQLExecDirect",OdbcHandleType.Stmt,hstmt));
\r
286 public int ExecuteNonQuery ()
\r
288 if (connection == null)
\r
289 throw new InvalidOperationException ();
\r
290 if (connection.State == ConnectionState.Closed)
\r
291 throw new InvalidOperationException ();
\r
292 // FIXME: a third check is mentioned in .NET docs
\r
294 ExecSQL(CommandText);
\r
297 // libodbc.SQLFreeHandle( (ushort) OdbcHandleType.Stmt, hstmt);
\r
301 public void Prepare()
\r
303 OdbcReturn ret=libodbc.SQLAllocHandle(OdbcHandleType.Stmt, Connection.hDbc, ref hstmt);
\r
304 if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
\r
305 throw new OdbcException(new OdbcError("SQLAllocHandle",OdbcHandleType.Dbc,Connection.hDbc));
\r
307 ret=libodbc.SQLPrepare(hstmt, CommandText, CommandText.Length);
\r
308 if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
\r
309 throw new OdbcException(new OdbcError("SQLPrepare",OdbcHandleType.Stmt,hstmt));
\r
312 foreach (OdbcParameter p in parameters)
\r
321 public OdbcDataReader ExecuteReader ()
\r
323 return ExecuteReader (CommandBehavior.Default);
\r
326 IDataReader IDbCommand.ExecuteReader ()
\r
328 return ExecuteReader ();
\r
331 public OdbcDataReader ExecuteReader (CommandBehavior behavior)
\r
334 dataReader=new OdbcDataReader(this,behavior);
\r
338 IDataReader IDbCommand.ExecuteReader (CommandBehavior behavior)
\r
340 return ExecuteReader (behavior);
\r
343 public object ExecuteScalar ()
\r
346 OdbcDataReader reader=ExecuteReader();
\r
359 object ICloneable.Clone ()
\r
361 throw new NotImplementedException ();
\r
364 public void ResetCommandTimeout ()
\r