首页 > 解决方案 > 更新时 ASP.NET 表适配器日期溢出

问题描述

我正在为 AdventureWorks 示例数据库创建一个前端。

我正在尝试更新 HumanResources.Department 中的一行。

我正在为此表使用带有表适配器的数据集。

插入和更新行会导致问题:

 public Department InserDepartment(Department department) 
 {
    try 
    {
       department.ModifiedDate = DateTime.Now;
       Int32 id = departmentTableAdapter.Insert(department.Name, department.GroupName, department.ModifiedDate);   
       return new Department();
     }
     catch (Exception ex) 
     {
        return new Department();
     }
 }
public Department UpdateDepratment(Department department) 
{
   try
   {
      HumanResourcesDataSet.DepartmentDataTable tblDepartments = departmentTableAdapter.GetDataByDepartmentId(department.DepartmentID);
      HumanResourcesDataSet.DepartmentRow departmentRow = tblDepartments[0];
      departmentRow.Name = department.Name;
      department.GroupName = department.GroupName;
      department.ModifiedDate = DateTime.Now;
      departmentTableAdapter.Update(tblDepartments);
      department = GetDepartment(department.DepartmentID);
      return department;
   }
   catch (Exception ex)
   {
       // TBD
   }
}

出现以下异常:

提供的时间值的小数部分会溢出相应 SQL Server 参数或列的刻度。增加 DBPARAMBINDINFO 中的 bScale 或列比例以更正此错误。

该问题是由我的更新方法中的以下行引起的:

department.ModifiedDate = DateTime.Now;

数据集接受此列的 DateTime 类,数据库定义是 datetime,而不是 null。

我已经在其他数据库中实现了日期时间列,并且从未遇到过这个问题。

我错过了什么,导致问题?
我应该在哪里寻找这个问题的解决方案?

标签: c#asp.netsql-server

解决方案


所以,我没有完全找到这个问题的原因和适当的解决方案。我为相关表的更新添加了新的存储过程,这似乎解决了一些问题。问题出在我的数据提供程序 (System.Data.OleDb) 和我的 SQL 服务器中。

服务器是 SQL - Express。我从 .bak 文件中导入了数据库。

在存储过程中,我设置ModifiedDate = CURRENT_TIMESTAMP并传递其他值作为参数。

我的假设是,我的更新干扰了以下 SQL 脚本

[修改日期] [日期时间] 非空约束 [DF_Department_ModifiedDate] 默认 (GETDATE())

我真的不想修改数据库的结构并更改默认值,因为到目前为止我还不太了解数据库。

我也不能真正忽略该列,因为我只能使用我的表适配器更新整行,其中包括在我的 DataRow 实例中设置的日期。


推荐阅读