2 // System.Data.Odbc.OdbcParameter
5 // Brian Ritchie (brianlritchie@hotmail.com)
7 // Copyright (C) Brian Ritchie, 2002
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.
35 using System.Data.Common;
36 using System.ComponentModel;
38 namespace System.Data.Odbc
40 [TypeConverterAttribute (typeof (OdbcParameterConverter))]
41 public sealed class OdbcParameter : MarshalByRefObject, IDbDataParameter, IDataParameter, ICloneable
51 DataRowVersion sourceVersion;
53 ParameterDirection direction;
56 OdbcParameterCollection container = null;
58 // Buffers for parameter value based on type. Currently I've only optimized
59 // for int parameters and everything else is just converted to a string.
60 private bool bufferIsSet;
68 public OdbcParameter ()
76 sourceColumn = String.Empty;
79 public OdbcParameter (string name, object value)
83 this.ParamValue = value;
86 public OdbcParameter (string name, OdbcType dataType)
93 public OdbcParameter (string name, OdbcType dataType, int size)
94 : this (name, dataType)
99 public OdbcParameter (string name, OdbcType dataType, int size, string srcColumn)
100 : this (name, dataType, size)
102 this.sourceColumn = srcColumn;
105 [EditorBrowsable (EditorBrowsableState.Advanced)]
106 public OdbcParameter(string name, OdbcType dataType, int size, ParameterDirection direction, bool isNullable, byte precision, byte scale, string srcColumn, DataRowVersion srcVersion, object value)
107 : this (name, dataType, size, srcColumn)
109 this.direction = direction;
110 this.isNullable = isNullable;
111 this.precision = precision;
113 this.sourceVersion = srcVersion;
114 this.ParamValue = value;
121 // Used to ensure that only one collection can contain this
123 internal OdbcParameterCollection Container {
124 get { return container; }
125 set { container = value; }
128 [BrowsableAttribute (false)]
129 [OdbcDescriptionAttribute ("The parameter generic type")]
130 [RefreshPropertiesAttribute (RefreshProperties.All)]
131 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
132 [OdbcCategory ("Data")]
133 public DbType DbType {
134 get { return dbType; }
140 [OdbcCategory ("Data")]
141 [OdbcDescriptionAttribute ("Input, output, or bidirectional parameter")]
142 [DefaultValue (ParameterDirection.Input)]
143 public ParameterDirection Direction {
144 get { return direction; }
145 set { direction = value; }
148 [BrowsableAttribute (false)]
149 [OdbcDescriptionAttribute ("A design-time property used for strongly typed code generation")]
150 [DesignOnlyAttribute (true)]
151 [EditorBrowsableAttribute (EditorBrowsableState.Advanced)]
152 [DefaultValue (false)]
153 public bool IsNullable {
154 get { return isNullable; }
\r
155 set { isNullable = value; }
\r
158 [DefaultValue (OdbcType.NChar)]
159 [OdbcDescriptionAttribute ("The parameter native type")]
160 [RefreshPropertiesAttribute (RefreshProperties.All)]
161 [OdbcCategory ("Data")]
162 public OdbcType OdbcType {
163 get { return odbcType; }
169 [OdbcDescription ("DataParameter_ParameterName")]
171 public string ParameterName {
173 set { name = value; }
176 [OdbcDescription ("DbDataParameter_Precision")]
177 [OdbcCategory ("DataCategory_Data")]
179 public byte Precision {
180 get { return precision; }
181 set { precision = value; }
184 [OdbcDescription ("DbDataParameter_Scale")]
185 [OdbcCategory ("DataCategory_Data")]
188 get { return scale; }
189 set { scale = value; }
192 [OdbcDescription ("DbDataParameter_Size")]
193 [OdbcCategory ("DataCategory_Data")]
197 set { size = value; }
200 [OdbcDescription ("DataParameter_SourceColumn")]
201 [OdbcCategory ("DataCategory_Data")]
203 public string SourceColumn {
204 get { return sourceColumn; }
205 set { sourceColumn = value; }
208 [OdbcDescription ("DataParameter_SourceVersion")]
209 [OdbcCategory ("DataCategory_Data")]
211 public DataRowVersion SourceVersion {
212 get { return sourceVersion; }
213 set { sourceVersion = value; }
216 [TypeConverter (typeof(StringConverter))]
217 [OdbcDescription ("DataParameter_Value")]
218 [OdbcCategory ("DataCategory_Data")]
219 [DefaultValue (null)]
220 public object Value {
225 this.ParamValue = value;
230 #endregion // Properties
234 internal void Bind(IntPtr hstmt, int ParamNum) {
\r
236 // Set up the buffer if we haven't done so yet
\r
240 // Convert System.Data.ParameterDirection into odbc enum
\r
241 OdbcInputOutputDirection paramdir = libodbc.ConvertParameterDirection(this.direction);
\r
242 // Bind parameter based on type
\r
243 if (odbcType == OdbcType.Int)
\r
244 ret = libodbc.SQLBindParameter(hstmt, (ushort)ParamNum, (short)paramdir,
\r
245 (short)odbcType, (short)odbcType, Convert.ToUInt32(size),
\r
246 0, ref intbuf, 0, 0);
\r
248 ret = libodbc.SQLBindParameter(hstmt, (ushort)ParamNum, (short)paramdir,
\r
249 (short)OdbcType.Char, (short)odbcType, Convert.ToUInt32(size),
\r
251 // Check for error condition
\r
252 if ((ret != OdbcReturn.Success) && (ret != OdbcReturn.SuccessWithInfo))
\r
253 throw new OdbcException(new OdbcError("SQLBindParam", OdbcHandleType.Stmt, hstmt));
\r
256 private void setBuffer() {
\r
257 // Load buffer with new value
\r
258 if (odbcType == OdbcType.Int)
\r
259 intbuf = (int)ParamValue;
\r
261 string paramValueString = ParamValue.ToString();
\r
262 // Treat everything else as a string
\r
263 // Init string buffer
\r
264 if (ParamValue is String)
265 paramValueString = "\'"+paramValueString+"\'";
267 if (buffer == null || buffer.Length < ((size > 20) ? size : 20))
\r
268 buffer = new byte[(size > 20) ? size : 20];
\r
270 buffer.Initialize();
\r
271 // Convert value into string and store into buffer
\r
272 System.Text.Encoding.ASCII.GetBytes(paramValueString, 0, paramValueString.Length, buffer, 0);
\r
274 bufferIsSet = true;
\r
278 object ICloneable.Clone ()
280 throw new NotImplementedException ();
283 public override string ToString ()
285 return ParameterName;