Sql 如何在vb.net中使用try-and-catch块?
对于下面显示的代码,如果发生错误,我将如何使用try/catch块来处理错误Sql 如何在vb.net中使用try-and-catch块?,sql,vb.net,error-handling,try-catch,Sql,Vb.net,Error Handling,Try Catch,对于下面显示的代码,如果发生错误,我将如何使用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 = Text
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来执行所有更新和插入,因此当出现错误时,我想显示“正确输入数据”,一旦捕获到异常,我该如何执行该操作?我在下面添加了一个简单的Try…catch,但我想您会发现您有很多问题:
- 如果您似乎在对字符串对象执行乘法运算,则应首先解析出数字,然后根据需要将其转换回字符串对象李>
- 您还将CommandType设置两次,并在第二个实例中设置为更新命令,而不是适当的值
- 您的代码对SQL注入非常开放,您不应该通过字符串连接来构建SQL语句,而应该使用sqlparameters
- 如果要进行字符串连接,请至少使用字符串生成器,但不要进行字符串连接(参见上文)
- 一次性对象(如SQLCommand)需要放置在使用块中
- 不要用大写字母发帖
- 只发布一次问题,如果您想添加详细信息,可以在发布问题后编辑问题
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
请你不要对每一个可能重复的问题大喊大叫,也不要把同一个问题贴两次!我试着在编辑中整理一下…我能说什么我是受虐狂呢?你们要投票来结束他的一个问题,让它成为另一个问题。这本书至少尝试了一些可读性。