English 中文(简体)
c 成套数据
原标题:c# set datarow DateTime field basing on DB type

我撰写了一个方案,可以替代使用一个“贱民银行”或“MySql DB”。 (如果不止一个需要使用,基础设施净额......)

I ve written a generic DBType interface and two classes that implement it based on the DB type. There is a function DataTable GetAllRows(tableName) that, as you expect, simply retrieves all the rows of a table and fills a System.Data.DataTable. For SQLite I ve used the SQLIte connector and for MySql I ve used the MySql connector.

现在的问题是,当我阅读了专门数据检索器(由链接器提供)时,数据表将这一连接器返回的类型列起来。 日间田使用<代码>System.Datetime MySql使用MySql.Data.Types.MySqlDatetime

我也实施了实体提供人框架,因此,我默示将每个检索的数据Row转换为一个实体(按要求),因此,时间领域将成为相应实体类别的一个时间领域。

例如,假定在非行有表格:

userTable: 
field "userName" as string,
field "date" as DateTime

with one row:

"asd" 2000/01/01

I call GetAllRows for this table, now I ve a DataTable with 2 columns: the first column is a string field, the second column is System.DateTime or MySqlDateTime depending on the type of the DB I m using for this run.

该行将改为实体

class userTableEntity
{
    string name;
    DateTime data;

    /* methods... */

    implicit operator userTableEntity(DataRow row);
}

userTableEntity u = datarow; //uses the implicit operator

如果我想把该实体推回并转变为一个数据Row I ve,以使用类似的东西:

MyDataRow[userFieldName] = entity.name;
MyDataRow[dataFieldName] = entity.data;

如果Im利用QL,就不存在问题。 如果使用MySql的Im使用,那么MyDataRow[data FieldName] = 实体.将出现一种类型的例外,因为MyDataRow[data FieldName]为类型的,但实体为。 缩略语

当然,在分配外地之前,我可以转开Kallite/MySql,但我把像75张桌子这样的东西带去许多时间的现场,因此,我不希望每次开关(或者说是这样),因为明显的原因。

我试图与这些运营商进行内部转换的“日报”班:

implicit operator DateTime
implicit operator MySql.Data.Types.MySqlDateTime

但是,这些操作者永远不会被召来,因为以下表述的类型:<代码>MyDataRow[data FieldName]/code>:object,而不是Datetime/code>或MySqlDatetime! 我也不能写明一个暗的操作者表示反对,因为我的时间等级是物体的亚类,因此不允许。

注:MySql.Data.Types.MySqlDatetime有一些有用的方法:

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

一位教授

public MySqlDateTime(DateTime dt);

我如何安排该守则做像MyDataRow[data FieldName] = 实体等的事情。 我可以改变实体的类型。 数据,但我不想每次开关,我倾向于保留可读数据(各栏中无改动)。

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...