日期时间字段更新导致异常

发布于 2024-07-27 02:56:07 字数 3070 浏览 5 评论 0原文

过去几天我一直在使用 subsonic 3.0,但遇到了问题。 使用 ActiveRecord 并在现有记录上调用 save 导致:

[SqlException (0x80131904): The conversion of a char data type to a datetime
                            data type resulted in an out-of-range datetime value.
                            The statement has been terminated.]

我使用的代码只是为了演示问题:

public void ButtonCreate_OnClick(object sender, EventArgs e) {
    stPost post = new stPost();
    post.postCreatedDate = DateTime.Now;
    post.postDescription = "cool post at " + DateTime.Now.ToString();
    post.postGuid = Guid.NewGuid();
    post.postTitle = "Post title";
    post.postUpdatedDate = DateTime.Now;

    post.Save();
}

public void ButtonUpdate_OnClick(object sender, EventArgs e) {
    stPost post = stPost.All().ToList<stPost>()[0];
    if (post != null && !post.IsNew()) {
        post.postDescription = "cool post UPDATED at " + DateTime.Now.ToString();
        post.postTitle = "Post title Updated";
        post.postUpdatedDate = DateTime.Now;

        post.Save();
    }
}

研究 sql profiler 和 resharper 我发现插入和更新语句是以完全不同的方式生成的。 在插入时,我得到以下 sql 代码:

exec sp_executesql N'INSERT INTO 
 [dbo].[stPost]([dbo].[stPost].[postGuid],[dbo].[stPost].
 [postCreatedDate],[dbo].[stPost].[postUpdatedDate],[dbo].
 [stPost].[postTitle],[dbo].[stPost].[postDescription],[dbo].[stPost].[deleted])
VALUES
 (@ins_dbostPostpostGuid,@ins_dbostPostpostCreatedDate,
  @ins_dbostPostpostUpdatedDate,@ins_dbostPostpostTitle,
  @ins_dbostPostpostDescription,@ins_dbostPostdeleted);
SELECT SCOPE_IDENTITY() as new_id;
SELECT SCOPE_IDENTITY() as new_id',N'@ins_dbostPostpostGuid uniqueidentifier,
               @ins_dbostPostpostCreatedDate datetime,
               @ins_dbostPostpostUpdatedDate datetime,
               @ins_dbostPostpostTitle nvarchar(10),
               @ins_dbostPostpostDescription nvarchar(32),
               @ins_dbostPostdeleted bit',
               @ins_dbostPostpostGuid='91695935-588B-4617-8DB0-14210E97F718',
               @ins_dbostPostpostCreatedDate=''2009-07-14 14:11:52:997'',
               @ins_dbostPostpostUpdatedDate=''2009-07-14 14:11:52:997'',
               @ins_dbostPostpostTitle=N'Post title',
               @ins_dbostPostpostDescription=N'cool post at 14.07.2009 14:11:52',
               @ins_dbostPostdeleted=0

在更新时

exec sp_executesql N'UPDATE [stPost] 
 SET postDescription=@up_postDescription, 
postTitle=@up_postTitle, 
postUpdatedDate=@up_postUpdatedDate
 WHERE [dbo].[stPost].[postID] =
 @0',N'@up_postDescription varchar(40),
 @up_postTitle varchar(18),
 @up_postUpdatedDate varchar(19),
 @0 int',
 @up_postDescription='cool post UPDATED at 14.07.2009 14:11:58',
 @up_postTitle='Post title Updated',
 @up_postUpdatedDate='14.07.2009 14:11:58',
 @0=2

插入和更新日期以不同的方式传递。 解析“14.07.2009”会导致值超出范围。 我想问题可能出在文化/全球化设置中,因为我将本地文化设置为俄语,并且 SQL Server 排序规则设置为 Cyrillic_General_CI_AS。 但问题不会发生在插入时,只会发生在更新时。 这让我认为问题出在亚音速的某个地方。 任何帮助或想法将不胜感激。

弗拉基米尔

I've been using subsonic 3.0 for the past few days and ran into a problem. Using ActiveRecord and calling save on the existing record caused:

[SqlException (0x80131904): The conversion of a char data type to a datetime
                            data type resulted in an out-of-range datetime value.
                            The statement has been terminated.]

The code I'm using is just to demonstrate the problem:

public void ButtonCreate_OnClick(object sender, EventArgs e) {
    stPost post = new stPost();
    post.postCreatedDate = DateTime.Now;
    post.postDescription = "cool post at " + DateTime.Now.ToString();
    post.postGuid = Guid.NewGuid();
    post.postTitle = "Post title";
    post.postUpdatedDate = DateTime.Now;

    post.Save();
}

public void ButtonUpdate_OnClick(object sender, EventArgs e) {
    stPost post = stPost.All().ToList<stPost>()[0];
    if (post != null && !post.IsNew()) {
        post.postDescription = "cool post UPDATED at " + DateTime.Now.ToString();
        post.postTitle = "Post title Updated";
        post.postUpdatedDate = DateTime.Now;

        post.Save();
    }
}

Looking into sql profiler and resharper I found that insert and update statements are generated in completely different ways. On insert I'm getting the following sql code:

exec sp_executesql N'INSERT INTO 
 [dbo].[stPost]([dbo].[stPost].[postGuid],[dbo].[stPost].
 [postCreatedDate],[dbo].[stPost].[postUpdatedDate],[dbo].
 [stPost].[postTitle],[dbo].[stPost].[postDescription],[dbo].[stPost].[deleted])
VALUES
 (@ins_dbostPostpostGuid,@ins_dbostPostpostCreatedDate,
  @ins_dbostPostpostUpdatedDate,@ins_dbostPostpostTitle,
  @ins_dbostPostpostDescription,@ins_dbostPostdeleted);
SELECT SCOPE_IDENTITY() as new_id;
SELECT SCOPE_IDENTITY() as new_id',N'@ins_dbostPostpostGuid uniqueidentifier,
               @ins_dbostPostpostCreatedDate datetime,
               @ins_dbostPostpostUpdatedDate datetime,
               @ins_dbostPostpostTitle nvarchar(10),
               @ins_dbostPostpostDescription nvarchar(32),
               @ins_dbostPostdeleted bit',
               @ins_dbostPostpostGuid='91695935-588B-4617-8DB0-14210E97F718',
               @ins_dbostPostpostCreatedDate=''2009-07-14 14:11:52:997'',
               @ins_dbostPostpostUpdatedDate=''2009-07-14 14:11:52:997'',
               @ins_dbostPostpostTitle=N'Post title',
               @ins_dbostPostpostDescription=N'cool post at 14.07.2009 14:11:52',
               @ins_dbostPostdeleted=0

And on update

exec sp_executesql N'UPDATE [stPost] 
 SET postDescription=@up_postDescription, 
postTitle=@up_postTitle, 
postUpdatedDate=@up_postUpdatedDate
 WHERE [dbo].[stPost].[postID] =
 @0',N'@up_postDescription varchar(40),
 @up_postTitle varchar(18),
 @up_postUpdatedDate varchar(19),
 @0 int',
 @up_postDescription='cool post UPDATED at 14.07.2009 14:11:58',
 @up_postTitle='Post title Updated',
 @up_postUpdatedDate='14.07.2009 14:11:58',
 @0=2

On insert and update date is passed in the different ways. And parsing '14.07.2009' results in out-of-range value. I guess the problem might be somewhere in cultural/globalization settings because I have my local culture set to Russian, and sql server collation is set to Cyrillic_General_CI_AS. But the problem doesn't occure on insert, only on updates. And this makes me think that problem is somewhere in subsonic.
Any help or ideas will be very appreciated.

Vladimir

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文