将 varchar 转换为 smalldatetime 会导致值超出范围 [英] conversion of a varchar to a smalldatetime results in an out-of-range value

查看:17
本文介绍了将 varchar 转换为 smalldatetime 会导致值超出范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码:

strSql = "insert into table2 (transactiondate) values ('" & transactiondate & "')"

似乎给了我运行时错误:

seems to be giving me the runtime error:

The conversion of a varchar data type to a smalldatetime data type resulted in an out-of-range value

代码中,strSql是一个String对象,transactiondate是一个Date对象.但是在 SQL 数据库中,transactiondate 是一个 smalldatetime 对象.

In the code, strSql is a String object and transactiondate is a Date object. In the SQL database however, transactiondate is a smalldatetime object.

我尝试将 smalldatetime 更改为日期时间(在数据库中)并且我尝试过 transactiondate.toString() 但没有成功.

I've tried changing the smalldatetime to a datetime (in the database) and I've tried transactiondate.toString() but with no success.

如何解决这个问题?

注意:我知道内联 SQL 的危险.我在这里寻找快速修复解决方案,而不是关于 SQL 注入的讨论.

Note: I know about the dangers of inline SQL. I am looking for a quick fix solution here and not a discussion about SQL injection.

推荐答案

尝试在插入语句中添加单引号.

Try adding single quotes to your insert statement.

strSql = "insert into table2 (transactiondate) values ('" & transactiondate & "')"

这篇关于将 varchar 转换为 smalldatetime 会导致值超出范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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