年、月、日参数描述了无法表示的 DateTime 如何忽略
我使用 ODBC 从普遍使用的 PSQL 数据库读取数据,在某些情况下,日期列可以包含 00/00/0000 日期。我并不真正关心无效日期,所以有什么方法可以将所有这些不可表示的日期转换为 Null 或某个特定日期,而不是查询失败。
编辑:
以下显示了我正在使用的代码以及失败的地方:
Private _connODBC As OdbcConnection
Dim dt As New DataTable
_connODBC = New OdbcConnection(txtConnectionString.Text)
_connODBC.Open()
Dim dataadapter = New OdbcDataAdapter(QueryString, _connODBC)
dataadapter.Fill(dt) '<--- This line throws the unrepresentable date time error
_connODBC.Close()
I am using ODBC to read data from a pervasive PSQL database and in some scenarios, the date column can contain 00/00/0000 date. I don't really care about invalid dates so is there some way I can convert all of these un-representable dates into Null or some specific date instead of the query failing.
EDIT:
The following shows the code I am using and where it is failing:
Private _connODBC As OdbcConnection
Dim dt As New DataTable
_connODBC = New OdbcConnection(txtConnectionString.Text)
_connODBC.Open()
Dim dataadapter = New OdbcDataAdapter(QueryString, _connODBC)
dataadapter.Fill(dt) '<--- This line throws the unrepresentable date time error
_connODBC.Close()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
间接地,如果您将其导入文本字段而不是日期,那么在后面的步骤中,您可以根据需要识别和修改文本值。
Indirectly, if you import it into a text field instead of date, then, in a later step, you can identify and modify the text value however you want.
@SSS 让我想到这可以在查询中完成,尽管它有点冗长:
虽然我希望解决方案不依赖于我在读取数据库时必须记住执行此操作!
@SSS got me me thinking that this could be done in the query, although it is a bit long winded:
Although I would like a solution doesn't rely on me having to remember to do this whenever I read the database!
您可以使用 可空 DateTime - 如果该值无效,请将其设置为
null
(Nothing
),否则为日期。You can use a nullable DateTime - if the value is invalid set it to
null
(Nothing
), otherwise to the date.我也遇到过同样的问题。不久前,我忘记了确切的细节,但问题是 Pervasive.SQL 使用 &H0(即零)来表示缺失的日期,但 &H0 对于 ODBC 中的日期字段无效。
我的解决方案是将字段转换为 SQL 中的字符串,因此在 QueryString 中使用它: CONVERT(datefield,SQL_CHAR)
然后编写以下函数将其转换回日期(&H0 成为新日期):
I've had this exact same problem. It was a while ago and I forget the exact details, but the problem is that the Pervasive.SQL uses &H0 (ie. zero) to represent a missing date, but &H0 is invalid for a date field in ODBC.
My solution was to cast the field to a string in the SQL, so use this in your QueryString: CONVERT(datefield,SQL_CHAR)
I then wrote the following function to convert it back to a date (&H0 becomes a New Date):