简体   繁体   English

将 C# 中的空 DateTime 插入 FoxPro

[英]Insert empty DateTime from C# into FoxPro

I am trying to insert an empty DateTime into a FoxPro database using DbParameter in C#.我正在尝试使用 C# 中的DbParameter将空的DateTime插入 FoxPro 数据库。 Our application marries FoxPro data along with SQL Server and .NET models/data.我们的应用程序将 FoxPro 数据与 SQL 服务器和 .NET 模型/数据结合在一起。

My current issue is that most of our DateTime types in C# are not nullable, nor should they be.我当前的问题是 C# 中的大多数DateTime类型都不可为空,也不应该为空。 However, most of our legacy data in FoxPro are empty dates (shown as ' / /: : ').但是,我们在 FoxPro 中的大部分遗留数据都是空日期(显示为'//::')。 I am trying to do an insert into the FoxPro table where I do a check to see if the .NET DateTime meets certain criteria, then inserts an empty date.我正在尝试插入 FoxPro 表,在其中检查 .NET DateTime是否符合某些标准,然后插入一个空日期。 This last part has proven to be a nightmare.最后一部分已被证明是一场噩梦。

What I have tried so far:到目前为止我已经尝试过:

.Parameters.Add(string.Empty, new DateTime())

The above understandably inserts 01/01/0001 but is not what we want.上面可以理解地插入01/01/0001但不是我们想要的。

.Parameters.Add(string.Empty, "{}")
.Parameters.Add(string.Empty, "{:://}")
.Parameters.Add(string.Empty, "{//}")
.Parameters.Add(string.Empty, "'{}'")

The above all result in以上所有导致

System.Data.OleDb.OleDbException: 'Data type mismatch'. System.Data.OleDb.OleDbException:“数据类型不匹配”。

which makes sense because I'm trying to send a string to a field with DateTime type.这是有道理的,因为我正在尝试将string发送到DateTime类型的字段。

Does anyone know how to insert an empty DateTime into FoxPro?有谁知道如何在 FoxPro 中插入一个空的DateTime

If I'm understanding you correctly, you're not using a DbParameter , but rather an OleDbParameter , and you're adding them through the OleDbParameterCollection.Add method?如果我对您的理解正确,那么您使用的不是DbParameter而是OleDbParameter ,并且您是通过OleDbParameterCollection.Add方法添加它们的?

If so, consider that you are using the overload that is .Add(String, Object) , and you could instead use the overload that is .Add(String, OleDbType) :如果是这样,请考虑您使用的是.Add(String, Object)的重载,而您可以改用.Add(String, OleDbType)的重载:

.Parameters.Add(string.Empty, OleDbType.Date)

The default value of an OleDbParameter is null , so you don't need to do anything more for your empty dates. OleDbParameter的默认值为null ,因此您无需为空日期执行任何操作。

Also, depending on how the column is defined in your FoxPro database schema, it may be appropriate to pass OleDbType.Date , OleDbType.DBDate , OleDbType.DBTime , or OleDbType.DBTimeStamp .此外,根据列在 FoxPro 数据库架构中的定义方式,可能适合传递OleDbType.DateOleDbType.DBDateOleDbType.DBTimeOleDbType.DBTimeStamp The full list of OleDB types is documented here , but I'm not entirely certain how they align to FoxPro's data types. 此处记录了 OleDB 类型完整列表,但我不完全确定它们如何与 FoxPro 的数据类型保持一致。

I believe your second example is close, but you have the Date and Time portions reversed.我相信你的第二个例子很接近,但你的日期和时间部分颠倒了。

It should be .Parameters.Add(string.Empty, "{//::}")它应该是.Parameters.Add(string.Empty, "{//::}")

The // represent the Date portion, and the :: the Time portion. //代表日期部分,而::代表时间部分。

Not sure of your SQL-Insert statement for VFP, but you MAY need to adjust it and do TWO different inserts.不确定 VFP 的 SQL-Insert 语句,但您可能需要调整它并执行两个不同的插入。 One IF a date exists, another if it does not.一个如果日期存在,另一个如果不存在。

For the one that does NOT have a date, I would hard-enter the following (for example where the "?" are the parameter place-holders)对于没有日期的人,我会硬输入以下内容(例如“?”是参数占位符的地方)

insert into yourTable ( fld1, fld2,..., YourDateField ) values ( ?, ?, ..., ctot('') )

the function CTOT() means character to datetime and an empty string will comply with expected VFP Date/time field.函数 CTOT() 表示日期时间的字符,空字符串将符合预期的 VFP 日期/时间字段。

Work for me.为我工作。

Parameters.AddWithValue("CreateDate", new DateTime(1899,12,30,0,0,0));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM