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

百般思念 提交于 2019-12-23 14:03:19

问题


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.

来源:https://stackoverflow.com/questions/36409212/configure-a-custom-data-type-mapping-for-use-with-a-system-data-sqlclient-sqlpar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!