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; }
\r
116 set { isNullable = value; }
\r
119 public OdbcType OdbcType {
120 get { return odbcType; }
126 public string ParameterName {
128 set { name = value; }
131 public byte Precision {
132 get { return precision; }
133 set { precision = value; }
137 get { return scale; }
138 set { scale = value; }
143 set { size = value; }
146 public string SourceColumn {
147 get { return sourceColumn; }
148 set { sourceColumn = value; }
151 public DataRowVersion SourceVersion {
152 get { return sourceVersion; }
153 set { sourceVersion = value; }
156 public object Value {
161 this.ParamValue = value;
166 #endregion // Properties
170 internal void Bind(IntPtr hstmt, int ParamNum) {
\r
172 // Set up the buffer if we haven't done so yet
\r
176 // Convert System.Data.ParameterDirection into odbc enum
\r
177 OdbcInputOutputDirection paramdir = libodbc.ConvertParameterDirection(this.direction);
\r
178 // Bind parameter based on type
\r
179 if (odbcType == OdbcType.Int)
\r
180 ret = libodbc.SQLBindParameter(hstmt, (ushort)ParamNum, (short)paramdir,
\r
181 (short)odbcType, (short)odbcType, Convert.ToUInt32(size),
\r
182 0, ref intbuf, 0, 0);
\r
184 ret = libodbc.SQLBindParameter(hstmt, (ushort)ParamNum, (short)paramdir,
\r
185 (short)OdbcType.Char, (short)odbcType, Convert.ToUInt32(size),
\r
187 // Check for error condition
\r
188 if ((ret != OdbcReturn.Success) && (ret != OdbcReturn.SuccessWithInfo))
\r
189 throw new OdbcException(new OdbcError("SQLBindParam", OdbcHandleType.Stmt, hstmt));
\r
192 private void setBuffer() {
\r
193 // Load buffer with new value
\r
194 if (odbcType == OdbcType.Int)
\r
195 intbuf = (int)ParamValue;
\r
197 string paramValueString = ParamValue.ToString();
\r
198 // Treat everything else as a string
\r
199 // Init string buffer
\r
200 if (buffer == null || buffer.Length < ((size > 20) ? size : 20))
\r
201 buffer = new byte[(size > 20) ? size : 20];
\r
203 buffer.Initialize();
\r
204 // Convert value into string and store into buffer
\r
205 System.Text.Encoding.ASCII.GetBytes(paramValueString, 0, paramValueString.Length, buffer, 0);
\r
207 bufferIsSet = true;
\r
211 object ICloneable.Clone ()
213 throw new NotImplementedException ();
216 public override string ToString ()
218 return ParameterName;