Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在vb.net中为字符串变量插入空值_Vb.net_String_Null_Dbnull - Fatal编程技术网

在vb.net中为字符串变量插入空值

在vb.net中为字符串变量插入空值,vb.net,string,null,dbnull,Vb.net,String,Null,Dbnull,我正在尝试向string类型的变量插入空值。请让我知道可能的方法。我在谷歌上搜索了这个问题,发现DBNul.value可以指定给必须作为NULL插入的特定变量。当我尝试时,它错误地说DB.Null不能转换为typesystem.string您可以通过将字符串设置为nothing来为中的字符串分配Null值 dim MyString as string MyString = nothing VBNull.Value通常用于在使用数据库插入时应用null值,或验证接收到的值是否为null 编辑:当

我正在尝试向string类型的变量插入空值。请让我知道可能的方法。我在谷歌上搜索了这个问题,发现DBNul.value可以指定给必须作为NULL插入的特定变量。当我尝试时,它错误地说DB.Null不能转换为type
system.string

您可以通过将字符串设置为nothing来为中的字符串分配Null值

dim MyString as string
MyString = nothing
VBNull.Value通常用于在使用数据库插入时应用null值,或验证接收到的值是否为null

编辑:当您明确表示要向数据库添加空值时,可以通过直接在命令对象中设置空值来实现:

Dim Command As New OleDb.OleDbCommand 'Use the proper database command
Command.CommandText = "INSERT INTO Table (Column1, Column2) Values (@Value1, @Value2)"
Command.Parameters.AddWithValue("@Value1", If(string.IsNullOrEmpty(MyString1), DBNull.Value, MyString1))
Command.Parameters.AddWithValue("@Value2", If(string.IsNullOrEmpty(MyString2), DBNull.Value, MyString2))

你什么都不想用


myStringVar=Nothing

使用LINQtoSQL或类似的实体框架,可以使用
Nothing

我有一张简单的桌子和

CREATE TABLE [dbo].[Table_1](
    [ID] [bigint] IDENTITY(1,1) NOT NULL,
    [TestID] [bigint] NOT NULL,
    [Description] [nchar](10) NULL,
使用这个代码

Sub Main()
    Using dc As New DataClasses1DataContext
        Dim testNothing = New Table_1
        testNothing.Description = Nothing
        testNothing.TestID = 1
        dc.Table_1s.InsertOnSubmit(testNothing)
        Dim testEmpty = New Table_1
        testEmpty.Description = ""
        testEmpty.TestID = 2
        dc.Table_1s.InsertOnSubmit(testEmpty)
        dc.SubmitChanges()
    End Using
End Sub
这张表的内容是

IDTestedDescription
11NULL

22

看看你有什么会有帮助。这里不清楚您是在尝试构建SQL命令,还是希望结果是什么样子。很明显,这里还有别的事情。否则,为什么不干脆
myStringVar=null?我正试图通过SQL向列插入空记录。我还没有尝试myStringVar=null。请查找下面的代码段。。。我正在使用SqlCommand…我正在从文件中读取flag的值,并尝试插入数据库。。。。我无法将整个代码粘贴到这里……但下面是我面临的实际部分,我将Dim flag作为String if flag=“”query=插入表(id、名称、状态、标志)值(1、xx、NY、NULL)请将问题信息放入问题并删除注释。您可以将更新放在它上面,以表明您已将其添加到原始问题之外。切勿使用字符串连接将值放入SQL语句中!这是一个很好的方法,可以在一年后发现你的应用程序在六个月前被黑客攻击。这将在数据库中插入一个空格,而不是OP要求的空值。我可能误解了他的问题,但我认为他试图将值应用于变量,而不是数据库,不是吗?如果我使用string=nothing…数据库中的该列就会出现一个空白框,这会影响我是否查询要插入的列augustoq…的空值database@Kals编辑了SQL插入的答案,请签出。这不会插入空值。它将插入一个空白值。