简体   繁体   English

数据库中的C#DateTime类和Datetime

[英]C# DateTime Class and Datetime in database

I have the following problem. 我有以下问题。

I have an object with some DateTime properties , and a Table in database that I store all that objects , in Sql server I want to store the DateTime properties in some columns of DateTime Datatype, but the format of datetime in sql server is different from the DateTime class in c# and I got an sql exception saying "DateTime cannot be parsed". 我有一个具有某些DateTime属性的对象,并且在数据库中有一个表将所有这些对象存储在Sql Server中,我想将DateTime属性存储在DateTime数据类型的某些列中,但是sql server中datetime的格式与在c#中的DateTime类,我得到一个SQL异常,说“无法解析DateTime”。 I know how to solve this by making the format yyyy-MM-dd but is this the proper and best solution to do this? 我知道如何通过将格式设置为yyyy-MM-dd来解决此问题,但这是否是实现此目的的最佳方法?

public void UpdateInvitation(string ownerId, string couponKey, string status, string invitedEmail, DateTime? sentDate, DateTime? redeemedDate)
    {
        using (var con = new SqlConnection(this.ConnectionString))
        {
            try
            {
                con.Open();
                var command =
                        new SqlCommand(
                                "UPDATE INVITATIONS SET Status='@status', InvitedEmail='@invitedEmail', SentDate='@sentDate', RedeemDate='@redeemedDate' WHERE CouponKey='@CouponKey' AND OwnerId='@OwnerId'");
                var ownerIdParam = new SqlParameter("@ownerId", ownerId);
                var couponKeyParam = new SqlParameter("@couponKey", couponKey);
                var statusParam = new SqlParameter("@status", status);
                var invitedEmailParam = new SqlParameter("@invitedEmail", invitedEmail);
                var sentDateParam = new SqlParameter("@sentDate", sentDate.Value) { SqlDbType = SqlDbType.DateTime };
                var redeemedDateParam = new SqlParameter("@redeemedDate", redeemedDate.Value) { SqlDbType = SqlDbType.DateTime };
                command.Parameters.AddRange(new[]
                                            {
                                                    ownerIdParam, couponKeyParam, statusParam, invitedEmailParam,
                                                    sentDateParam, redeemedDateParam
                                            });
                command.Connection = con;
                var rowsAffected = command.ExecuteNonQuery();
                Log.DebugFormat("Invitation updated for owner id : [{0}] with key {1}, {2} rows affected", ownerId,
                                couponKey, rowsAffected);
            }
            catch (Exception exception)
            {
                throw new InvitationDataContextException(exception);
            }
        }
    }

没有引号

new SqlCommand("UPDATE INVITATIONS SET Status=@status, InvitedEmail=@invitedEmail, SentDate=@sentDate, ...

好吧,在不查看您的代码的情况下,我建议您开始使用Parameterized Queries

If you're using SQL Server 2008, the new datetime2 data type maps directly to the .NET DateTime object. 如果您使用的是SQL Server 2008,则新的datetime2数据类型将直接映射到.NET DateTime对象。 If you're stuck with using SQL's old datetime field, be careful you don't pass an out-of-range date, or else you're looking at a runtime error. 如果您坚持使用SQL的旧datetime字段,请当心不要传递超出范围的日期,否则会遇到运行时错误。 Trying to insert DateTime.MinValue is a common mistake. 尝试插入DateTime.MinValue是一个常见错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM