DateTime字段更新导致异常

时间:2009-07-14 10:54:45

标签: subsonic subsonic3

过去几天我一直在使用亚音速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我发现insert和update语句以完全不同的方式生成。在插入时我得到以下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 collat​​ion设置为Cyrillic_General_CI_AS。但问题不会在插入时发生,仅在更新时发生。这让我觉得问题在于亚音速。 任何帮助或想法将非常感激。

弗拉基米尔

0 个答案:

没有答案