【问题标题】:How to insert NULL into database if form field is empty如果表单字段为空,如何将 NULL 插入数据库
【发布时间】:2011-01-18 09:59:08
【问题描述】:

我有一个表单和从表单插入数据的存储过程。它工作正常,只是如果一个字段没有填写,它不会将NULL 插入到 SQL 中,它会插入""

我尝试了几种不同的方法,但似乎都没有插入NULL,下面的仍然插入"",谁能指出我正确的方向?

这是代码的必需部分,如果您需要更多,请告诉我。

Dim rdr As SqlDataReader
            Dim cmdInsert As SqlCommand = New SqlCommand()
            cmdInsert.CommandText = "spPersonalDetailsInsert"
            cmdInsert.CommandType = CommandType.StoredProcedure
            cmdInsert.Connection = connSQL


            Dim firstname, lastname, address, address1, town, county, postcode As SqlParameter
            'convert to null if ""
            Dim frmFirstName As String
            If pd_first_name.Text = "" Then
                frmFirstName = Convert.DBNull
            Else
                frmFirstName = pd_first_name.Text
            End If

            firstname = New SqlParameter()
            firstname.ParameterName = "@firstname"
            firstname.SqlDbType = SqlDbType.NVarChar
            firstname.Size = 50
            firstname.Direction = ParameterDirection.Input
            firstname.Value = frmFirstName

编辑

我测试了以下代码:

If pd_first_name.Text = "" Then
            frmFirstName = DBNull.Value
        Else
            frmFirstName = pd_first_name.Text
        End If

但它仍然没有插入 NULL 所以我测试了这个:

            If pd_first_name.Text = "" Then
                Response.Write("NULL")
                address1.Value = DBNull.Value
            Else
                Response.Write("NOT NULL")
                address1.Value = pd_address1.Text
            End If

因此,如果我在address1 字段中没有输入任何内容,它应该将NULL 写入屏幕,但它始终会写入NOT NULL。一个空的表单域等于什么?在经典 ASP 中,它总是""

【问题讨论】:

    标签: asp.net vb.net


    【解决方案1】:

    你需要使用DBNull.Value

                If String.IsNullOrEmpty(pd_first_name.Text.ToString().Trim) = true Then
                    frmFirstName = DBNull.Value
                Else
                    frmFirstName = pd_first_name.Text
                End If
    

    【讨论】:

    • 我刚刚在上面测试过,它仍然没有插入 NULL,请参阅上面的编辑问题,谢谢。
    • 这确实有效,我的错,我在编辑的代码中引用了错误的表单字段,谢谢 Barry。
    【解决方案2】:

    在我的情况下,使用 'Nothing' 可以解决问题。像这样使用它

    If String.IsNullOrEmpty(pd_first_name.Text) = True Then
       frmFirstName = Nothing
    Else
       frmFirstName = pd_first_name.Text
    End If
    

    【讨论】:

    • 这也是我解决问题的方法。由于某种原因 DBNull.Value 不适用于 DateTime 字段
    【解决方案3】:

    如果它为空,为什么还要设置它?

            If pd_first_name.Text <> "" Then
              frmFirstName = pd_first_name.Text
              firstname = New SqlParameter()
              firstname.ParameterName = "@firstname"
              firstname.SqlDbType = SqlDbType.NVarChar
              firstname.Size = 50
              firstname.Direction = ParameterDirection.Input
              firstname.Value = frmFirstName
            End If
    

    【讨论】:

    • 它是一个表单项,如果没有输入它等于“”而不是NULL。
    【解决方案4】:

    我认为你的问题是frmFirstName是一个字符串,一个字符串不能代表DBNull。

    我认为这将解决您的问题(我刚刚注释掉了您的代码):

    Dim rdr As SqlDataReader
                Dim cmdInsert As SqlCommand = New SqlCommand()
                cmdInsert.CommandText = "spPersonalDetailsInsert"
                cmdInsert.CommandType = CommandType.StoredProcedure
                cmdInsert.Connection = connSQL
    
    
                Dim firstname, lastname, address, address1, town, county, postcode As SqlParameter
                'convert to null if ""
                Dim frmFirstName As String
                'If pd_first_name.Text = "" Then
                '    frmFirstName = Convert.DBNull
                'Else
                '    frmFirstName = pd_first_name.Text
                'End If
    
                firstname = New SqlParameter()
                firstname.ParameterName = "@firstname"
                firstname.SqlDbType = SqlDbType.NVarChar
                firstname.Size = 50
                firstname.Direction = ParameterDirection.Input
                If pd_first_name.Text = "" Then
                      firstname.Value = DBNull.Value
                Else
                      firstname.Value = frmFirstName
                End If
    

    【讨论】:

      【解决方案5】:
      Dim TempStr As String
      TempStr= "spPersonalDetailsInsert"
      TempStr = TempStr.Replace("''", "null")
      cmdInsert.CommandText = TempStr
      

      现在不用了

      If pd_first_name.Text = "" Then
         Response.Write("NULL")
         address1.Value = DBNull.Value
      Else
         Response.Write("NOT NULL")
         address1.Value = pd_address1.Text
      End If 
      

      希望这可能会有所帮助

      【讨论】:

        【解决方案6】:

        在创建存储过程时,将这些列设为空,可以为空.. 喜欢

        CREATE PROCEDURE [dbo].[USP_TDS_SaveRecod]
        
        @ID INT,
        
        @CODE  INT,
        
        @FIRSTNAME VARCHAR(8)=NULL,
        
        @CITY VARCHAR(15)=NULL
        
        AS
        
        BEGIN
        
            .........................
        
            .........................
        
            .........................
        END
        

        然后在代码中不要添加那些为空的参数..

        cmd.Parameters.Add("@ID", SqlDbType.Int).Value = obj.ID;
        cmd.Parameters.Add("@CODE", SqlDbType.Int).Value = obj.CODE;
        if(pd_first_name.Text != "")
        {
            cmd.Parameters.Add("@FIRSTNAME", SqlDbType.VarChar).Value = pd_first_name.Text;
        }
        if(city.Text != "")
        {
            cmd.Parameters.Add("@CITY", SqlDbType.VarChar).Value = pd_first_name.Text;
        }
        

        【讨论】:

          【解决方案7】:
          If RdFree.Checked = True Then
              Dim nu As String = "NULL"
              UpdPolicys.Append(", AccIDFree = " & nu & " , AccTypeIDFree = " & nu & " ")
          End If
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-09-10
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-02-07
            • 1970-01-01
            相关资源
            最近更新 更多