如何使用VB.NET中的自定义消息而不是默认异常msg

时间:2015-12-10 07:27:00

标签: sql-server vb.net try-catch

我知道我需要首先在SQL server DB中搜索主键值然后如果值已经显示msg该记录已经存在,则在DB中插入记录 它会是这样的:

Dim matchISBN As string = txtISBN.Text

        da = New SqlDataAdapter("SELECT isbn WHERE isbn =" & matchISBN, cn)
        dt = New DataTable
        da.Fill(dt)
        If isbn = matchISBN Then messagebox.show("record already exist")
        else ....

插入记录

我可以使用try& amp;抓住但尝试& catch将显示默认消息我希望我自己的自定义消息仅用于重复的主键值! 我不知道如何编码,任何帮助都将不胜感激!

1 个答案:

答案 0 :(得分:0)

这样做(IMO,您无需使用Try-Catch来处理它),

    Dim cmd As New SqlCommand
    Dim retval As Object
    With cmd
        .Connection = GenConnection()
        .CommandText = "select id from gmtab08 where id=1440"
        retval = .ExecuteScalar
    End With
    If retval Is Nothing Then
        MsgBox("Not Exists")
    Else
        MsgBox("Exists")
    End If

或者

        If dt.Rows.Count > 0 Then
            MsgBox("Exists")
        Else
            /*'Your code*/
        End If