请问这段VB代码中的intTrans = m_clsData.Connection.BeginTrans是什么意思?能否给说一下这个方法Reg是干什么用的?

C罗子 2016-11-29 04:09:18
由于需要将老版本的VB代码重新改造成C#代码,因此需要理清里面老系统VB代码的意思,而本人没有接触过VB,因此想请各位VB专家看一下这段代码的意思,If intTrans > 0 Then m_clsData.Connection.RollbackTrans这2据是什么意思?怎么知道 intTrans = m_clsData.Connection.BeginTrans后intTrans的值是多少?m_clsData.Connection.BeginTrans是什么意思?
请各位给方法Reg加些简单的注释吧,先谢谢各位了!
Public Function Reg(flg As Integer) As Boolean
Dim rs As ADODB.Recordset
Dim intTrans As Integer
Dim m_rs As ADODB.Recordset
On Error GoTo Err_Handler
Registry = False

Set rs = m_rsDirect.Clone
Set m_rs = m_rsDirect.Clone(adLockReadOnly)
intTrans = m_clsData.Connection.BeginTrans
If flg = 1 Then

Call m_clsData.DeleteProductionHavingData 'm_clsData As clsPCDatabase
Do Until m_rs.EOF = True
If m_clsData.Correct1(txtBin.Text, txtProdCode.Text, m_rs) = False Then
If intTrans > 0 Then
m_clsData.Connection.RollbackTrans
End If
Exit Function
End If
m_rs.MoveNext
Loop
Call ReleaseRecordset(m_rs)
Else
If m_clsData.Correct2(txtBin.Text, txtProdCode.Text, rs) = False Then
If intTrans > 0 Then
m_clsData.Connection.RollbackTrans
End If
Exit Function
End If

End If
If intTrans > 0 Then
m_clsData.Connection.CommitTrans
End If

Registry = True

Exit Function
Err_Handler:
Dim lngErr As Long
Dim strErr As String

lngErr = Err.Number
strErr = Err.Description

If intTrans > 0 Then
m_clsData.Connection.RollbackTrans
End If

Call RaiseError(lngErr, TypeName(Me) & "::Reg", strErr)
End Function 'Reg



Reg方法调用的Correct1和方法Correct2代码很相似,这里我列出方法Correct2的代码,代码为:
Public Function Correct2(ByVal intBin As Integer, _
ByVal strProdCode As String, _
ByRef rs As ADODB.Recordset) As Boolean
Dim strSQL As String

On Error GoTo Err_Handler

Correct2 = False

With rs

strSQL = ""
strSQL = strSQL & "EXEC Correct2 " & vbCrLf
strSQL = strSQL & " @intBin =" & intBin & vbCrLf
strSQL = strSQL & " ,@intBranch =" & .Fields("CD").Value & vbCrLf
strSQL = strSQL & " ,@strProdCode = '" & strProdCode & "'" & vbCrLf
strSQL = strSQL & " ,@intDirectNum =" & Nz(.Fields("数量").Value, 0) & vbCrLf
strSQL = strSQL & " ,@fltWeight =" & Nz(.Fields("重量").Value, 0) & vbCrLf
If IsNull(.Fields("数量").Value) And IsNull(.Fields("重量").Value) Then
strSQL = strSQL & " ,@bytDeleteFlg = 1" & vbCrLf
End If
End With

Call ExecuteSQL(m_cn, strSQL)

Correct2 = True

Exit Function

Err_Handler:
Call RaiseError(Err.Number, TypeName(Me) & "::Correct2", Err.Description)
End Function 'Correct2
...全文
202 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
Tiger_Zhao 2016-11-30
  • 打赏
  • 举报
回复
这样写没问题。
C罗子 2016-11-30
  • 打赏
  • 举报
回复
引用 3 楼 Tiger_Zhao 的回复:
如果执行 Correct2() 失败 就回滚事务 Function Reg() 中 Err_Handler: 以前的 If intTrans > 0 Then 都是废话! 调用 BeginTrans 之后 intTrans 肯定非零的。
您好,由于这段VB代码要换成C#代码,因为存储过程中是没有错误处理的,我可否在代码块try{}catch{}中写这个回滚达到与原有的VB代码一样的逻辑?想写成这样是否可行? try { // 利用sqlcommand进行数据操作 ... 调用Correct2()方法; // 成功提交 sqlTransaction.Commit(); } catch(Exception ex) { // 出错回滚 sqlTransaction.Rollback(); }
Tiger_Zhao 2016-11-29
  • 打赏
  • 举报
回复
如果执行 Correct2() 失败
就回滚事务

Function Reg() 中 Err_Handler: 以前的 If intTrans > 0 Then 都是废话!
调用 BeginTrans 之后 intTrans 肯定非零的。
C罗子 2016-11-29
  • 打赏
  • 举报
回复
引用 1 楼 Tiger_Zhao 的回复:
BeginTrans 启动事务,返回的是当前事务的层次(第一层 1、第二层 2……) CommitTrans 递交最近一层事务 RollbackTrans 回滚最近一层事务 intTrans 关键是在错误分支 Err_Handler: 下 如果 BeginTrans 出错或之前就出错,intTrans 就是初始的 0,表示本层事务未启动,不需要调用 RollbackTrans。 否则只要 BeginTrans 成功,intTrans 就非 0,出错时需要调用 RollbackTrans。
您好我这3句代码加的注释您看看对吗?不对的话您能否给注释一下这3据话?: If m_clsData.Correct2(txtBin.Text, txtProdCode.Text, rs) = False Then--注释:如果调用Correct2的结果为False,也就是Correct2进入Err_Handler:。 If intTrans > 0 Then--注释:这句什么意思,有事务的意思吗? m_clsData.Connection.RollbackTrans--注释:回滚事务 If intTrans > 0 Then--注释:这句什么意思?
Tiger_Zhao 2016-11-29
  • 打赏
  • 举报
回复
BeginTrans 启动事务,返回的是当前事务的层次(第一层 1、第二层 2……)
CommitTrans 递交最近一层事务
RollbackTrans 回滚最近一层事务

intTrans 关键是在错误分支 Err_Handler: 下
如果 BeginTrans 出错或之前就出错,intTrans 就是初始的 0,表示本层事务未启动,不需要调用 RollbackTrans。
否则只要 BeginTrans 成功,intTrans 就非 0,出错时需要调用 RollbackTrans。

7,763

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧