executescalar:对象不能从dbnull强制转换为其他类型

k97glaaz  于 2021-06-21  发布在  Mysql
关注(0)|答案(2)|浏览(299)

我遇到一个问题,在运行程序时没有数据/值被提取,我得到以下错误:
对象不能从dbnull转换为其他类型。
我试着用if验证它 dr.hasrow() 但我还是会犯错。

drqry = "SELECT max(num) FROM tbCVinfo WHERE cvno LIKE '%" & cvstr & "%'"

cmd2.CommandText = drqry
cmd2.Connection = con

Dim result As String = ""

cv = Convert.ToInt32(cmd2.ExecuteScalar())
'''''''''in this section I'm getting the error.

cv = cv + 1
If cv >= 0 And cv <= 9 Then
  ...............
Else
    cn = "0001"
End If
cn = result

cvno = cn
whlutmcx

whlutmcx1#

解决这个问题的一个简单方法是检查返回值是否正确 DBNull :

Dim cv As Integer
Dim queryResult = cmd2.ExecuteScalar()
If IsDBNull(queryResult) Then
    ' No matching records. Do something about it.
Else
    cv = DirectCast(queryResult, Integer)
End If
5anewei6

5anewei62#

你能为c执行官写一封信吗

SqlCommand smd = new SqlCommand("Select Min(GidenEvrakSira) FROM GidenEvrak Where UserID is null", con);

minum = Convert.ToInt32(smd.ExecuteScalar());

相关问题