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;
34 CommandBehavior behavior;
35 internal IntPtr hstmt;
43 commandText = String.Empty;
44 timeout = 30; // default timeout
45 commandType = CommandType.Text;
47 parameters = new OdbcParameterCollection ();
49 designTimeVisible = false;
51 behavior = CommandBehavior.Default;
54 public OdbcCommand (string cmdText) : this ()
56 CommandText = cmdText;
59 public OdbcCommand (string cmdText, OdbcConnection connection)
62 Connection = connection;
65 public OdbcCommand (string cmdText,
66 OdbcConnection connection,
67 OdbcTransaction transaction) : this (cmdText, connection)
69 this.transaction = transaction;
72 #endregion // Constructors
81 public string CommandText
92 public int CommandTimeout {
101 public CommandType CommandType {
110 public OdbcConnection Connection {
119 public bool DesignTimeVisible {
121 return designTimeVisible;
124 designTimeVisible = value;
128 public OdbcParameterCollection Parameters {
137 public OdbcTransaction Transaction {
146 public UpdateRowSource UpdatedRowSource {
149 throw new NotImplementedException ();
153 throw new NotImplementedException ();
157 IDbConnection IDbCommand.Connection {
162 Connection = (OdbcConnection) value;
166 IDataParameterCollection IDbCommand.Parameters {
172 IDbTransaction IDbCommand.Transaction {
174 return (IDbTransaction) Transaction;
177 throw new NotImplementedException ();
181 #endregion // Properties
185 public void Cancel ()
187 if (hstmt!=IntPtr.Zero)
189 OdbcReturn ret=libodbc.SQLCancel(hstmt);
190 libodbchelper.DisplayError("SQLCancel",ret);
193 throw new InvalidOperationException();
196 public OdbcParameter CreateParameter ()
198 return new OdbcParameter ();
201 IDbDataParameter IDbCommand.CreateParameter ()
203 return CreateParameter ();
207 protected override void Dispose (bool disposing)
211 private void ExecSQL(string sql)
218 if (Parameters.Count>0)
219 Parameters.Bind(hstmt);
224 ret=libodbc.SQLExecute(hstmt);
225 libodbchelper.DisplayError("SQLExecute",ret);
229 ret=libodbc.SQLAllocHandle(OdbcHandleType.Stmt, Connection.hDbc, ref hstmt);
230 libodbchelper.DisplayError("SQLAllocHandle(hstmt)",ret);
231 ret=libodbc.SQLExecDirect(hstmt, sql, sql.Length);
232 libodbchelper.DisplayError("SQLExecDirect",ret);
236 public int ExecuteNonQuery ()
238 if (connection == null)
239 throw new InvalidOperationException ();
240 if (connection.State == ConnectionState.Closed)
241 throw new InvalidOperationException ();
242 // FIXME: a third check is mentioned in .NET docs
243 if (connection.DataReader != null)
244 throw new InvalidOperationException ();
246 ExecSQL(CommandText);
249 libodbc.SQLFreeHandle( (ushort) OdbcHandleType.Stmt, hstmt);
253 public void Prepare()
255 OdbcReturn ret=libodbc.SQLAllocHandle(OdbcHandleType.Stmt, Connection.hDbc, ref hstmt);
256 libodbchelper.DisplayError("SQLAlloc(Prepare)",ret);
257 ret=libodbc.SQLPrepare(hstmt, CommandText, CommandText.Length);
258 libodbchelper.DisplayError("SQLPrepare",ret);
262 public OdbcDataReader ExecuteReader ()
264 return ExecuteReader (CommandBehavior.Default);
267 IDataReader IDbCommand.ExecuteReader ()
269 return ExecuteReader ();
272 public OdbcDataReader ExecuteReader (CommandBehavior behavior)
275 dataReader=new OdbcDataReader(this);
279 IDataReader IDbCommand.ExecuteReader (CommandBehavior behavior)
281 return ExecuteReader (behavior);
284 public object ExecuteScalar ()
286 throw new NotImplementedException ();
287 // if (connection.DataReader != null)
288 // throw new InvalidOperationException ();
293 object ICloneable.Clone ()
295 throw new NotImplementedException ();
298 public void ResetCommandTimeout ()