【发布时间】: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