Configure a custom data type mapping for use with a System.Data.SqlClient.SqlParameter

时间:2016-04-04 17:34:37

标签: c# .net ado.net type-conversion

I have a struct called CaseInsensitiveString which is just a wrapper that allows my program to work with strings without caring about their case. When it is persisted to a DB, it is no different from a regular string, though, so I want to be able to use it with a System.Data.SqlClient.SqlParameter like this:

var myString = new CaseInsensitiveString("foo");
var param = new SqlParameter("@MyValue", myString);

Of course I can't do that because SqlParameter doesn't know about my CaseInsensitiveString. I get a System.ArgumentException when I try to use it with a SqlDataAdapter:

No mapping exists from object type ConsoleApplication1.CaseInsensitiveString to a known managed provider native type.

Is there a way to make my own mapping so that I can convert CaseInsensitiveString to a System.String? I could imagine there being an interface that I'd have to implement on CaseInsensitiveString. MSDN does not provide any information on this.

I guess, to put it more succinctly, my question is really: how can I coerce a data type into one that is supported by System.Data.SqlClient.SqlParameter.

0 个答案:

没有答案