简体   繁体   English

Dapper SqlDateTime在SQL Server中使用GETDATE()溢出

[英]Dapper SqlDateTime overflow using GETDATE() in SQL Server

I have the following code which gets the input from a form and calls a stored procedure to store it in a database. 我有以下代码,该代码从表单获取输入并调用存储过程以将其存储在数据库中。

 [HttpPost]
 public ActionResult Index(GuestMessage model)
 {
        if (ModelState.IsValid)
        { 
            using(IDbConnection conn = DatabaseAcess.OpenConnection())
            {
                const string storedProcedure = "dbo.InsertMessage";
                conn.Execute(storedProcedure, new GuestMessage { Name = model.Name, Email = model.Email, Message = model.Message }, null, null, CommandType.StoredProcedure);

                return View("ThankYou");
            }
         }
        return View();
}

There are three fields, Name , E-mail and Message that have to be filled in by the user. 用户必须填写三个字段,即NameE-mailMessage In the database I store the data that the user enters, from these three fields, and the time of the entry using a stored function called dbo.Insert Message. 在数据库中,我使用一个名为dbo.Insert Message的存储函数存储了用户从这三个字段中输入的数据以及输入时间。

BEGIN
  BEGIN TRAN

  SET NOCOUNT ON;

  DECLARE @GuestID int;
  INSERT INTO Guest(Name, Email) VALUES (@Name, @Email);
  SELECT @GuestID = scope_identity();

  INSERT INTO Message (EntryDate, GuestID, Message, Status) 
  VALUES (GETDATE(), @GuestID, @Message, 2);

  COMMIT TRAN
END

Notice that I do not pass the entry date and time as an input parameter to the stored procedure since there is no need for that. 注意,我没有将输入日期和时间作为输入参数传递给存储过程,因为没有必要。 I use the built-in function GETDATE() in SQL Server to determine the time a user has entered a message. 我使用SQL Server中的内置函数GETDATE()来确定用户输入消息的时间。

However, whenever I try to enter a message I get the following error: 但是,每当我尝试输入消息时,都会出现以下错误:

SqlDateTime overflow. SqlDateTime溢出。 Must be between 1/1/1753 12:00:00AM and 12/31/9999 11:59:59PM 必须介于1/1/1753 12:00:00 AM和12/31/9999 11:59:59 PM之间

I would suggest the following change: 我建议进行以下更改:

conn.Execute(storedProcedure,
    new { Name = model.Name, Email = model.Email, Message = model.Message },
    commandType: CommandType.StoredProcedure);

The only actual change there is that I have swapped new GuestMessage { ... } for new { ... } . 唯一实际的更改是,我已将new GuestMessage { ... }替换为new { ... } What I suspect is happening is that your GuestMessage type has additional properties that aren't needed , but which it is sending as parameters. 怀疑正在发生的事情是,您的GuestMessage类型具有不需要的其他属性,但它会作为参数发送。 A DateTime left at its default value is "00:00:00.0000000, January 1, 0001". 保留其默认值的DateTime是“ 0001年1月1日00:00:00.0000000”。 By swapping to just new { ... } , we have explicitly limited the members we are sending to Name , Email and Message , because those are the only things defined . 通过交换到new { ... } ,我们明确限制了要发送给NameEmailMessage ,因为这是定义的唯一内容

Note: when using plain-text, dapper does use some voodoo to try to see which parameter names are actually used in the SQL, and it doesn't send anything that it knows isn't referenced in the SQL - however, it cannot do this with a stored procedure. 注意:在使用纯文本时,dapper确实会使用一些巫毒教来尝试查看SQL中实际使用了哪些参数名称,并且它不会发送它知道在SQL中未引用的任何内容-但是,它无法执行这与存储过程。

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

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