我想在一些asp.net会员字段中添加一些日期(LastLoginDate,LastPasswordChangedDate等)
所以我做的是
DateTime sendDate = new DateTime(1754, 1, 1, 12, 0, 0);
然后我厌倦了使用linq来添加它们。
它返回异常
"SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM."
我不确定如何将其转换为可以放入的内容。
DateTime dateFields = new DateTime(1800, 1, 1, 12, 0, 0);
aspnet_Membership membership = new aspnet_Membership();
membership.ApplicationId = applicationId;
membership.UserId = userId;
membership.Password = password;
membership.PasswordFormat = passwordFormat;
membership.PasswordSalt = base64Salt;
membership.MobilePIN = null;
membership.Email = email;
membership.LoweredEmail = email.ToLower();
membership.PasswordQuestion = null;
membership.PasswordAnswer = null;
membership.IsApproved = isApproved;
membership.IsLockedOut = false;
membership.CreateDate = dateCreated;
membership.LastLoginDate = dateFields;
membership.LastPasswordChangedDate = dateFields;
membership.LastLoginDate = dateFields;
membership.FailedPasswordAttemptWindowStart = dateFields;
membership.FailedPasswordAnswerAttemptCount = 0;
membership.FailedPasswordAnswerAttemptWindowStart = dateFields;
membership.FailedPasswordAnswerAttemptCount = 0;
membership.Comment = null;
// from asp.net mvc unleashed book.
GenericRepository.Create<aspnet_Membership>(membership);
答案 0 :(得分:2)
也许还有一个你尚未填充的DateTime字段?在这种情况下,它默认为01/01/0001,并且在SQL Server的DATETIME
范围之外。
如果您使用的是SQL Server 2008,则可以避免使用DATETIME2
类型 - 其有效日期范围为01/01/0001至12/31/9999
马克
答案 1 :(得分:1)
你从哪里获得c#中的“dateCreated”?它有什么价值?
你没有设置“LastLockoutDate”......你实际上有两次“LastLoginDate”。为LastLockoutDate设置了什么值?
看起来“dateFields”对于SQL Server术语中的日期时间是正确的(由aspnet_Membership使用),也许错误来自其他地方......
答案 2 :(得分:0)
可能你发错的顺序。
您的SQL子句是什么样的?如果你只是设置一个SqlParameter(你应该这样),假设DateTime的参数设置正确,那么生活应该没那么好。
答案 3 :(得分:0)
答案 4 :(得分:0)
尝试使用此功能转换为。
public static string GetDateTimeSQLString(DateTime SomeTime)
{
//yyyy-MM-dd HH:mm:ss
string SqlTime = SomeTime.Year.ToString() + "-" + SomeTime.Month.ToString() + "-" + SomeTime.Day.ToString() + " " + SomeTime.Hour.ToString() + ":" + SomeTime.Minute + ":" + SomeTime.Second.ToString();
return SqlTime;
}