2 // System.Data.Odbc.OdbcParameter
5 // Brian Ritchie (brianlritchie@hotmail.com)
7 // Copyright (C) Brian Ritchie, 2002
12 using System.Data.Common;
14 namespace System.Data.Odbc
16 public sealed class OdbcParameter : MarshalByRefObject, IDbDataParameter, IDataParameter, ICloneable
26 DataRowVersion sourceVersion;
28 ParameterDirection direction;
31 OdbcParameterCollection container = null;
33 // Buffers for parameter value based on type. Currently I've only optimized
34 // for int parameters and everything else is just converted to a string.
35 private bool bufferIsSet;
43 public OdbcParameter ()
51 sourceColumn = String.Empty;
54 public OdbcParameter (string name, object value)
58 this.ParamValue = value;
61 public OdbcParameter (string name, OdbcType dataType)
68 public OdbcParameter (string name, OdbcType dataType, int size)
69 : this (name, dataType)
74 public OdbcParameter (string name, OdbcType dataType, int size, string srcColumn)
75 : this (name, dataType, size)
77 this.sourceColumn = srcColumn;
80 public OdbcParameter(string name, OdbcType dataType, int size, ParameterDirection direction, bool isNullable, byte precision, byte scale, string srcColumn, DataRowVersion srcVersion, object value)
81 : this (name, dataType, size, srcColumn)
83 this.direction = direction;
84 this.isNullable = isNullable;
85 this.precision = precision;
87 this.sourceVersion = srcVersion;
88 this.ParamValue = value;
95 // Used to ensure that only one collection can contain this
97 internal OdbcParameterCollection Container {
98 get { return container; }
99 set { container = value; }
102 public DbType DbType {
103 get { return dbType; }
109 public ParameterDirection Direction {
110 get { return direction; }
111 set { direction = value; }
114 public bool IsNullable {
115 get { return isNullable; }
118 public OdbcType OdbcType {
119 get { return odbcType; }
125 public string ParameterName {
127 set { name = value; }
130 public byte Precision {
131 get { return precision; }
132 set { precision = value; }
136 get { return scale; }
137 set { scale = value; }
142 set { size = value; }
145 public string SourceColumn {
146 get { return sourceColumn; }
147 set { sourceColumn = value; }
150 public DataRowVersion SourceVersion {
151 get { return sourceVersion; }
152 set { sourceVersion = value; }
155 public object Value {
160 this.ParamValue = value;
165 #endregion // Properties
167 #region public Properties
169 public void Bind(IntPtr hstmt,int ParamNum)
172 // Set up the buffer if we haven't done so yet
176 // Convert System.Data.ParameterDirection into odbc enum
177 OdbcInputOutputDirection paramdir=libodbc.ConvertParameterDirection(this.direction);
178 // Bind parameter based on type
179 if (odbcType==OdbcType.Int)
180 ret=libodbc.SQLBindParameter(hstmt, (ushort) ParamNum, (short) paramdir,
181 (short) odbcType, (short) odbcType, Convert.ToUInt32(size),
182 0, ref intbuf, 0, 0);
184 ret=libodbc.SQLBindParameter(hstmt, (ushort) ParamNum, (short) paramdir,
185 (short) OdbcType.Char, (short) odbcType, Convert.ToUInt32(size),
187 // Check for error condition
188 if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
189 throw new OdbcException(new OdbcError("SQLBindParam",OdbcHandleType.Stmt,hstmt));
192 private void setBuffer() {
193 // Load buffer with new value
194 if (odbcType==OdbcType.Int)
195 intbuf=(int) ParamValue;
198 string paramValueString = ParamValue.ToString();
199 // Treat everything else as a string
200 // Init string buffer
201 if (buffer==null || buffer.Length< ((size>20)?size:20) )
202 buffer=new byte[(size>20)?size:20];
205 // Convert value into string and store into buffer
206 System.Text.Encoding.ASCII.GetBytes(paramValueString, 0, paramValueString.Length , buffer, 0);
211 #endregion // public Properties
216 object ICloneable.Clone ()
218 throw new NotImplementedException ();
221 public override string ToString ()
223 return ParameterName;