English 中文(简体)
MySQL 颠覆 C#中的例外问题
原标题:MySQLConversionException Problem in C#

我目前正在开发一个C# MySQL Export Utility。 由于这一原因,我不了解表格中每个领域的领域或数据类型。

当我从数据库的表格中输出数据时,它显示的是MySQL的变幻影,称它无法更改MySQL的截止日期/时间。 日期: 在我制定法典时,它正在这样做:

if (!reader.isDBNull(fieldCount)){}

However, when the exception was thrown on this line I fixed it by adding Allow Zero DateTime=true to the MySQL Connection string but not it is displaying the error when I run the code

string value = reader.getString(field);

How can I get around this issue bearing in mind I am not going to know what data type is or what the value is going to be.

Thanks for any help you can provide.

最佳回答

You can get the raw value as object:

object value = reader[field];

Then decide what to do based on its type:

if (value is string)
{
    string sVal = value.ToString();
    //handle as string...
}
else if (value is DateTime)
{
    DateTime dtVal = (DateTime)value;
    //handle as DateTime...
}
else
{
    //some other type
}

也许不会说谎,而是应该工作。

问题回答

如果你与MySQLLinkor/Net读到DATETIME值,并设定“零日期”,那么你就应当使用读者。 GetValue()方法;在这种情况下,读者将返回一个MySqlDatetime物体,价值000-00-00:00:00。

rel=“nofollow”> 联接网/网络联系 参考

Note, than .NET DateTime.MinValue = 00:00:00.0000000, January 1, 0001.

http://www.devart.com/dotlink/mysql/“rel=“nofollow”>dotConnect for MySQL.

我在一个老的开放源项目中处理了类似这个问题。 见here,载于我的Util.DefaultConvert()方法。

骗局将使用<代码>Type.GetTypeCode()并转而使用结果。

然后对每种类型实行严格的换算。 在那里,你可以核查你需要做什么。 我在那里也有我的Sql供应商。





相关问题
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. ...

热门标签