注册 登录
编程论坛 SQL Server论坛

如何处理SQL查询返回空值的问题,求助!

mkb21 发布于 2008-05-13 16:30, 1094 次点击
WinFrm过程代码:
    public myItem as String    '用于返回值的公共变量
    Public Sub DatSearch(ByVal TabName As String, ByVal Field As String, ByVal Guide As String)
        CmdText = "Select " & Field & " From " & TabName & " Where " & Guide
        If Conn.State = 1 Then Conn.Close()
        Dim Cmd As SqlClient.SqlCommand = New SqlClient.SqlCommand(CmdText, Conn)
        SQLAdp.SelectCommand = New SqlClient.SqlCommand(CmdText, Conn)
        Try
            Conn.Open()
            SQLAdp.Fill(DataSet, "SearchTab")
            Dim myResp As Object = Cmd.ExecuteScalar         '判断是否存在记录
            myItem = IIf(myResp Is System.DBNull.Value, Nothing, DataSet.Tables("SearchTab").Rows(0).Item(0))
            DataSet.Tables("SearchTab").Clear()
            DataSet.Tables.Remove("SearchTab")
            Conn.Close()
        Catch ex As Exception
            MsgBox(ex.Message)
            Exit Try
        End Try
    End Sub
当查询返回空记录时,会出错:“在0处没有任何行!”
怎样才能处理出现空值或空记录的情况,谢谢高手指点!
1 回复
#2
happynight2008-05-13 17:04
发错了 应发论坛
1