Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c#根据DB类型设置datarow DateTime字段_C#_Mysql_Sqlite_Datetime_Operators - Fatal编程技术网

c#根据DB类型设置datarow DateTime字段

c#根据DB类型设置datarow DateTime字段,c#,mysql,sqlite,datetime,operators,C#,Mysql,Sqlite,Datetime,Operators,我正在编写一个程序,可以交替使用SQLite数据库或MySql数据库。(取决于是否需要多人使用it、网络基础设施等) 我已经编写了一个通用DBType接口和两个基于DB类型实现它的类。有一个函数DataTable GetAllRows(tableName),正如您所期望的那样,它只检索表中的所有行并填充System.Data.DataTable。对于SQLite,我使用了 对于MySql,我使用了 现在的问题是,当我使用专门的DataReader(由连接器提供)读取行时,DataTable使用从

我正在编写一个程序,可以交替使用SQLite数据库或MySql数据库。(取决于是否需要多人使用it、网络基础设施等)

我已经编写了一个通用DBType接口和两个基于DB类型实现它的类。有一个函数
DataTable GetAllRows(tableName)
,正如您所期望的那样,它只检索表中的所有行并填充System.Data.DataTable。对于SQLite,我使用了 对于MySql,我使用了

现在的问题是,当我使用专门的DataReader(由连接器提供)读取行时,DataTable使用从该连接器返回的类型设置列类型。SQLite连接器使用
System.DateTime
作为DateTime字段,MySql使用
MySql.Data.Types.MySqlDateTime

我还实现了一个实体提供程序框架,因此我隐式地将每个检索到的数据行转换为一个实体(按需),这样DateTime字段将成为相应实体类中的DateTime字段

例如,假设数据库中有一个表:

userTable: field "userName" as string, field "date" as DateTime 如果我想返回并将实体转换为DataRow,我必须使用如下内容:

MyDataRow[userFieldName] = entity.name;
MyDataRow[dataFieldName] = entity.data;
如果我使用SQLite,就没有问题。如果我使用的是MySql,那么行
MyDataRow[dataFieldName]=entity.data
将引发类型异常,因为
MyDataRow[dataFieldName]
属于
MySqlDateTime
类型,但entity.data是
System.DateTime

当然,在分配字段之前,我可以在SQLite/MySql之间切换,但是我有75个表,其中有很多DateTime字段,因此,出于明显的原因,我不想每次都创建一个切换案例(或if)

我尝试创建一个MyDateTime类,该类使用以下运算符执行内部转换:

implicit operator DateTime
implicit operator MySql.Data.Types.MySqlDateTime
但是这些操作符永远不会被调用,因为表达式的类型
MyDataRow[dataFieldName]
object
,而不是
DateTime
MySqlDateTime
!我也不能向对象写入隐式运算符,因为MyDateTime类是对象的子类型,所以它是不允许的

请注意,
MySql.Data.Types.MySqlDateTime
有一些有用的方法:

public static explicit operator DateTime(MySqlDateTime val);
public DateTime GetDateTime();
public DateTime Value { get; }

public MySqlDateTime(DateTime dt);

如何安排代码执行类似于
MyDataRow[dataFieldName]=entity.data
的操作而不出现异常?我可以更改entity.data的类型,但我不想每次都进行切换,我更喜欢在读取时维护DataTable(列中不更改类型)。

这不是您问题的答案,但是,您想将所有这些工作作为学习练习吗?如果没有,也许您应该看看NHibernate或实体框架。(对象关系映射框架),因为我对它不太了解,而且从简要概述来看,它不适合我的需要。。
public static explicit operator DateTime(MySqlDateTime val);
public DateTime GetDateTime();
public DateTime Value { get; }
public MySqlDateTime(DateTime dt);