请问VB.NET中如何判断更新成功呢?
Dim conn2 As New SqlClient.SqlConnection("data source=127.0.0.1;initial catalog=Mould;persist security info=False;user id=test;password=test123")
Dim updatecmd As String = "update table set qty=qty+1 where id=" & nid Dim cmd2 As New SqlClient.SqlCommand
If conn2.State = ConnectionState.Open Then conn2.Close() conn2.Open()
Dim da2 As New SqlClient.SqlDataAdapter
da2.SelectCommand = New SqlClient.SqlCommand(updatecmd, conn2)
以上是我的VB.NET 更新代码,请问如何判断更新成功呢? 请指教,谢谢
[解决办法]
在代码中添加try catch的错误处理
- VB.NET code
try Dim conn2 As New SqlClient.SqlConnection("data source=127.0.0.1;initial catalog=Mould;persist security info=False;userid=test;password=test123") Dim updatecmd As String = "update table set qty=qty+1 where id=" & nid Dim cmd2 As New SqlClient.SqlCommand If conn2.State = ConnectionState.Open Then conn2.Close() conn2.Open() Dim da2 As New SqlClient.SqlDataAdapter da2.SelectCommand = New SqlClient.SqlCommand(updatecmd, conn2)Catch e As Exception '更新失败 MessageBox("更新失败", MsgBoxStyle.Information, SystemInfo.SYSTEM_NAME)Finally '更新成功 MessageBox("更新成功", MsgBoxStyle.Information, SystemInfo.SYSTEM_NAME) conn2.Close() End Try
[解决办法]
楼上朋友写的不是很对,Finally中的语句更新失败也会执行的,应该把这个信息写在Catch上面