0

我通过 DataAdapter1.Update() 方法将新行插入到数据集 dsStaff1 中。我收到以下错误:

无法将值 NULL 插入到列“已启用”、表“dbo.tblStaff”中;列不允许空值。插入失败。该语句已终止。

我在 tblStaff 中确实有一个布尔字段“已启用”,我想这是 SQL Server 中的关键字。它默认为“真”。我真的无法更改该字段的名称。有解决办法吗?或者我做错了什么?PS:我正在通过 sqlcommand 生成器生成插入、更新命令。

4

3 回答 3

1

如果您有默认值,那么您的 SQL 命令构建器必须发送 NULL (DBNull.Value) 以覆盖默认值。默认值仅在以下情况下适用:

  • 如果您没有为 INSERT 中的列指定值
  • 或者如果您使用关键字 DEFAULT

这两种形式之一:

--Enabled is not mentioned, will use default
INSERT (col1, col1) VALUES (col1, col2) 

--Enabled will use DEFAULT
INSERT (col1, col1, Enabled) VALUES (col1, col2, DEFAULT) 
于 2011-01-31T06:15:27.107 回答
0

只需在更新参数中将固定值传递给数据集

喜欢

<asp:Parameter Name="col1" Type="boolean" DefaultValue="true"/>
于 2011-01-31T09:33:28.570 回答
0

我的代码:。更新失败。

        Dim dr As System.Data.DataRow

        dr = DsStaff1.Tables(0).NewRow()

        dr.BeginEdit()
        dr.Item("FirstName") = txtName.Text
        dr.Item("LastName") = txtSurname.Text

        dr.Item("StaffID") = txtStaffID.Text
        dr.Item("Enabled") = cbActive.Checked
        dr.EndEdit()
        DsStaff1.Tables(0).Rows.Add(dr)

        DataAdapter1.Update(DsStaff1)

我也在 DataAdapter1 中使用 TableMappings

        DataAdapter1.TableMappings.Add("Table", DsStaff1.Tables("tblStaff").ToString)

PS 更新和删除现有行可以正常工作。它只是插入一个新行是行不通的。

于 2011-02-01T02:19:35.437 回答