首页 \ 问答 \ mysql事务(提交和回滚)(mysql transaction (commit and rollback))

mysql事务(提交和回滚)(mysql transaction (commit and rollback))

下面是我用于MySqlTransaction的代码,它运行得很完美....但问题是因为我使用的是mysql命令的单个实例,我必须在其中使用唯一的PARAMETER。 这毁了我的查询。 有没有其他方法可以做....

我尝试在每次查询后处置cmd但没有用.. :(


          con.Open()
            Dim sqlTran As MySqlTransaction = con.BeginTransaction()
            Dim cmd As MySqlCommand = con.CreateCommand()
            cmd.Transaction = sqlTran
            Dim str As String = Nothing
            Try
                cmd.CommandText = "SELECT myid FROM memaster where    Adate=@adate and ANo=@ano and ASource=@asrc"
                cmd.Parameters.AddWithValue("@adate", txt_bpass_adate.Text)
                cmd.Parameters.AddWithValue("@ano", txt_bpass_af.Text)
                cmd.Parameters.AddWithValue("@asrc", txt_bpass_asource.Text)
                str = cmd.ExecuteScalar()
                'cmd.Dispose()'
                If str Is Nothing Then

                    cmd.CommandText = "Insert into memaster (ADate,ANo,ASource) values (@aDate,@aNo,@aSRC)"
                    cmd.Parameters.AddWithValue("@aDate", txt_bpass_adate.Text)
                    cmd.Parameters.AddWithValue("@aNo", txt_bpass_af.Text)
                    cmd.Parameters.AddWithValue("@aSRC", txt_bpass_asource.Text)

     cmd.ExecuteNonQuery()
    End If
     sqlTran.Commit()

            Catch ex As Exception
                Try
                    sqlTran.Rollback()
                Catch ex1 As Exception

                End Try
            End Try

我实际上想在单个事务中触发超过4个查询,这样如果出现任何问题我可以回滚它...

如果有人有任何其他方法,请与我分享这个概念...


     For index As Integer = 1 To 5
                    cmd.CommandText = "Insert into detail (ID,BNos,SNo) values (@FID1,@BNo,@SeqN1)"
                    cmd.Parameters.AddWithValue("@FID1", str)
                    cmd.Parameters.AddWithValue("@BNo", str1)
                    cmd.Parameters.AddWithValue("@SeqN1", txt_bpass_sqn1.Text)
                    cmd.ExecuteNonQuery()


                Next

below is the code i am using for MySqlTransaction and it is running perfectly.... but problem is as i am using single instance of mysqlcommand i have to use unique PARAMETER in it. which is ruining my query. is there any other way around to do....

i tried to dispose cmd after each query but of no use.. :(


          con.Open()
            Dim sqlTran As MySqlTransaction = con.BeginTransaction()
            Dim cmd As MySqlCommand = con.CreateCommand()
            cmd.Transaction = sqlTran
            Dim str As String = Nothing
            Try
                cmd.CommandText = "SELECT myid FROM memaster where    Adate=@adate and ANo=@ano and ASource=@asrc"
                cmd.Parameters.AddWithValue("@adate", txt_bpass_adate.Text)
                cmd.Parameters.AddWithValue("@ano", txt_bpass_af.Text)
                cmd.Parameters.AddWithValue("@asrc", txt_bpass_asource.Text)
                str = cmd.ExecuteScalar()
                'cmd.Dispose()'
                If str Is Nothing Then

                    cmd.CommandText = "Insert into memaster (ADate,ANo,ASource) values (@aDate,@aNo,@aSRC)"
                    cmd.Parameters.AddWithValue("@aDate", txt_bpass_adate.Text)
                    cmd.Parameters.AddWithValue("@aNo", txt_bpass_af.Text)
                    cmd.Parameters.AddWithValue("@aSRC", txt_bpass_asource.Text)

     cmd.ExecuteNonQuery()
    End If
     sqlTran.Commit()

            Catch ex As Exception
                Try
                    sqlTran.Rollback()
                Catch ex1 As Exception

                End Try
            End Try

i actually want to fire more then 4 queries in single transaction so that if anything go wrong i can rollback it...

if any onebody have any other method of it kindly share the concept wid me...


     For index As Integer = 1 To 5
                    cmd.CommandText = "Insert into detail (ID,BNos,SNo) values (@FID1,@BNo,@SeqN1)"
                    cmd.Parameters.AddWithValue("@FID1", str)
                    cmd.Parameters.AddWithValue("@BNo", str1)
                    cmd.Parameters.AddWithValue("@SeqN1", txt_bpass_sqn1.Text)
                    cmd.ExecuteNonQuery()


                Next

原文:https://stackoverflow.com/questions/14252560
更新时间:2022-04-24 21:04

最满意答案

在这里,我将向您展示如何使用DNN模块创建动态页面。 有关更多信息,请参阅此链接

  1. 首先在Visual Studio中打开您的DNN网站。 您可以在根目录中看到DesktopModules文件夹,右键单击该目录并添加新目录,将其命名为模块名称,例如“CountryState”。 现在右键单击CountryState文件夹并添加 - >添加新项目 - >选择Web用户控件并将其命名为模块名称,如“CountryState.ascx”,它将显示CountryState.ascx和CountryState.ascx.cs文件在DesktopModules / CountryState文件夹中。
  2. 下一步将继承类“System.Web.UI.UserControl”更新为“PortalModuleBase”。 这是因为PortalModuleBase类定义了Portal中所有桌面门户模块继承的自定义基类。 PortalModuleBase类定义门户网站框架用于正确显示门户网站模块的门户网站特定属性。
  3. 现在使用您的主机凭据登录您的网站。
  4. 在DNN面板中,转到Host-> Extensions。
  5. 单击“扩展页面”右上角的“ 创建新模块”按钮。
  6. Create New Module按钮用Create Module Form DropDownList打开一个弹出窗口。 从该下拉列表中选择“ 控制 ”。
  7. 它将显示表单,从中作为尊重的文件夹路径选择您的.ascx Web用户控件,然后单击“ 创建模块”按钮。
  8. 最后在页面上使用Drag-N-Drop模块。
  9. 请享用! ;)

Here I show you how to create dynamic pages using DNN module. For more information refer this link.

  1. First of all open your DNN website in Visual Studio. You can see the DesktopModules folder in your root directory, right click on the directory and add new directory give it to name as your module name, for example "CountryState". Now right click on the CountryState folder and Add->Add New Item->Select Web User Control and give it to name as your module name like "CountryState.ascx", it will show you CountryState.ascx and CountryState.ascx.cs files in DesktopModules/CountryState folder.
  2. Next step to update inherit class "System.Web.UI.UserControl" to "PortalModuleBase". This is because of the PortalModuleBase class defines a custom base class inherited by all desktop portal modules within the Portal. The PortalModuleBase class defines portal specific properties that are used by the portal framework to correctly display portal module.
  3. Now logged-In in your website using your Host credential.
  4. In DNN panel, go-to Host->Extensions.
  5. Click on Create New Module button on right top corner of the Extensions Page.
  6. Create New Module button open a popup window with Create Module Form DropDownList. Select Control from that Drop-Down.
  7. It will show you form, select your .ascx web user control in that from as respected folder path and click on Create Module button.
  8. Finally Drag-N-Drop module on your page.
  9. Enjoy! ;)

相关问答

更多