| |

VerySource

 Forgot password?
 Register
Search
View: 902|Reply: 9

About transaction processing

[Copy link]

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-1-21 14:40:01
| Show all posts |Read mode
I set a function for transaction processing, I do not know how to keep reporting this error, please which heroes correct me,
public bool updateDataBaseWithTran (string tempstr, string connstr)
{
this.Conn = new SqlConnection (connstr);
Conn.Open ();
SqlTransaction tx = Conn.BeginTransaction ();

SqlCommand tempCmd = new SqlCommand ();
tempCmd.Connection = Conn;
tempCmd.CommandType = CommandType.Text;
tempCmd.CommandText = tempstr;
try
{
     tempCmd.ExecuteNonQuery ();
     tx.Commit ();
     Conn.Close ();
     return true;
}
catch (Exception ex)
{
tx.Rollback ();
MessageBox.Show (ex.Message);
return false;
}
}

The error is:
When a connection assigned to a local is in a pending local transaction, the command must have a transaction object to execute. The TRansaction attribute of this command has not been initialized.
Reply

Use magic Report

1

Threads

16

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 China

Post time: 2020-2-1 07:45:01
| Show all posts
SqlTransaction tx = Conn.BeginTransaction ();

SqlCommand tempCmd = new SqlCommand ();

Change to
SqlCommand tempCmd = new SqlCommand ();
SqlTransaction tx = Conn.BeginTransaction ();
Reply

Use magic Report

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

 Author| Post time: 2020-2-1 15:27:01
| Show all posts
No, it still reports the same error!
Reply

Use magic Report

0

Threads

4

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-2-2 14:45:02
| Show all posts
Modify the program:
SqlTransaction tx = null;
using (tx = Conn.BeginTransaction ())
{
   SqlCommand tempCmd = new SqlCommand ();
tempCmd.Connection = Conn;
tempCmd.CommandType = CommandType.Text;
tempCmd.CommandText = tempstr;
try
{
     tempCmd.ExecuteNonQuery ();
     tx.Commit ();
     Conn.Close ();
     return true;
}
catch (Exception ex)
{
tx.Rollback ();
MessageBox.Show (ex.Message);
return false;
}
}
Reply

Use magic Report

0

Threads

32

Posts

22.00

Credits

Newbie

Rank: 1

Credits
22.00

 China

Post time: 2020-2-4 12:45:02
| Show all posts
Don't you tell me the error message?
SqlCommand.Transaction is not set
Reply

Use magic Report

0

Threads

32

Posts

22.00

Credits

Newbie

Rank: 1

Credits
22.00

 China

Post time: 2020-2-4 15:30:01
| Show all posts
tempCmd.Connection = Conn;
tempCmd.Transaction = Conn.BeingTransaction ();
Reply

Use magic Report

0

Threads

15

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 China

Post time: 2020-2-14 18:45:01
| Show all posts
using System.Transaction;

xxxTableAdapter xxxTA = new xxxTableAdapter ();

using (TransactionScope ts = new TransactionScope)
{
     xxxTA.Insert ("aa", "bb", "cc");
     xxxTA.Update ("dd", "ee", "mm", "cc");

     
     ts.Complete (); // Complete the transaction, write on success, or roll back automatically

}
Switch to .net 2.0. Convenient
Reply

Use magic Report

1

Threads

16

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 China

Post time: 2020-2-17 22:15:01
| Show all posts
Khan, only to discover that the landlord you forgot a key thing, your CommandText is not a query operation?
Need to add a sentence:
tempCmd.Transaction = tx;
I'm sure right now
Reply

Use magic Report

1

Threads

16

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 China

Post time: 2020-2-17 23:45:01
| Show all posts
Because your connection is already occupied by a transaction, all non-query operations should be performed with this transaction
Reply

Use magic Report

0

Threads

110

Posts

63.00

Credits

Newbie

Rank: 1

Credits
63.00

 United States

Post time: 2020-3-1 19:15:01
| Show all posts
SqlTransaction tx = Conn.BeginTransaction ();

SqlCommand tempCmd = new SqlCommand ();
tempCmd.Connection = Conn;
tempCmd.CommandType = CommandType.Text;
tempCmd.CommandText = tempstr;
// Add this sentence ..
tempCmd.Transaction = tx;
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list