以文本方式查看主题

-  Foxtable(狐表)  (http://www.foxtable.com/bbs/index.asp)
--  专家坐堂  (http://www.foxtable.com/bbs/list.asp?boardid=2)
----  请教:逐行写入数据库,这样使用数据库事务妥否?  (http://www.foxtable.com/bbs/dispbbs.asp?boardid=2&id=73992)

--  作者:bahamute
--  发布时间:2015/8/31 9:19:00
--  请教:逐行写入数据库,这样使用数据库事务妥否?

For Each r As Row In t.Rows

        GH=r("工号")

        XM=r("姓名")

Try

Connections("salaryMS").BeginTransaction() \'开始事务

Dim cmd As New SQLCommand

      Dim dt As DataTable

      cmd.C

      cmd.CommandText = "Insert Into {报盘记录} ([工号],[姓名]) Values (\'" & GH & "\',\'" & XM & "\')"

      cmd.ExecuteNonQuery

      Connections("salaryMS").Commit \'提交事务,所有操作生效

Catch ex As Exception \'如果出错

     Connections("salaryMS").Rollback() \'回滚事务,撤销所有操作

End Try


Next


--  作者:狐表开发
--  发布时间:2015/8/31 9:36:00
--  
Try
    Connections("salaryMS").BeginTransaction() \'开始事务
    Dim dt As DataTable
    Dim cmd As New SQLCommand
    cmd.C
    For Each r As Row In t.Rows
        GH=r("工号")
        XM=r("姓名")
        cmd.CommandText = "Insert Into {报盘记录} ([工号],[姓名]) Values (\'" & GH & "\',\'" & XM & "\')"
        cmd.ExecuteNonQuery
    Next
    Connections("salaryMS").Commit \'提交事务,所有操作生效
Catch ex As Exception \'如果出错
    Connections("salaryMS").Rollback() \'回滚事务,撤销所有操作
End Try

--  作者:大红袍
--  发布时间:2015/8/31 9:37:00
--  
Try
   
    Connections("salaryMS").BeginTransaction() \'开始事务
    For Each r As Row In t.Rows
       
        GH=r("工号")
       
        XM=r("姓名")
                       
        Dim cmd As New SQLCommand
       
        Dim dt As DataTable
       
        cmd.ConnectionName = "123456"
       
        cmd.CommandText = "Insert Into {报盘记录} ([工号],[姓名]) Values (\'" & GH & "\',\'" & XM & "\')"
       
        cmd.ExecuteNonQuery
    Next
   
    Connections("salaryMS").Commit \'提交事务,所有操作生效  
   
Catch ex As Exception \'如果出错
   
    Connections("salaryMS").Rollback() \'回滚事务,撤销所有操作
   
End Try
[此贴子已经被作者于2015/8/31 9:37:31编辑过]

--  作者:bahamute
--  发布时间:2015/8/31 9:39:00
--  
非常感谢。
刚刚接触数据库事务,还得学习呀。