Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/8.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.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
Database 如果值为null,则将字符串设置为空_Database_Vb.net_Structure - Fatal编程技术网

Database 如果值为null,则将字符串设置为空

Database 如果值为null,则将字符串设置为空,database,vb.net,structure,Database,Vb.net,Structure,我不知道如何寻找我在这里试图解决的问题。在我正在编写的程序(在VB.Net中)中,我试图将从数据库中提取的值分配给结构中的不同变量 现在我的问题是,有时候,从数据库中提取的一些值是NULL,例如,不是每个电话号码都有分机。这就是我目前的代码: Structure CustomerContact Public _name As String Public _email As String Public _fax As String Public _phone1 As

我不知道如何寻找我在这里试图解决的问题。在我正在编写的程序(在VB.Net中)中,我试图将从数据库中提取的值分配给结构中的不同变量

现在我的问题是,有时候,从数据库中提取的一些值是
NULL
,例如,不是每个电话号码都有分机。这就是我目前的代码:

Structure CustomerContact
    Public _name As String
    Public _email As String
    Public _fax As String
    Public _phone1 As String
    Public _phone2 As String
    Public _phone3 As String
    Public _ext1 As String
    Public _ext2 As String
    Public _ext3 As String
    Public _type1 As String
    Public _type2 As String
    Public _type3 As String
End Structure

    Dim contactData As DataTable = CustomerDBFunctions.GetCustomerContacts(Customer)
    For Each row As DataRow In contactData.Rows
        If contacts.Count < 1 Then
            contacts.Add(New CustomerContact With {
                            ._name = row.Item("FullName").ToString() & " (" & row.Item("ContactType").ToString() & ")",
                            ._email = row.Item("Email").ToString(),
                            ._fax = row.Item("Fax").ToString(),
                            ._phone1 = row.Item("Phone").ToString(),
                            ._ext1 = row.Item("Extension").ToString(),
                            ._type1 = row.Item("PhoneType").ToString()})
        End If
    Next
结构客户联系人
Public\u名称作为字符串
公共电子邮件作为字符串
公共传真作为字符串
公共电话1作为字符串
公共电话2作为字符串
公共电话3作为字符串
Public\u ext1作为字符串
Public_ext2作为字符串
Public\u ext3作为字符串
Public\u type1作为字符串
公共_type2作为字符串
Public\u type3作为字符串
端部结构
Dim contactData As DataTable=CustomerDBFunctions.GetCustomerContacts(客户)
对于contactData.Rows中作为DataRow的每一行
如果接触。计数<1,则
联系人。添加(新客户联系人){
._name=row.Item(“全名”).ToString()和(“&row.Item(“联系人类型”).ToString()&”),
._email=row.Item(“email”).ToString(),
._fax=行项目(“fax”).ToString(),
._phone1=行.Item(“Phone”).ToString(),
._ext1=row.Item(“扩展”).ToString(),
._type1=row.Item(“PhoneType”).ToString())
如果结束
下一个

现在,当数据库中的值为
NULL
时,我收到一个错误,因为它无法为字符串分配NULL值。我希望在出现空值的情况下,将变量的值改为
“”
。我只是不确定该如何编码。

从技术上讲,问题不在于列为空
String
是一种引用类型,因此它只能为null(不过,如果它为null,您无论如何都不能对其调用
ToString
)。实际上,ADO.NET总是为行中包含空值的所有列返回
DBNull.Value

你可以这样检查它:

If row.Item("Phone") <> DBNull.Value Then
    customerContact._phone1 = row.Item("Phone")?.ToString()
End If
._phone1 = If(row.IsNull("Phone"), row.Item("Phone")?.ToString(), Nothing)
显然,如果您经常这样做,最好将其包装成一个可重用的函数。此外,如果您想将其缩短为一行,您可以这样做:

If row.Item("Phone") <> DBNull.Value Then
    customerContact._phone1 = row.Item("Phone")?.ToString()
End If
._phone1 = If(row.IsNull("Phone"), row.Item("Phone")?.ToString(), Nothing)

从技术上讲,问题不在于列为null
String
是一种引用类型,因此它只能为null(不过,如果它为null,您无论如何都不能对其调用
ToString
)。实际上,ADO.NET总是为行中包含空值的所有列返回
DBNull.Value

你可以这样检查它:

If row.Item("Phone") <> DBNull.Value Then
    customerContact._phone1 = row.Item("Phone")?.ToString()
End If
._phone1 = If(row.IsNull("Phone"), row.Item("Phone")?.ToString(), Nothing)
显然,如果您经常这样做,最好将其包装成一个可重用的函数。此外,如果您想将其缩短为一行,您可以这样做:

If row.Item("Phone") <> DBNull.Value Then
    customerContact._phone1 = row.Item("Phone")?.ToString()
End If
._phone1 = If(row.IsNull("Phone"), row.Item("Phone")?.ToString(), Nothing)

字符串串联可用于将
Nothing
转换为
(我的答案中的其他选项)


字符串串联可用于将
Nothing
转换为
(我的答案中的其他选项)


这里有一个类似的问题:您看到了什么异常,因为null不应该引起问题。数据库空值由
DBNull.Value
表示,并对其调用
ToString
,应返回空的
字符串。这意味着如果该字段为空,则
row.Item(“Extension”).ToString()
应该已经返回空的
String
。顺便说一下,该结构太大了。这应该是一门课。类或结构,您还应该使用属性而不是字段,并且不要使用像
\u name
这样的名称而不是
name
。这里有一个类似的问题:您看到了什么异常,因为null不应该在那里引起问题。数据库空值由
DBNull.Value
表示,并对其调用
ToString
,应返回空的
字符串。这意味着如果该字段为空,则
row.Item(“Extension”).ToString()
应该已经返回空的
String
。顺便说一下,该结构太大了。这应该是一门课。类或结构,您还应该使用属性而不是字段,并且不要使用像
\u name
这样的名称而不是
name