> Given a custom type, how can you define what value gets passed when it is
> used as a database parameter? Consider:
>
> public class MyClass {
> public string Value;
> public MyClass(string value) { this.Value = value; }
> }
>
> using (SqlCommand cmd = new SqlCommand()) {
> // I want myClass to be a string when passed to the database!
> cmd.Parameters.AddWithValue("@param", new MyClass("test"));
> }
>
> Right now, I get a "No mapping exists from object type MyClass to a known
> managed provider native type" - which certainly implies that a mapping
> *could* exist. I've tried implicit conversion operators to System.String
> and SqlTypes.SqlString and IConvertible with no luck.
>
> Ideally, it should seamlessly convert to Odbc, OleDb, etc. parameter
> values as well (note that it's the value I want coerced - not converting
> to an actual SqlParameter).
>
> I would think an attribute, interface or conversion operator would be the
> trick - but spelunking with Reflector and Google isn't turning anything
> up. Anyone have any ideas?
Set parameter.SqlDbType to SqlDbType.VarChar afterwards.
FB
===================================
This list is hosted by DevelopMentorĀ® http://www.develop.com
View archives and manage your subscription(s) at http://discuss.develop.com