3

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
4

2 回答 2

5

To execute multiple commands within the same transaction, ensure that you assign the transaction object to each command individually:

Dim selectCmd As MySqlCommand = con.CreateCommand()
Dim insertCmd As MySqlCommand = con.CreateCommand()

selectCmd.CommandText = "SELECT ..."
insertCmd.CommandText = "INSERT ..."

Dim sqlTran As MySqlTransaction = con.BeginTransaction()
Try
  selectCmd.Transaction = sqlTran
  insertCmd.Transaction = sqlTran

  ...selectCmd.ExecuteScalar()...
  ...insertCmd.ExecuteNonQuery()...

  sqlTran.Commit()
Catch
  sqlTran.Rollback()
End Try

As others have mentioned, it is generally a good idea to Dispose() objects (that are IDisposable) as soon as you're done working with them. After disposing objects, they can no longer be used.

于 2013-01-10T10:42:38.893 回答
1

You can use Using keyword for auto dispose objects. i don't know VB but I know C#. Please convert the code into VB.

using(MySqlConnection con= new MySqlConnection("connectionString"))
{
    con.Open();
    using(MysqlTransaction trans=con.BeginTransaction())
    {
        try
        {
            //command to executive query
            using(MysqlCommand cmd= new MySqlCommand("query", con, trans))
            {
                cmd.Parameters.AddWithValue("@parameter1", parametervalue1);
                cmd.Parameters.AddWithValue("@parameter2", parametervalue2);
                cmd.ExecutenonQuery();
                cmd.Parameters.Clear();
            }
            //command to execute query
            using(MysqlCommand cmd= new MySqlCommand("query", con, trans))
            {
                cmd.Parameters.AddWithValue("@parameter1", parametervalue1);
                cmd.Parameters.AddWithValue("@parameter2", parametervalue2);
                cmd.ExecutenonQuery();
                cmd.Parameters.Clear();
            }
            //command to execute query
            using(MysqlCommand cmd= new MySqlCommand("query", con, trans))
            {
                cmd.Parameters.AddWithValue("@parameter1", parametervalue1);
                cmd.Parameters.AddWithValue("@parameter2", parametervalue2);
                cmd.ExecutenonQuery();
                cmd.Parameters.Clear();
            }
            trans.Commit();
        }
        catch(Exception ex)
        {
            trans.Rollback();
        }
    }
}
于 2015-05-29T10:01:58.227 回答