过去几天我一直在使用亚音速 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。但是插入时不会出现问题,只会在更新时出现问题。这让我觉得问题出在亚音速的某个地方。任何帮助或想法将不胜感激。
弗拉基米尔