jap*_*ino -1 sql vb.net null if-statement iif
我不清楚,所以如果有人可以详细解释 VB.Net 的两个函数(IsDBNull和String.IsNullOrEmpty)有什么区别。
下面是我问这个问题的场景,Company我的表中有一列,其值为NULL,然后我使用IIFvb.net 的函数来验证它是否然后NULL分配空字符串(“”),否则分配来自数据表的值
设想:
下面是使用String.IsNullOrEmpty,我收到转换错误:
从类型“DBNULL”到“String”的转换无效。
txtCompany.Text = IIf(String.IsNullOrEmpty(dtSample.Rows(grdInfo.SelectedIndex).Item("Company")).ToString, "", dtSample.Rows(grdInfo.SelectedIndex).Item("Company"))
Run Code Online (Sandbox Code Playgroud)但是,当我用IsDBNull替换 String.IsNullOrEmpty 时,验证工作正常。
txtCompany.Text = IIf(IsDBNull(dtSample.Rows(grdInfo.SelectedIndex).Item("Company")).ToString, "", dtSample.Rows(grdInfo.SelectedIndex).Item("Company"))
Run Code Online (Sandbox Code Playgroud)编辑:
这很令人困惑,因为如果我IF ELSE使用 String.IsNullOrEmpty 进行使用条件验证(请参阅下面的示例代码),它就可以正常工作。
If String.IsNullOrEmpty(dtSample.Rows(grdInfo.SelectedIndex).Item("Company").ToString) = True Then
txtCompany.Text = ""
Else
txtCompany.Text = dtSample.Rows(grdInfo.SelectedIndex).Item("Company").ToString
End If
Run Code Online (Sandbox Code Playgroud)
令人困惑的部分是当我使用IIF(String.IsNullOrEmpty...etc)它时它返回一个错误。但当我使用正常时IF String.IsNullOrEmpty(dtSample.Rows....etc) = True它工作正常。
任何解释将不胜感激。谢谢
长话短说
String.IsNullOrEmpty()仅检查空([blank]即''或"")或不检查的Null值,如果来自数据库的任何字段具有其值 DBNull,则会引发错误。DBNullIsDBNull()检查DBNull(与 不同Null).ToString将转换DBNull为空字符串,即''详细信息
考虑以下 SQL 表示例(使用 SQL Server 作为基础)
表结构:
Column_Name Type and Size Other Properties
---------------- ------------- ----------------------
Company_ID int IDENTITY(1,1) NOT NULL
Company_Name nvarchar (50) NOT NULL
Company_Address nvarchar (50) NULL
Run Code Online (Sandbox Code Playgroud)
插入语句:
INSERT [tbl_company] ([Company_Name], [Company_Address]) VALUES ('ABC', 'QWERT')
INSERT [tbl_company] ([Company_Name], [Company_Address]) VALUES ('ASD', ' ')
INSERT [tbl_company] ([Company_Name], [Company_Address]) VALUES ('XYZ', '')
INSERT [tbl_company] ([Company_Name]) VALUES ('PQR')
Run Code Online (Sandbox Code Playgroud)
表数据:
Company_ID Company_Name Company_Address
----------- ---------------- ---------------
1 ABC QWERT
2 ASD [SPACE]
3 XYZ [BLANK]
4 PQR NULL
Run Code Online (Sandbox Code Playgroud)
使用 SqlDataReader (r) 通过 IsNullOrEmpty() 和 IsDBNull() 测试 Company_Address:
Company_ID IsNullOrEmpty(r("Company_Address")) IsDBNull(r("Company_Address"))
---------- ----------------------------------- ------------------------------
1 False False
2 False False
3 True False
4 ERROR True
Run Code Online (Sandbox Code Playgroud)
现在具体到
OP在这里尝试的问题,让我们一一考虑所有陈述
带 IsNullOrEmpty 的 IIF 语句(错误)
txtCompany.Text = IIf(String.IsNullOrEmpty(dtSample.Rows(grdInfo.SelectedIndex).Item("Company")).ToString, "", dtSample.Rows(grdInfo.SelectedIndex).Item("Company"))
Run Code Online (Sandbox Code Playgroud)
在此语句中,OP 访问值 asdtSample.Rows(grdInfo.SelectedIndex).Item("Company")并检查它,然后使用ieIsNullOrEmpty()将 IsNullOrEmpty 的结果转换为字符串。如果值为 DBNull,它将始终返回错误。正确的使用方法是.ToStringIsNullOrEmpty(value).ToString()
IsNullOrEmpty(dtSample.Rows(grdInfo.SelectedIndex).Item("Company").ToString)
Run Code Online (Sandbox Code Playgroud)
请参阅最后一部分Company")).ToStringvs Company").ToString),只是一个错误位置的情况“)”
OP 的第二个(IIF 与 IsDBNull)和第三个(IF 与 IsNullOrEmpty)语句是正确的
txtCompany.Text = IIf(IsDBNull(dtSample.Rows(grdInfo.SelectedIndex).Item("Company")).ToString, "", dtSample.Rows(grdInfo.SelectedIndex).Item("Company"))
If String.IsNullOrEmpty(dtSample.Rows(grdInfo.SelectedIndex).Item("Company").ToString) = True Then
txtCompany.Text = ""
Else
txtCompany.Text = dtSample.Rows(grdInfo.SelectedIndex).Item("Company").ToString
End If
Run Code Online (Sandbox Code Playgroud)
至于第二个语句,OP 正确排序了参数,即第一个Company字段使用 转换为字符串dtSample.Rows(grdInfo.SelectedIndex).Item("Company").ToString。这会将任何 DBNull 转换为空字符串,然后检查IsNullOrEmpty. 现在,由于该值已转换为 EmptyString,因此不会给出任何错误。
与OP的旧讨论
您的文字中的差异很明显。你的第一个声明是:
txtCompany.Text = IIf(String.IsNullOrEmpty(dtSample.Rows(grdInfo.SelectedIndex).Item("Company")).ToString, "", dtSpecifierRebate.Rows(grdInfo.SelectedIndex).Item("Company"))
Run Code Online (Sandbox Code Playgroud)
第二个是
If String.IsNullOrEmpty(dtSample.Rows(grdInfo.SelectedIndex).Item("Company").ToString) = True Then
Run Code Online (Sandbox Code Playgroud)
现在将它们分开,第一个语句(IIF)
String.IsNullOrEmpty(dtSample.Rows(grdInfo.SelectedIndex).Item("Company")).ToString
'Item("Company")).ToString
Run Code Online (Sandbox Code Playgroud)
第二部分(IF)
String.IsNullOrEmpty(dtSample.Rows(grdInfo.SelectedIndex).Item("Company").ToString)
'Item("Company").ToString)
Run Code Online (Sandbox Code Playgroud)
发现有什么不同吗?
在第一个语句中,您将IsNullOrEmpty 的结果转换为 String
在第二个语句中,您将.Item("Company") ToString 转换,然后进行比较。
如果.Item("Company")返回 DBNull,
则 IsNullOrEmpty 失败,因为 .Item("Company") 返回类型DBNull,而 IsNullOrEmpty 检查null
IsDBNull 有效,因为它检查DBNull
所有点都放错了括号“)”这是一个错字
关于这些语句的使用:
If 和 IIF 需要将结果检查为布尔值而不是字符串
最好删除语句的 ToString 部分
| 归档时间: |
|
| 查看次数: |
11097 次 |
| 最近记录: |