vb.net 如果文本框为空,则在数据库中写入 Null

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13006995/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 16:00:44  来源:igfitidea点击:

Write Null in Database if the TextBoxes are Empty

asp.netsql-servervb.netado.netdbnull

提问by Kristian Hernan C. Manuel

All I want to do is to insert Null with the database when the textbox is empty.

我想要做的就是在文本框为空时将 Null 与数据库一起插入。

 --ACCOUNT

 AS
 BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
SET IDENTITY_INSERT account ON


  INSERT INTO dbo.account
      ( 
        AccntID                   ,
        managedby                 ,
        AccountHolder             ,
        Description               ,
        AccountType               ,
        ContactPerson             ,
        ContactNumber             ,
        EmailAddress              ,
        Address                   ,
        ClientTYpe                ,
        SchemeType                ,
        SalesManager              ,
        DateCreated               ,
        Login                     ,
        Password                  ,
        Balance                   ,
        FilterOption              ,
        Enable22                  ,
        AllowExtendedConfig                  
      ) 
-- Insert statements for procedure here

 VALUES (@AccntID, @managedby, @AccountHolder, @Description, @AccountType, @ContactPerson, @ContactNumber, @EmailAddress, @Address, @ClientType, @SchemeType, @SalesManager, @DateCreated, @Login, @Password, @Balance, @FilterOption, @Enable22, @AllowExtendedConfig)
SET IDENTITY_INSERT account OFF



END

What is the best way to add NULL values to the database when my textboxes are empty

当我的文本框为空时,将 NULL 值添加到数据库的最佳方法是什么

采纳答案by Parag Meshram

Set up helper functions such as:

设置辅助函数,例如:

Public Function DbNullOrStringValue(ByVal value As String) As Object
    If String.IsNullOrEmpty(value) Then
        Return DBNull.Value
    Else
        Return value
    End If
End Function

And so the first code block would be simplified by calling it thus:

所以第一个代码块可以通过这样调用来简化:

Cmd.Parameters.AddWithValue("@AccntID", DbNullOrStringValue(TextBox1.Text)) 
Cmd.Parameters.AddWithValue("@managedby", DbNullOrStringValue(TextBox2.Text)) 
Cmd.Parameters.AddWithValue("@AccountHolder", DbNullOrStringValue(TextBox3.Text)) 
Cmd.Parameters.AddWithValue("@Description", DbNullOrStringValue(TextBox4.Text)) 
Cmd.Parameters.AddWithValue("@AccountType", DbNullOrStringValue(TextBox5.Text))

Cmdis your SqlCommanadobject

Cmd是你的SqlCommanad对象

回答by Minus

Consider something lighter than a new method :

考虑一些比新方法更轻的东西:

    Cmd.Parameters.AddWithValue("@AccntID", IIF(String.IsNullOrEmpty(TextBox1.Text),  DBNull.Value, TextBox1.Text)) 

But for the rest, use a SqlDataAdapteras suggested before.

但对于其余部分,请SqlDataAdapter按照之前的建议使用 a 。

回答by Farfarak

In the stored procedure you can use following:

在存储过程中,您可以使用以下内容:

Assumption: parameter name is @TextData

假设:参数名称是@TextData

 NULLIF(LTRIM(RTRIM(@TextData)),'')

It will return NULL if @TextData = ''.

如果@TextData = '',它将返回NULL。

回答by Hyman Gajanan

The best way will be to inherited text box and add additional functionality to it

最好的方法是继承文本框并为其添加附加功能

Public Class MyText
    Inherits TextBox

    'Either change default text property (but this will bug you while comparing text property)
    Public Overrides Property Text() As String
        Get
            Return MyBase.Text
        End Get
        Set(ByVal value As String)
            MyBase.Text = value
        End Set
    End Property

    'Best way will be to write new property to use in sql query
    Public ReadOnly Property TextSQL() As String
        Get
            If Me.Text = String.Empty Then
                Return "NULL"
            Else
                Return Me.Text
                'Return "'" & Me.Text & "'" 'or this
            End If
        End Get
    End Property
End Class

now use this text box in your application

现在在您的应用程序中使用此文本框

cmd.Parameters.AddWithValue("@accountholder", txt_accHolder.TextSQL)