【问题标题】:How to save a null datetime to SqlServer DB? not working如何将空日期时间保存到 Sql Server DB?不工作
【发布时间】:2016-11-29 04:32:40
【问题描述】:

VS-Studio 2012 Web Express、ASP.NET、WebForms、VB、SqlServer、WebSite 应用程序无法将 DateTime 的 NULL 值保存到强类型 ROW:

      Dim oRowVEHICLES As Main_TblAdap.tVEHICLESRow = odtVEHICLES.Rows(0)  ' (0) is the first row.
      oRowVEHICLES.[WElectrical] = If(WElectrical.Year < 10, DBNull.Value, WElectrical)
...etc...

目前,DetailsView 模板字段文本框为 或空或“”,BLL 函数将其显示为日期,例如:#01/01/0001#。所以我测试传入变量的 YEAR 值,如果小于 10,然后将 DBNull.Value 保存到 oRowVehicles.[WElectrical] 但由于 datatype=Date 并且无法将 DBNull 转换为 Date 失败。

DB 字段是 Date 类型,允许为空。

TableAdapter.xsd 视图显示默认值为 。

那么,为什么 oRowVehicles 的 Date 不能为空?

如何使 WElectrical 列 DATE 为空?

我一定忽略了一些东西,因为我不能是唯一一个将可选 DATE 值保存到 Sql-DB 的人。

欢迎您的 cmets 和解决方案。谢谢...约翰

编辑 详细信息视图中的 ASPX 代码一个 DATE 字段(其他类似):

              <asp:TemplateField HeaderText="Electrical End Date" SortExpression="WElectrical">
                 <EditItemTemplate>
                    <TGADate:GADate ID="ucdtWElectrical" runat="server" Enabled="True" MinDate="01/01/1980" MaxDate="12/31/2050"
                       Caption="Electrical End Date" HideCaption="True" Width="100"
                       IsRequired="false"
                       UpdateMode="Conditional"
                       Text='<%# Bind("WElectrical")%>' />
                 </EditItemTemplate>
                 <InsertItemTemplate>
                    <TGADate:GADate ID="ucdtWElectrical2" runat="server" Enabled="True" MinDate="01/01/1980" MaxDate="12/31/2050"
                       Caption="Electrical End Date" HideCaption="True" Width="100"
                       IsRequired="false"
                       UpdateMode="Conditional"
                       Text='<%# Bind("WElectrical")%>' />
                 </InsertItemTemplate>
                 <ItemTemplate>
                    <asp:Label ID="lblWElectrical" runat="server" Text='<%# clsGA_Lib1.fnGetDateTextFromObject(Eval("WElectrical"))%>' Style="font-weight: bold;"></asp:Label>
                 </ItemTemplate>
                 <ItemStyle Font-Bold="true" />
              </asp:TemplateField>

ASPX 中的Object DataSource 参数定义。

 <asp:Parameter Name="WElectrical" Type="DateTime" />

BLL 代码:

   <System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Update, False)> _
Public Function UpdateFromDetailsView(ByVal original_UID_VEHICLE As Int32, _
                                     ByVal VehicleNbr As String, _
...other function parameter variables...      
                                     ByVal WElectrical As Date, _
...other function parameter variables...      
                                   ) As Boolean

  ' Get the new VEHICLE-row instance to be updated.
  Dim odtVEHICLES As Main_TblAdap.tVEHICLESDataTable = Adapter.GetVhclByVhclID(original_UID_VEHICLE)

  If odtVEHICLES.Count <> 1 Then
     ' no matching record found, return false
     Return False
  End If

  ' Populate the values of the ROW.
  Dim oRowVEHICLES As Main_TblAdap.tVEHICLESRow = odtVEHICLES.Rows(0)  ' (0) is the first row.
  With oRowVEHICLES
     ...setting row-field values...
     .[WElectrical] = If(WElectrical.Year < 10, Nothing, WElectrical)
     ...etc...
  End With

  ' Update the oRowVEHICLES.
  Dim rowsAffected As Integer = Adapter.Update(odtVEHICLES)

  ' Return TRUE if precisely one row was INSERTED, otherwise false.
  Return CBool(rowsAffected = 1)
End Function

编辑上述代码的注释

进入 BLL 函数的 WElectrical 参数是一个 DATE,其值为 #01/01/0001#。
将值放入 ROW 对象的代码

.[WElectrical] = If(WElectrical.Year < 10, Nothing, WElectrical)

将 Nothing 作为行对象字段值。

Adapter.Update(odtVEHICLES) 更新 Sql-DB。

那么是什么导致 #01/01/0001# 值被放入 Sql-DB 中?

Sql-DB 列定义

//////// 编辑结束 ///////////

【问题讨论】:

  • 如果您正在处理 .net 对象,请使用 null 或 Nothing。仅当您直接处理数据库时才使用DBNull,例如向查询或 SP 发送参数。 WElectrical 是 DateTime? 类型的字段吗?我不喜欢 VB,所以你可能需要按照这里的要求做:stackoverflow.com/questions/12595775/…
  • 感谢您的评论。该链接非常有用且非常重要。我将检查这些更改并让您知道。谢谢。
  • 看起来您正在使用表格适配器。您需要将参数设置为 AllowNulls:msdn.microsoft.com/en-us/library/…
  • @JonP -- 谢谢,但数据集设计器已按照您的建议进行配置。

标签: asp.net sql-server datetime nullable bll


【解决方案1】:

做一件事:

将 DBNull.Value 更改为 Nothing

或者,您可以将数据集中的数据类型更改为 System.Object,并且 转到该数据列的属性 然后您可以在下拉列表中选择“(无)”。

将 Null 值设置为 >> 无

【讨论】:

  • 感谢您的回答。我确实将 DBNull.Value 更改为 Nothing,这让我克服了无法转换为 Date 类型的错误。然而,另一个建议并没有消除获得 BIND 值 #01/01/0001# 的问题,这实际上可能在我的用户控件中用于屏幕上的日期输入。现在真正的问题是为什么#01/01/0001# 被保存到 Sql-DB 而不是 NULL。我会在这件事上回复你。谢谢。
  • 发布您的代码,以便我们找到问题所在。您可能必须在绑定之前检查该值是否大于DateTime.MinDate。
  • 添加了代码 sn-ps -- 欢迎您的 cmets...谢谢。
  • 听起来你的数据库列不接受可为空的日期,因此它被初始化为日期的最小值
  • @kurakura88 - 请参阅上面的编辑代码,显示 Sql 列定义的图像 - 允许使用 NULL。感谢您的评论。
【解决方案2】:

感谢上述问题的所有评论员。 解决方案是将 Row-column-variable 更改为将 Nothing to Date? (可为空)如下...

 .[WElectrical] = If(WElectrical.Year < 10, CType(Nothing, Date?), WElectrical)

AND -- 将 dataset-column-definition(在 .xsd 中)更改如下:

DataType ==> System.Object(不是日期)

NullValue ==>(无)(不抛出异常)

衷心感谢所有贡献者——因为他们的每一个建议元素都对这个解决方案做出了贡献。
这是将可空值发送到 DataRow 列的解决方案。

感谢您的所有帮助。

【讨论】:

  • 谢谢@Andrew——我该怎么做?我将其标记为绿色复选标记。可以吗?
猜你喜欢
  • 2023-04-08
  • 1970-01-01
  • 1970-01-01
  • 2020-07-29
  • 2023-03-30
  • 1970-01-01
  • 2014-08-09
  • 2019-05-30
  • 2015-03-20
相关资源
最近更新 更多