vb中sql字符串yes或no到boolean的转换

fcipmucu  于 2021-07-29  发布在  Java
关注(0)|答案(2)|浏览(350)

我想把txtqualified的字符串转换成boolean,在数据库中它被设置为bit,但是它不起作用,错误“string was not recognized has a valid boolean”,我该怎么办?

Sub verify()

        If aggregate >= CutOffPoint.txtMaleCut.Text AndAlso cmbGender.SelectedItem = "Male" Then
            MsgBox("Sorry, You do not Qualify to Offer the Program")
            txtQualified.Text = "No"
            Convert.ToBoolean(txtQualified.Text)
        ElseIf aggregate >= CutOffPoint.txtFemaleCut.Text AndAlso cmbGender.SelectedItem = "Female" Then
            MsgBox("Sorry, You do not Qualify to Offer the Program")
            txtQualified.Text = "No"
            Convert.ToBoolean(txtQualified.Text)
        ElseIf aggregate < less Then
            MsgBox("Sorry, Invalid Entry, Please Entry all Provided Grades")
        Else
            MsgBox("Congratulation, You have Qualified")
            txtQualified.Text = "Yes"
            Convert.ToBoolean(txtQualified.Text)

        End If

    End Sub
wribegjk

wribegjk1#

尝试将此函数添加到项目中:

Public function ConvertTextToBoolean(InputStr as String) As Boolean

if InputStr = "No" Then
   Return False
end if

if InputStr = "Yes" Then
   Return True
end if

if not InputStr = "Yes" and not InputStr = "No" Then
   MsgBox("Invalid String")
end if

end function

但我不明白:你想不带“如果”就这么做吗!?

bkkx9g8r

bkkx9g8r2#

只需编写一个小函数就可以得到布尔值。如果值可能不是yes和no,则可以添加一些验证。
包括 .Add 方法为参数指定正确的数据库数据类型将使操作更安全。

Private Sub OPCode()
    Dim sql = "Insert Into YourTable (ABitField) Values (@BitField);"
    Using cn As New SqlConnection("Your connection string"),
            cmd As New SqlCommand(Sql, cn)
        cmd.Parameters.Add("@BitField", SqlDbType.Bit).Value = GetBoolean(txtQualified.Text)
        cn.Open()
        cmd.ExecuteNonQuery()
    End Using
End Sub

Private Function GetBoolean(s As String) As Boolean
    Return s = "Yes"
    Return False
End Function

相关问题