使用ASP在SQL Server 2000中插入日期和时间 [英] Insert Date and Time in SQL Server 2000 using ASP

查看:98
本文介绍了使用ASP在SQL Server 2000中插入日期和时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

又一个问题!


我已从Access数据库转移到SQL服务器,现在遇到问题

插入日期和时间进入单独的领域。我正在使用ASP和下面的代码

来获取日期和时间,但我的脚本错误。


'' - 获取登录日期和时间

cmdLoginDate =日期()

cmdLoginTime =时间()


'' - 插入表格
cmdDC1.CommandText =" INSERT INTO tblUserTracking(LoginDate,LoginTime,

用户名)VALUES(''"& cmdLoginDate&"'',''"& ; cmdLoginTime&"'',''"&

cmdUsername&"'')"


'' - 错误消息是:


将char数据类型转换为日期时间数据类型导致

超出范围的日期时间值。


任何人都可以帮我解决这个问题....我已经在网上看了所有文章

我找到了解释为什么有问题但是我我找不到任何东西

帮助我解决问题.....


感谢advanc e ....


Robin。

解决方案

Robin Lawrie写道:

又一个问题!

我已经从Access数据库转移到SQL服务器,现在无法在单独的字段中插入日期和时间。我正在使用ASP
和下面的代码来获取日期和时间,但我的脚本是错误的。

'' - 获取登录日期和时间
cmdLoginDate = Date()
cmdLoginTime = Time()
'' - 插入表格
cmdDC1.CommandText =" INSERT INTO tblUserTracking(LoginDate, LoginTime,用户名)VALUES(''"& cmdLoginDate&"'',''"&
cmdLoginTime&"'',''"& cmdUsername&" '')"

'' - 错误信息是:

将char数据类型转换为日期时间数据类型导致
范围的日期时间值。

任何人都可以帮我解决这个问题....我已经在网上查了一下,但我找到的所有
文章都解释了为什么会出现问题但我没有找到帮助我解决问题的任何事情......

提前致谢......

罗宾。



和往常一样,sql语句如果不知道它们是什么就无法调试。

Response.Write连接的结果。最好将连接结果存储到变量中,而不是直接将它设置为ado对象的属性。很容易回复 - 写一个

变量。


这就是说,我会建议不要进行连接。使用存储的

过程并将参数值传递给它(不需要Command对象):


cn.proc_name cmdLoginDate,cmdLoginTime,cmdUsername


如果使用存储过程违反您的宗教信仰,您可以使用参数。看起来cmdDC1是一个Command对象,所以请执行




cmdDC1.CommandText =" INSERT INTO tblUserTracking" &安培; _

"(LoginDate,LoginTime,Username)VALUES(?,?,?)"

设置cmdDC1.activeconnection = cn

cmdDC1.Execute,_

数组(cmdLoginDate,cmdLoginTime,cmdUsername),129

如果这导致错误,请回复 - 写入参数值。告诉我们

它们是什么,并为我们提供DDL表格

www.aspfaq.com/5006


Bob Barrows

-

Microsoft MVP - ASP / ASP.NET

请回复新闻组。这个电子邮件帐户是我的垃圾邮件陷阱所以我

不经常检查它。如果您必须离线回复,请删除

无垃圾邮件


我可以问您为什么要插入日期和时间分成两列?

SQL Server没有能力分开日期和时间。您将会注意到,如果您按照书面形式执行此操作,LoginDate将具有日期和午夜的时间值,并且LoginTime将具有时间和日期值

1900-01-01。您应该将这两列组合成一列。在某些情况下,
可以看到只有日期的单独列可能会产生感觉,但是时间本身将毫无用处。这个设计怎么样,在

的情况下:


CREATE TABLE dbo.UserTracking



用户名VARCHAR(32)NOT NULL,

LoginDateTime SMALLDATETIME NOT NULL DEFAULT GETDATE(),

LoginDate AS CONVERT



SMALLDATETIME,

DATEADD(DAY,DATEDIFF(DAY,0,LoginDate))






注意我重命名了你的表。 tbl前缀几乎没有用,除了

增加所需的输入量...


无论如何,现在你可以让数据库插入当前日期/ time $

表格,你的SQL语句变得更加整洁。


cmdDC1.CommandText =''INSERT dbo.UserTracking(UserName)SELECT'' " &

cmdUserName& "''"


别忘了删除cmdUserName中的任何单引号,请

考虑使用存储过程。

http://www.aspfaq.com/2201

2005年2月27日下午3:20,文章 kr ******************** @ pipex.net ,Robin

Lawrie < SE ***** @ gotadsl.co.uk>写道:

嗨再次,另一个问题!

我已经从Access数据库转移到SQL服务器,现在我很难插入
日期和时间进入单独的领域。我正在使用ASP和下面的代码来获取日期和时间,但我的脚本是错误的。

'' - 获取登录日期和时间
cmdLoginDate =日期()
cmdLoginTime =时间()

'' - 插入表格
cmdDC1.CommandText =" INSERT INTO tblUserTracking(LoginDate,LoginTime,
用户名) )VALUES(''"& cmdLoginDate&"'',''"& cmdLoginTime&"'',''"&
cmdUsername&"'')" ;

'' - 错误信息是:

将char数据类型转换为日期时间数据类型导致
超出范围的日期时间值得。

任何人都可以帮我解决这个问题....我已经在网上看了所有的文章
我找到了解释为什么有问题但是我''我找不到任何帮助我解决问题的事情......

提前致谢......

罗宾。




再次感谢您的帮助Bob,非常感谢!


我是SQL Server的新手,正在尝试开发Web应用程序。我不知道我的托管公司是否允许在他们的

共享SQL Server托管计划上创建存储过程,但我会问。如果是这样的话,我会考虑使用

存储过程,如果托管公司提供它(如果他们提供它

你可以确定新的帖子到这个新闻组!)


问候,


罗宾。


" Bob Barrows [MVP] " <再****** @ NOyahoo.SPAMcom>在消息中写道

新闻:uV ************* @ TK2MSFTNGP15.phx.gbl ...

Robin Lawrie写道:

嗨再次,另一个问题!

我已经从Access数据库转移到SQL服务器,现在无法在单独的字段中插入日期和时间。我正在使用ASP
和下面的代码来获取日期和时间,但我的脚本是错误的。

'' - 获取登录日期和时间
cmdLoginDate = Date()
cmdLoginTime = Time()
'' - 插入表格
cmdDC1.CommandText =" INSERT INTO tblUserTracking(LoginDate, LoginTime,用户名)VALUES(''"& cmdLoginDate&"'',''"&
cmdLoginTime&"'',''"& cmdUsername&" '')"

'' - 错误信息是:

将char数据类型转换为日期时间数据类型导致
范围的日期时间值。

任何人都可以帮我解决这个问题....我已经在网上查了一下,但我找到的所有
文章都解释了为什么会出现问题但我没有找到帮助我解决问题的任何事情......

提前致谢......

罗宾。


一如既往,sql语句不能调试后不知道它们是什么。 Response.Write连接的结果。最好将串联结果存储到变量中,而不是直接将其设置为ado对象的属性。很容易回复 - 写一个变量。

那就是说,我建议不要进行连接。使用存储的
过程并将参数值传递给它(不需要Command对象):

cn.proc_name cmdLoginDate,cmdLoginTime,cmdUsername

如果使用存储过程是反对你的宗教信仰,你可以继续使用参数。看起来cmdDC1是一个Command对象,所以这样做:

cmdDC1.CommandText =" INSERT INTO tblUserTracking" &安培; _
"(LoginDate,LoginTime,Username)VALUES(?,?,?)"
设置cmdDC1.activeconnection = cn
cmdDC1.Execute,_
数组(cmdLoginDate ,cmdLoginTime,cmdUsername),129
如果这导致错误,请响应 - 写入参数值。告诉我们
它们是什么,并为我们提供DDL表格
www.aspfaq.com/5006

Bob Barrows
-
Microsoft MVP - ASP / ASP.NET
请回复新闻组。这个电子邮件帐户是我的垃圾邮件陷阱所以我不经常检查它。如果您必须离线回复,请删除
NO SPAM



Hi again, another problem!

I''ve moved from an Access database to SQL server and am now having trouble
inserting dates and times into seperate fields. I''m using ASP and the code
below to get the date and time, but my script is erroring.

''-- Get login date and time
cmdLoginDate = Date()
cmdLoginTime = Time()

''-- Insert into table
cmdDC1.CommandText = "INSERT INTO tblUserTracking (LoginDate, LoginTime,
Username) VALUES (''" & cmdLoginDate & "'',''" & cmdLoginTime & "'',''" &
cmdUsername & "'') "

''-- Error message is:

The conversion of a char data type to a datetime data type resulted in an
out-of-range datetime value.

Can anyone help me fix this....I''ve looked on the web but all the articles
I''ve found explain why there are problems but I''ve not found anything that
helps me fix the problem.....

Thanks in advance....

Robin.

解决方案

Robin Lawrie wrote:

Hi again, another problem!

I''ve moved from an Access database to SQL server and am now having
trouble inserting dates and times into seperate fields. I''m using ASP
and the code below to get the date and time, but my script is
erroring.

''-- Get login date and time
cmdLoginDate = Date()
cmdLoginTime = Time()

''-- Insert into table
cmdDC1.CommandText = "INSERT INTO tblUserTracking (LoginDate,
LoginTime, Username) VALUES (''" & cmdLoginDate & "'',''" &
cmdLoginTime & "'',''" & cmdUsername & "'') "

''-- Error message is:

The conversion of a char data type to a datetime data type resulted
in an out-of-range datetime value.

Can anyone help me fix this....I''ve looked on the web but all the
articles I''ve found explain why there are problems but I''ve not found
anything that helps me fix the problem.....

Thanks in advance....

Robin.


As always, sql statements cannot ve debugged without knowing what they are.
Response.Write the result of your concatenation. It is always best to store
the result of your concatenation into a variable rather than setting it
directly to the property of your ado object. It is easy to response-write a
variable.

That said, i would advise not doing the concatenation at all. Use a stored
procedure and pass parameter values to it (no Command object needed):

cn.proc_name cmdLoginDate, cmdLoginTime, cmdUsername

If using stored procedures is against your religion or something, you can
still utilize parameters. It looks like cmdDC1 is a Command object, so do
this:

cmdDC1.CommandText = "INSERT INTO tblUserTracking " & _
"(LoginDate, LoginTime,Username) VALUES (?,?,?) "
Set cmdDC1.activeconnection=cn
cmdDC1.Execute , _
array(cmdLoginDate, cmdLoginTime, cmdUsername), 129

If this results in an error, response-write your parameter values. Show us
what they are and provide us with the DDL for yout table
(www.aspfaq.com/5006)

Bob Barrows
--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don''t check it very often. If you must reply off-line, then remove the
"NO SPAM"


Can I ask why you are inserting the date and time into two separate columns?
SQL Server doesn''t have the ability to separate date and time. You will
notice that if you do this as written, LoginDate will have the date AND a
time value of midnight, and LoginTime will have the time AND a date value of
1900-01-01. You should combine these two columns into a single column. I
can see in some cases that a separate column for just the date might make
sense, but time by itself will be pretty useless. How about this design, in
that case:

CREATE TABLE dbo.UserTracking
(
Username VARCHAR(32) NOT NULL,
LoginDateTime SMALLDATETIME NOT NULL DEFAULT GETDATE(),
LoginDate AS CONVERT
(
SMALLDATETIME,
DATEADD(DAY, DATEDIFF(DAY, 0, LoginDate))
)
)

Notice I renamed your table. The tbl prefix serves little purpose except to
increase the amount of typing required...

Anyway, now you can leave the database to insert the current date/time into
the table, and your SQL statement becomes much tidier.

cmdDC1.CommandText = ''INSERT dbo.UserTracking(UserName) SELECT ''" &
cmdUserName & "''"

Don''t forget to remove any single quotes from cmdUserName, and please
consider using stored procedures.

http://www.aspfaq.com/2201

On 2/27/05 3:20 PM, in article kr********************@pipex.net, "Robin
Lawrie" <se*****@gotadsl.co.uk> wrote:

Hi again, another problem!

I''ve moved from an Access database to SQL server and am now having trouble
inserting dates and times into seperate fields. I''m using ASP and the code
below to get the date and time, but my script is erroring.

''-- Get login date and time
cmdLoginDate = Date()
cmdLoginTime = Time()

''-- Insert into table
cmdDC1.CommandText = "INSERT INTO tblUserTracking (LoginDate, LoginTime,
Username) VALUES (''" & cmdLoginDate & "'',''" & cmdLoginTime & "'',''" &
cmdUsername & "'') "

''-- Error message is:

The conversion of a char data type to a datetime data type resulted in an
out-of-range datetime value.

Can anyone help me fix this....I''ve looked on the web but all the articles
I''ve found explain why there are problems but I''ve not found anything that
helps me fix the problem.....

Thanks in advance....

Robin.




Thanks again for your help Bob, it is very much appreciated!

I''m new to SQL Server and am trying to develop a web application. I don''t
know if my hosting company allows stored procedures to be created on their
shared SQL Server hosting plan but I will ask. If so, I''ll look into using
stored procedures if the hosting company offers it (and if they do offer it
you can be sure of new posts to this newsgroup!)

Regards,

Robin.

"Bob Barrows [MVP]" <re******@NOyahoo.SPAMcom> wrote in message
news:uV*************@TK2MSFTNGP15.phx.gbl...

Robin Lawrie wrote:

Hi again, another problem!

I''ve moved from an Access database to SQL server and am now having
trouble inserting dates and times into seperate fields. I''m using ASP
and the code below to get the date and time, but my script is
erroring.

''-- Get login date and time
cmdLoginDate = Date()
cmdLoginTime = Time()

''-- Insert into table
cmdDC1.CommandText = "INSERT INTO tblUserTracking (LoginDate,
LoginTime, Username) VALUES (''" & cmdLoginDate & "'',''" &
cmdLoginTime & "'',''" & cmdUsername & "'') "

''-- Error message is:

The conversion of a char data type to a datetime data type resulted
in an out-of-range datetime value.

Can anyone help me fix this....I''ve looked on the web but all the
articles I''ve found explain why there are problems but I''ve not found
anything that helps me fix the problem.....

Thanks in advance....

Robin.


As always, sql statements cannot ve debugged without knowing what they
are. Response.Write the result of your concatenation. It is always best to
store the result of your concatenation into a variable rather than setting
it directly to the property of your ado object. It is easy to
response-write a variable.

That said, i would advise not doing the concatenation at all. Use a stored
procedure and pass parameter values to it (no Command object needed):

cn.proc_name cmdLoginDate, cmdLoginTime, cmdUsername

If using stored procedures is against your religion or something, you can
still utilize parameters. It looks like cmdDC1 is a Command object, so do
this:

cmdDC1.CommandText = "INSERT INTO tblUserTracking " & _
"(LoginDate, LoginTime,Username) VALUES (?,?,?) "
Set cmdDC1.activeconnection=cn
cmdDC1.Execute , _
array(cmdLoginDate, cmdLoginTime, cmdUsername), 129

If this results in an error, response-write your parameter values. Show us
what they are and provide us with the DDL for yout table
(www.aspfaq.com/5006)

Bob Barrows
--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don''t check it very often. If you must reply off-line, then remove the
"NO SPAM"



这篇关于使用ASP在SQL Server 2000中插入日期和时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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