无法将参数值从字符串转换为日期时间 [英] Fail to convert parameter value from a string to a datetime

查看:141
本文介绍了无法将参数值从字符串转换为日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想把日期保存到访问数据库但是没有用..我想怎么办

这里是我的代码:



Dim SelectedDate As Object

SelectedDate = New DateTimePicker()

SelectedDate.Format = DateTimePickerFormat.Time

SelectedDate.ShowUpDown = True

SelectedDate.Location =新点(10,10)

SelectedDate.Width = 100

Controls.Add(SelectedDate)

SelectedDate = DateDateTimePicker.Text



我尝试过:



现在我尝试将我的日期时间值保存到访问数据库中

解决方案

嗯。

所选日期是DateTimePicker,所以你不能指定它。

那么为什么不把价值从一个DTP分配给另一个?

 SelectedDate.Value = DateDateTimePicker.Value 


Fir st issue :在最后一行,您尝试使用字符串覆盖DateTimePicker变量 SelectedDate (来自DateDateTimePicker.Text)。这无法奏效。您可能打算设置DateTimePicker的 .Value -Property,可能是这样的:

 SelectedDate。 Value = DateTime.Parse(DateDateTimePicker.Text); 



如果 DateDateTimePicker.Text 可以包含一个作为DateTime字符串无效的字符串。



编辑,看到OG的回复):除非 DateDateTimePicker 是另一个 DateTimePicker -object,然后a)使用DateDateTimePicker.Value或者DateDateTimePicker.Value.Date或b)重新考虑你是什么在那里 - 你需要两个DateTimePickers,其中一个值写入另一个吗?)



下一期(可能):你的错误消息听起来像是在尝试将字符串插入数据库的DateTime列。你需要为(Ole)DbParameter提供DateTime-value(这也是你从 SelectedDate.Value 获得的)。

不要得到的想法是将格式化为字符串的DateTime存储在数据库中。这是解决更大问题的途径。





链接:

DateTime.Parse Method(String)(System) [ ^ ]

DateTime.TryParse Method(System) [ ^ ]


i wan to save the date into an access database but didn't work.. how i suppose to do
here is my code :

Dim SelectedDate As Object
SelectedDate = New DateTimePicker()
SelectedDate.Format = DateTimePickerFormat.Time
SelectedDate.ShowUpDown = True
SelectedDate.Location = New Point(10, 10)
SelectedDate.Width = 100
Controls.Add(SelectedDate)
SelectedDate = DateDateTimePicker.Text

What I have tried:

Now I tried to make my datetime value save into an access database

解决方案

Um.
Selected Date is a DateTimePicker, so you can't assign a to it.
So why not just assign the Value from one DTP to the other?

SelectedDate.Value = DateDateTimePicker.Value


First issue: On the last line you try to overwrite your DateTimePicker-variable SelectedDate with a string (from DateDateTimePicker.Text). That can't work ever. You probably intended to set the .Value-Property of the the DateTimePicker, maybe like this:

SelectedDate.Value = DateTime.Parse(DateDateTimePicker.Text);


Use .TryParse(..) instead if DateDateTimePicker.Text could contain a string that's invalid as a DateTime-string.

(Edit, seeing OG's response): Unless DateDateTimePicker is another DateTimePicker-object, then a) use DateDateTimePicker.Value or maybe DateDateTimePicker.Value.Date or b) reconsider what you're doing there - do you need two DateTimePickers where the value of one is written to the other?)

Next issue (probably): Your error message sounds like you're trying to insert a string into the DateTime-column of your database. You need to provide the DateTime-value instead (which again you get from SelectedDate.Value) for the (Ole)DbParameter.
Don't get the idea of storing a DateTime formatted as a string in the database though. That's the path to far greater problems.


Links:
DateTime.Parse Method (String) (System)[^]
DateTime.TryParse Method (System)[^]


这篇关于无法将参数值从字符串转换为日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆