【问题标题】:How do I use a try and catch block in vb.net? [closed]如何在 vb.net 中使用 try 和 catch 块? [关闭]
【发布时间】:2011-07-18 14:27:00
【问题描述】:

对于下面显示的代码,如果发生错误,我将如何使用 try/catch 块来处理错误?

        TOTKILO.Text = KILO.Text * TOUCH.Text * 0.01
        TextBox10.Text = TextBox9.Text * TextBox8.Text * 0.01
        K = Math.Round(Val(TOTKILO.Text) - Val(TextBox10.Text), 5)
        TextBox11.Text = TextBox7.Text + K

        cmd.CommandType = Data.CommandType.Text
        con.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\SHOPPROJECT\SHOPPROJECT\shop.mdf;Integrated Security=True;User Instance=True"
        con.Open()
        If RadioButton1.Checked Then
            GOLD = 1
        ElseIf RadioButton2.Checked Then
            SILVER = 1
        End If
        If RadioButton3.Checked Then
            KGOLD = 1
        ElseIf RadioButton4.Checked Then
            KSILVER = 1
        End If
        cmd.CommandText = " INSERT INTO SALES VALUES('" & ComboBox1.Text & " ' , " & SILVER & " ," & GOLD & ",'" & ComboBox2.Text & "'," & KILO.Text & ", " & TOUCH.Text & " ," & TOTKILO.Text & "," & TextBox3.Text & "," & TextBox8.Text & "," & KGOLD & "," & KSILVER & "," & TextBox9.Text & " ," & TextBox10.Text & "," & TextBox11.Text & "," & TextBox12.Text & " , " & TextBox13.Text & " ) "
        cmd.CommandType = " UPDATE BALANCE SET OBBALANCE = " & TextBox11.Text & " WHERE CUSTOMERNAME = '" & ComboBox1.Text & "' "
        cmd.Connection = con
        cmd.ExecuteNonQuery()
        con.Close()
END SUB

我使用 VB.Net 控件和 LINQ to SQL 来执行所有更新和插入,所以当发生错误时我想显示“正确输入数据”,一旦我发现异常,我该怎么做?

【问题讨论】:

  • 请你不要对每个人大喊大叫
  • 并且不要两次发布相同的问题!
  • 我试图在编辑中清理它...我能说什么我是受虐狂
  • 如果你们都打算投票结束他的一个问题,那就改成另一个。这至少做了一些可读性的尝试。

标签: sql vb.net error-handling try-catch


【解决方案1】:

我在下面添加了一个简单的 Try...Catch,但我想你会发现你有很多问题:

  • 您似乎正在对字符串对象进行乘法运算,您应该先解析出数字,然后根据需要将其转换回字符串对象。
  • 您还设置了两次 CommandType,第二次设置为更新命令而不是适当的值。
  • 您的代码对 SQL 注入开放,您不应该通过字符串连接来构建 SQL 语句,而应该使用 sqlparameters
  • 如果您要进行字符串连接,至少使用字符串生成器,但不要进行字符串连接(见上文)
  • 一次性对象(如 SQLCommand)需要放在 Using 块中
  • 不要全部大写
  • 只发布一次您的问题,如果您想添加详细信息,可以在发布后编辑问题

修改后的代码如下:

Try
    TOTKILO.Text = KILO.Text * TOUCH.Text * 0.01
    TextBox10.Text = TextBox9.Text * TextBox8.Text * 0.01
    K = Math.Round(Val(TOTKILO.Text) - Val(TextBox10.Text), 5)
    TextBox11.Text = TextBox7.Text + K
    cmd.CommandType = Data.CommandType.Text
    con.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\SHOPPROJECT\SHOPPROJECT\shop.mdf;Integrated Security=True;User Instance=True"
    con.Open()
    If RadioButton1.Checked Then
        GOLD = 1
    ElseIf RadioButton2.Checked Then
        SILVER = 1
    End If
    If RadioButton3.Checked Then
        KGOLD = 1
    ElseIf RadioButton4.Checked Then
        KSILVER = 1
    End If
    cmd.CommandText = " INSERT INTO SALES VALUES('" & ComboBox1.Text & " ' , " & SILVER & " ," & GOLD & ",'" & ComboBox2.Text & "'," & KILO.Text & ", " & TOUCH.Text & " ," & TOTKILO.Text & "," & TextBox3.Text & "," & TextBox8.Text & "," & KGOLD & "," & KSILVER & "," & TextBox9.Text & " ," & TextBox10.Text & "," & TextBox11.Text & "," & TextBox12.Text & " , " & TextBox13.Text & " ) "
    cmd.CommandType = " UPDATE BALANCE SET OBBALANCE = " & TextBox11.Text & " WHERE CUSTOMERNAME = '" & ComboBox1.Text & "' "
    cmd.Connection = con
    cmd.ExecuteNonQuery()
Catch (ex as Exception)
    MsgBox.Show("Enter Data Correctly: " & ex.toString)
Finally
    con.Close()
End Try

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-15
    • 1970-01-01
    • 1970-01-01
    • 2020-04-02
    • 2016-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多