如何创建一个通用类型的?DataType [英] How to create a generic type of ?DataType
问题描述
大家好。我需要一些关于泛型的帮助......至少我相信这个。
我的目标是创建一个包含ParamName,ParamValue和ParamType的泛型参数类。这个ParamType需要足够灵活,以接受来自其中一个tyeps(SQL,Oracle和DB2)的任何数据类型。
这甚至可能吗?请帮助:-)。
Hi all. I need some help with generics...at least that's what I believe.
My goal here is to create a generic parameters class that has ParamName, ParamValue, and ParamType. This ParamType needs to be flexible enough to accept any data type from the one of these tyeps (SQL, Oracle, and DB2).
Is this even possible? Please help :-).
Public class GenericParameters
{
public string ParamName;
public string ParamValue;
public ???? ParamDataType;
}
感谢所有回复的人!!!!
Thank you to all who respond!!!!
推荐答案
如果您尝试构建自己的数据访问类或层,最好先查看下面的文章看看Dapper.NET [ ^ ]
Dapper
if you trying to build your own data access class or layer you better check below article firstA Look at Dapper.NET[^]
Dapper
是单个文件 [ ^ ]您可以直接进入将扩展IDbConnection接口的项目。
is a single file[^] you can drop in to your project that will extend your IDbConnection interface.
您可以检查代码处理参数的方式。希望这有帮助。
you can check the code for how they handle parameters as well. hope this helps.
感谢DamithSL的潜在解决方案!我查看了Dapper代码,并能够获得有关如何向前发展的一些想法。
我最终创建了参数数据类型作为对象。客户端可以将其设置为任何数据类型(即SqlDbType,OracleDbType,Db2Type等),然后在WCF服务上,我检查发送的类型和处理方式。
例如:
客户:
.. 。
(服务客户端)
ParameterName = @@ Id;
ParameterType = SqlDbType.Int;
ParameterValue = 123456
WCF IService:
...
[DataContract]
公共对象ParameterType;
WCF服务:
...
if(dbConnection.Parameters [0] .ParameterType.GetType()== typeof(SqlDbType))
{
/ /如果包含参数,则获取每个参数数据tyupe。
foreach(dbConnection.Parameters中的var参数)
{
if(String.IsNullOrEmpty) (dbConnection.Parameters [0] .Para meterValue))返回null;
var returnValue = TypeDescriptor.GetConverter((SqlDbType)parameter.ParameterType).ConvertFromString(parameter.ParameterValue);
}
}
Thank you DamithSL for the potential solution! I reviewed the Dapper code and was able to get some ideas on how to move forward.
I ended up creating the parameter data type as an object. The client can set this to any data type (i.e. SqlDbType, OracleDbType, Db2Type, ect.) then on the WCF service, I check to see what type was sent and handle accordingly.
EXAMPLE:
Client:
...
(Service Client)
ParameterName = @"@Id";
ParameterType = SqlDbType.Int;
ParameterValue = 123456
WCF IService:
...
[DataContract]
public object ParameterType;
WCF Service:
...
if (dbConnection.Parameters[0].ParameterType.GetType() == typeof(SqlDbType))
{
// If parameters are included get each parameter data tyupe.
foreach (var parameter in dbConnection.Parameters)
{
if (String.IsNullOrEmpty(dbConnection.Parameters[0].ParameterValue)) return null;
var returnValue = TypeDescriptor.GetConverter((SqlDbType)parameter.ParameterType).ConvertFromString(parameter.ParameterValue);
}
}
这篇关于如何创建一个通用类型的?DataType的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!