错误:将 nvarchar 数据类型转换为smalldatetime 数据类型导致值超出范围

发布于 2024-08-22 13:12:49 字数 780 浏览 1 评论 0原文

嘿,我正在尝试执行以下插入查询

SqlDataSource userQuizDataSource = new SqlDataSource();
userQuizDataSource.ConnectionString = "Data Source=localhost\\SQLEXPRESS;Initial Catalog=quizApp;Integrated Security=True";
userQuizDataSource.InsertCommand = "INSERT INTO [UserQuiz] ([DateTimeComplete], [Score], [UserName]) VALUES (@DateTimeComplete, @Score, @UserName)";

userQuizDataSource.InsertParameters.Add("DateTimeComplete", DateTime.Now.ToString());
userQuizDataSource.InsertParameters.Add("Score", score.ToString());
userQuizDataSource.InsertParameters.Add("UserName", User.Identity.Name);

int rowsAffected = userQuizDataSource.Insert();

,但不断收到以下错误:

nvarchar 数据类型的转换 结果为小日期时间数据类型 超出范围的值。 该声明已终止。

有人可以帮我吗?

Hey all I'm trying to do the following insert query

SqlDataSource userQuizDataSource = new SqlDataSource();
userQuizDataSource.ConnectionString = "Data Source=localhost\\SQLEXPRESS;Initial Catalog=quizApp;Integrated Security=True";
userQuizDataSource.InsertCommand = "INSERT INTO [UserQuiz] ([DateTimeComplete], [Score], [UserName]) VALUES (@DateTimeComplete, @Score, @UserName)";

userQuizDataSource.InsertParameters.Add("DateTimeComplete", DateTime.Now.ToString());
userQuizDataSource.InsertParameters.Add("Score", score.ToString());
userQuizDataSource.InsertParameters.Add("UserName", User.Identity.Name);

int rowsAffected = userQuizDataSource.Insert();

Buti keep getting the following error:

The conversion of a nvarchar data type
to a smalldatetime data type resulted
in an out-of-range value.
The statement has been terminated.

Can anyone help me out?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

南烟 2024-08-29 13:12:49

您的语句 DateTime.Now.ToString() 返回什么?

您的 SQL Server 需要什么语言和区域设置?

你那里有不匹配吗???也许您的 .NET 返回 MM/dd/yyyy 格式,而 SQL Server 需要 dd/MM/yyyy(反之亦然)。

在 SQL Server 中尝试以下代码:

DECLARE @test TABLE (smalldate SMALLDATETIME)
INSERT INTO @test VALUES ('02/21/2010 22:00:32') --
SELECT * FROM @test

将其中的字符串替换为从 .NET 的 DateTime.Now.ToString() 获得的输出 - 这有效吗? SQL Server 是否会为您提供更好的错误消息?

接下来,尝试使用 ISO-8601 日期格式 (YYYYMMDD) - 这适用于 SQL Server 中的所有区域和语言设置 - 这有效吗?

DECLARE @test TABLE (smalldate SMALLDATETIME)
INSERT INTO @test VALUES ('20100221 22:00:32') --
SELECT * FROM @test

What does your statement DateTime.Now.ToString() return??

What language and regional settings is your SQL Server expecting??

Do you have a mismatch there??? Maybe your .NET returns a MM/dd/yyyy format, while SQL Server expects dd/MM/yyyy (or vice-versa).

Try this code in your SQL Server:

DECLARE @test TABLE (smalldate SMALLDATETIME)
INSERT INTO @test VALUES ('02/21/2010 22:00:32') --
SELECT * FROM @test

Replace my string there with what output you got from .NET's DateTime.Now.ToString() - does this work? Does SQL Server give you a better error message?

Next, try to use the ISO-8601 format for dates (YYYYMMDD) - this works for ALL regional and language settings in SQL Server - does this work??

DECLARE @test TABLE (smalldate SMALLDATETIME)
INSERT INTO @test VALUES ('20100221 22:00:32') --
SELECT * FROM @test
‖放下 2024-08-29 13:12:49

我在将 datetime.now 添加到我的 SQL Server 列“日期”中并设置为数据类型 SmallDateTime 时遇到了同样的问题。

解决这个问题非常简单(经过多次尝试!!)

string currentdatetime=
DateTime.Now.Year + "." + DateTime.Now.Month + "." + DateTime.Now.Day +
               " " + DateTime.Now.Hour+(":")+DateTime.Now.Minute+(":")+DateTime.Now.Second

这会将日期返回为服务器期望的格式

I had the same problem adding datetime.now into my SQL server column 'Date' set to datatype SmallDateTime.

To resolve it was quite simple (after many attempts!!)

string currentdatetime=
DateTime.Now.Year + "." + DateTime.Now.Month + "." + DateTime.Now.Day +
               " " + DateTime.Now.Hour+(":")+DateTime.Now.Minute+(":")+DateTime.Now.Second

This will return the date into the format that the Server will expect

把时间冻结 2024-08-29 13:12:49

尝试将其更改

userQuizDataSource.InsertParameters.Add("DateTimeComplete", DateTime.Now.ToString());

为:

userQuizDataSource.InsertParameters.Add("@startdate", SqlDbType.DateTime, DateTime.Now.ToString());

Try changing this:

userQuizDataSource.InsertParameters.Add("DateTimeComplete", DateTime.Now.ToString());

to this:

userQuizDataSource.InsertParameters.Add("@startdate", SqlDbType.DateTime, DateTime.Now.ToString());
违心° 2024-08-29 13:12:49

尝试不将日期转换为字符串:

userQuizDataSource.InsertParameters.Add("DateTimeComplete", DateTime.Now);

编辑: 然后试试这个:

userQuizDataSource.InsertParameters.Add("DateTimeComplete", TypeCode.DateTime, DateTime.Now.ToString());

还有另一种方法可以传递实际对象,但我不记得了..抱歉。

Try no converting your date to string:

userQuizDataSource.InsertParameters.Add("DateTimeComplete", DateTime.Now);

Edit: Try this then:

userQuizDataSource.InsertParameters.Add("DateTimeComplete", TypeCode.DateTime, DateTime.Now.ToString());

There is another way to just pass the actual object, but I can't remember.. sorry.

有深☉意 2024-08-29 13:12:49

在 Windows 8 中,如果您在更改以下位置的格式后仍遇到此问题

控制面板->地区

您仍然需要将这些设置传输给您的用户。在同一窗口中,转到“管理”选项卡,单击复制设置。

选择适当的复选框并单击确定

In Windows 8, if you are facing this problem even after changing Formats in below location

Control Panel -> Region

You still have to transfer these settings to your users. In the same window, go to tab "Administrative", click on copy settings.

Select the appropriate check boxes and click OK.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文