错误:将 nvarchar 数据类型转换为smalldatetime 数据类型导致值超出范围
嘿,我正在尝试执行以下插入查询
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您的语句
DateTime.Now.ToString()
返回什么?您的 SQL Server 需要什么语言和区域设置?
你那里有不匹配吗???也许您的 .NET 返回
MM/dd/yyyy
格式,而 SQL Server 需要dd/MM/yyyy
(反之亦然)。在 SQL Server 中尝试以下代码:
将其中的字符串替换为从 .NET 的
DateTime.Now.ToString()
获得的输出 - 这有效吗? SQL Server 是否会为您提供更好的错误消息?接下来,尝试使用 ISO-8601 日期格式 (YYYYMMDD) - 这适用于 SQL Server 中的所有区域和语言设置 - 这有效吗?
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 expectsdd/MM/yyyy
(or vice-versa).Try this code in your SQL Server:
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??
我在将 datetime.now 添加到我的 SQL Server 列“日期”中并设置为数据类型 SmallDateTime 时遇到了同样的问题。
解决这个问题非常简单(经过多次尝试!!)
这会将日期返回为服务器期望的格式
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!!)
This will return the date into the format that the Server will expect
尝试将其更改
为:
Try changing this:
to this:
尝试不将日期转换为字符串:
编辑: 然后试试这个:
还有另一种方法可以传递实际对象,但我不记得了..抱歉。
Try no converting your date to string:
Edit: Try this then:
There is another way to just pass the actual object, but I can't remember.. sorry.
在 Windows 8 中,如果您在更改以下位置的
格式
后仍遇到此问题您仍然需要将这些设置传输给您的用户。在同一窗口中,转到“管理”选项卡,单击复制设置。
选择适当的复选框并单击
确定
。In Windows 8, if you are facing this problem even after changing
Formats
in below locationYou 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
.