2 // System.Data.Odbc.OdbcCommand
5 // Brian Ritchie (brianlritchie@hotmail.com)
7 // Copyright (C) Brian Ritchie, 2002
10 using System.ComponentModel;
12 using System.Data.Common;
13 using System.Collections;
14 using System.Runtime.InteropServices;
16 namespace System.Data.Odbc
19 /// Represents an SQL statement or stored procedure to execute against a data source.
21 public sealed class OdbcCommand : Component, ICloneable, IDbCommand
27 CommandType commandType;
28 OdbcConnection connection;
29 OdbcParameterCollection parameters;
30 OdbcTransaction transaction;
31 bool designTimeVisible;
33 OdbcDataReader dataReader;
42 commandText = String.Empty;
43 timeout = 30; // default timeout
44 commandType = CommandType.Text;
46 parameters = new OdbcParameterCollection ();
48 designTimeVisible = false;
52 public OdbcCommand (string cmdText) : this ()
54 CommandText = cmdText;
57 public OdbcCommand (string cmdText, OdbcConnection connection)
60 Connection = connection;
63 public OdbcCommand (string cmdText,
64 OdbcConnection connection,
65 OdbcTransaction transaction) : this (cmdText, connection)
67 this.transaction = transaction;
70 #endregion // Constructors
79 public string CommandText
90 public int CommandTimeout {
99 public CommandType CommandType {
108 public OdbcConnection Connection {
117 public bool DesignTimeVisible {
119 return designTimeVisible;
122 designTimeVisible = value;
126 public OdbcParameterCollection Parameters {
135 public OdbcTransaction Transaction {
144 public UpdateRowSource UpdatedRowSource {
147 throw new NotImplementedException ();
151 throw new NotImplementedException ();
155 IDbConnection IDbCommand.Connection {
160 Connection = (OdbcConnection) value;
164 IDataParameterCollection IDbCommand.Parameters {
170 IDbTransaction IDbCommand.Transaction {
172 return (IDbTransaction) Transaction;
175 throw new NotImplementedException ();
179 #endregion // Properties
183 public void Cancel ()
185 if (hstmt!=IntPtr.Zero)
187 OdbcReturn Ret=libodbc.SQLCancel(hstmt);
188 if ((Ret!=OdbcReturn.Success) && (Ret!=OdbcReturn.SuccessWithInfo))
\r
189 throw new OdbcException(new OdbcError("SQLCancel",OdbcHandleType.Stmt,hstmt));
192 throw new InvalidOperationException();
195 public OdbcParameter CreateParameter ()
197 return new OdbcParameter ();
200 IDbDataParameter IDbCommand.CreateParameter ()
202 return CreateParameter ();
206 protected override void Dispose (bool disposing)
210 private void ExecSQL(string sql)
214 if ((parameters.Count>0) && !prepared)
219 ret=libodbc.SQLExecute(hstmt);
220 if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
\r
221 throw new OdbcException(new OdbcError("SQLExecute",OdbcHandleType.Stmt,hstmt));
225 ret=libodbc.SQLAllocHandle(OdbcHandleType.Stmt, Connection.hDbc, ref hstmt);
226 if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
\r
227 throw new OdbcException(new OdbcError("SQLAllocHandle",OdbcHandleType.Dbc,Connection.hDbc));
229 ret=libodbc.SQLExecDirect(hstmt, sql, sql.Length);
230 if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
\r
231 throw new OdbcException(new OdbcError("SQLExecDirect",OdbcHandleType.Stmt,hstmt));
235 public int ExecuteNonQuery ()
237 if (connection == null)
238 throw new InvalidOperationException ();
239 if (connection.State == ConnectionState.Closed)
240 throw new InvalidOperationException ();
241 // FIXME: a third check is mentioned in .NET docs
242 if (connection.DataReader != null)
243 throw new InvalidOperationException ();
245 ExecSQL(CommandText);
248 // libodbc.SQLFreeHandle( (ushort) OdbcHandleType.Stmt, hstmt);
252 public void Prepare()
254 OdbcReturn ret=libodbc.SQLAllocHandle(OdbcHandleType.Stmt, Connection.hDbc, ref hstmt);
255 if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
\r
256 throw new OdbcException(new OdbcError("SQLAllocHandle",OdbcHandleType.Dbc,Connection.hDbc));
258 ret=libodbc.SQLPrepare(hstmt, CommandText, CommandText.Length);
259 if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
\r
260 throw new OdbcException(new OdbcError("SQLPrepare",OdbcHandleType.Stmt,hstmt));
263 foreach (OdbcParameter p in parameters)
272 public OdbcDataReader ExecuteReader ()
274 return ExecuteReader (CommandBehavior.Default);
277 IDataReader IDbCommand.ExecuteReader ()
279 return ExecuteReader ();
282 public OdbcDataReader ExecuteReader (CommandBehavior behavior)
285 dataReader=new OdbcDataReader(this,behavior);
289 IDataReader IDbCommand.ExecuteReader (CommandBehavior behavior)
291 return ExecuteReader (behavior);
294 public object ExecuteScalar ()
296 if (connection.DataReader != null)
297 throw new InvalidOperationException ();
299 OdbcDataReader reader=ExecuteReader();
312 object ICloneable.Clone ()
314 throw new NotImplementedException ();
317 public void ResetCommandTimeout ()