日期时间:嗯? [英] DateTime: Huh?

查看:79
本文介绍了日期时间:嗯?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




有谁能告诉我这两行之间的区别:


myDate = DateTime.Now;
< br $> b $ b和


myDate = DateTime.Parse(DateTime.Now.ToString(" dd / MM / yyyy"));

稍后在代码中看到,ExecuteNonQuery使用第一个

选项,但不使用第二个选项。 (我得到:{&数据类型不匹配标准

表达。})

私人OleDbCommand cmd;


....


cmd.Parameters.Add(" issueDate",myDate

cmd.ExecuteNonQuery();


命令对象是一个带有单个日期参数的Access更新查询。

连接字符串是Jet。命令类型是''StoredProcedure''

请不要对''Parameter.Add'的重载做任何评论''我是

使用,我知道如果我正确构造我的参数,我就不会得到

问题。我只是对一个版本的工作原理和另一个版本感兴趣

不是。


谢谢,


ChrisM。

解决方案

ChrisM,


虽然你没有要求对Add方法的重载有任何意见,但是如果你知道如果构造参数p它将会起作用roperly,那么为什么不用

使用它?


这就是说,第一行和第二行之间的区别,在

结果条款,应仅在值的时间部分。解析日期时间的

不会产生第二行中的时间值,

,而第一行将包含其中的时间。


希望这会有所帮助。

-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com

" ChrisM" <喜**** @ AskMeIfYouWantIt.com>在消息中写道

news:eR ************** @ TK2MSFTNGP12.phx.gbl ...


<有谁能告诉我这两行之间的区别:

myDate = DateTime.Now;



myDate = DateTime。 Parse(DateTime.Now.ToString(" dd / MM / yyyy"));

稍后在代码中看到,ExecuteNonQuery使用第一个
选项,但不是第二。 (我得到:{"数据类型不符合标准
表达。}}

私人OleDbCommand cmd;

......

cmd.Parameters.Add(" issueDate",myDate
cmd.ExecuteNonQuery();

命令对象是具有单个日期参数的Access更新查询。
连接字符串是Jet。命令类型是''StoredProcedure''

请不要对''Parameter.Add'的重载做任何评论''我是
使用,我知道如果我正确地构建了我的参数,我就不会得到问题。我只是对一个版本的工作原理感兴趣而另一个版本没有感兴趣。

谢谢,

ChrisM。



嗨Chris,

" ChrisM"< hi **** @ AskMeIfYouWantIt.com>在留言中写道

news:eR ************** @ TK2MSFTNGP12.phx.gbl ...



有谁能告诉我d这两行之间的差异:

myDate = DateTime.Now;



myDate = DateTime.Parse(DateTime.Now.ToString(") ; dd / MM / yyyy));




除了尼古拉斯的观察之外,我会指出上面的内容将是/ b $ b交换日期和月份(假设您当前的文化是en-us)。

DateTime.Parse会假设日期格式为MM / dd / yyyy,但您要给予

itdd / MM / yyyy。


问候,

Dan


ChrisM< hi **** @ AskMeIfYouWantIt.com>写道:

有谁能告诉我这两行之间的区别:

myDate = DateTime.Now;



myDate = DateTime.Parse(DateTime.Now.ToString(" dd / MM / yyyy"));

稍后在代码中看到,ExecuteNonQuery与第一个一起工作/>选项,但不是第二个。 (我得到:{"数据类型不符合标准
表达。}}

私人OleDbCommand cmd;

......

cmd.Parameters.Add(" issueDate",myDate
cmd.ExecuteNonQuery();

命令对象是具有单个日期参数的Access更新查询。
连接字符串是Jet。命令类型是''StoredProcedure''

请不要对''Parameter.Add'的重载做任何评论''我是
使用,我知道如果我正确地构造了我的参数,我就不会得到问题了。我只是对一个版本的工作原理感兴趣而另一个版本没有感兴趣。



我怀疑它是猜测参数是日期而不是

日期时间,因为你只有非零日期我只是完全创建

参数,我建议。


-

Jon Skeet - < sk***@pobox.com>
http://www.pobox.com/~skeet

如果回复小组,请不要给我发邮件




Could anyone please tell me the difference between these 2 lines:

myDate = DateTime.Now;

and

myDate = DateTime.Parse(DateTime.Now.ToString("dd/MM/yyyy"));
Seeing as later on in the code, the ExecuteNonQuery works with the first
option, but not with the second. (I get : {"Data type mismatch in criteria
expression." })

Private OleDbCommand cmd;

....

cmd.Parameters.Add("issueDate",myDate
cmd.ExecuteNonQuery();

The command object is a Access update query with a single date parameter.
Connection string is Jet. Command type is ''StoredProcedure''
Please no-one make any comments about the overload of ''Parameter.Add'' I''m
using, I know that if I constructed my parameters properly, I wouldn''t get
the problem. I''m just interested as to why one version works and the other
doesn''t.

Thanks,

ChrisM.

解决方案

ChrisM,

While you asked for no comments about the overload of the Add method, if
you know it will work if you construct the parameters properly, then why not
use that?

That being said, the difference between the first and second line, in
terms of results, should be in the time portion of the value only. The
parsing of the datetime does not yield a value for time in the second line,
while the first line will have the time of day included in it.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"ChrisM" <hi****@AskMeIfYouWantIt.com> wrote in message
news:eR**************@TK2MSFTNGP12.phx.gbl...



Could anyone please tell me the difference between these 2 lines:

myDate = DateTime.Now;

and

myDate = DateTime.Parse(DateTime.Now.ToString("dd/MM/yyyy"));
Seeing as later on in the code, the ExecuteNonQuery works with the first
option, but not with the second. (I get : {"Data type mismatch in criteria
expression." })

Private OleDbCommand cmd;

...

cmd.Parameters.Add("issueDate",myDate
cmd.ExecuteNonQuery();

The command object is a Access update query with a single date parameter.
Connection string is Jet. Command type is ''StoredProcedure''
Please no-one make any comments about the overload of ''Parameter.Add'' I''m
using, I know that if I constructed my parameters properly, I wouldn''t get
the problem. I''m just interested as to why one version works and the other
doesn''t.

Thanks,

ChrisM.



Hi Chris,

"ChrisM" <hi****@AskMeIfYouWantIt.com> wrote in message
news:eR**************@TK2MSFTNGP12.phx.gbl...



Could anyone please tell me the difference between these 2 lines:

myDate = DateTime.Now;

and

myDate = DateTime.Parse(DateTime.Now.ToString("dd/MM/yyyy"));



Besides Nicholas'' observations, I will point out that the above will
swap the date and month (assuming your current culture is en-us).
DateTime.Parse will assume a date format of "MM/dd/yyyy", but you are giving
it "dd/MM/yyyy".

Regards,
Dan


ChrisM <hi****@AskMeIfYouWantIt.com> wrote:

Could anyone please tell me the difference between these 2 lines:

myDate = DateTime.Now;

and

myDate = DateTime.Parse(DateTime.Now.ToString("dd/MM/yyyy"));
Seeing as later on in the code, the ExecuteNonQuery works with the first
option, but not with the second. (I get : {"Data type mismatch in criteria
expression." })

Private OleDbCommand cmd;

...

cmd.Parameters.Add("issueDate",myDate
cmd.ExecuteNonQuery();

The command object is a Access update query with a single date parameter.
Connection string is Jet. Command type is ''StoredProcedure''

Please no-one make any comments about the overload of ''Parameter.Add'' I''m
using, I know that if I constructed my parameters properly, I wouldn''t get
the problem. I''m just interested as to why one version works and the other
doesn''t.



I suspect it''s guessing that the parameter is a date rather than a
datetime, as you''ll only have non-zero date information. Just create
the parameter fully though, I''d recommend.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


这篇关于日期时间:嗯?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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