Skip to content

Commit a291580

Browse files
committed
Switch to using declarations in code examples.
1 parent 637d876 commit a291580

File tree

2 files changed

+34
-42
lines changed

2 files changed

+34
-42
lines changed

docs/content/troubleshooting/transaction-usage.md

Lines changed: 28 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
lastmod: 2019-07-30
2+
lastmod: 2021-09-21
33
date: 2018-09-29
44
title: Transaction Usage
55
customtitle: "Fix: The transaction associated with this command is not the connection’s active transaction"
@@ -30,42 +30,38 @@ To easily migrate code from Connector/NET, use the `IgnoreCommandTransaction=tru
3030
### ADO.NET example
3131

3232
```csharp
33-
using (var connection = new MySqlConnection(...))
34-
{
35-
connection.Open();
36-
using (var transaction = connection.BeginTransaction())
37-
using (var command = connection.CreateCommand())
38-
{
39-
command.CommandText = "SELECT ...";
40-
41-
// *** ADD THIS LINE ***
42-
command.Transaction = transaction;
43-
44-
// otherwise, this will throw System.InvalidOperationException: The transaction associated with this command is not the connection's active transaction.
45-
command.ExecuteScalar();
46-
47-
transaction.Commit();
48-
}
49-
}
33+
using var connection = new MySqlConnection(...);
34+
connection.Open();
35+
36+
using var transaction = connection.BeginTransaction();
37+
using var command = connection.CreateCommand();
38+
command.CommandText = "SELECT ...";
39+
40+
// *** ADD THIS LINE ***
41+
command.Transaction = transaction;
42+
43+
// otherwise, this will throw System.InvalidOperationException: The transaction associated with this command is not the connection's active transaction.
44+
command.ExecuteScalar();
45+
46+
// ... remaining code
47+
transaction.Commit();
5048
```
5149

5250
### Dapper Example
5351

5452
```csharp
55-
using (var connection = new MySqlConnection(...))
56-
{
57-
connection.Open();
58-
using (var transaction = connection.BeginTransaction())
59-
{
60-
// this will throw System.InvalidOperationException: The transaction associated with this command is not the connection's active transaction.
61-
connection.Query("SELECT ...");
62-
63-
// use this instead:
64-
connection.Query("SELECT ...", transaction: transaction);
65-
66-
transaction.Commit();
67-
}
68-
}
53+
using var connection = new MySqlConnection(...);
54+
connection.Open();
55+
using var transaction = connection.BeginTransaction();
56+
57+
// this will throw System.InvalidOperationException: The transaction associated with this command is not the connection's active transaction.
58+
connection.Query("SELECT ...");
59+
60+
// use this instead:
61+
connection.Query("SELECT ...", transaction: transaction);
62+
63+
// ... remaining code
64+
transaction.Commit();
6965
```
7066

7167
## Further Reading

docs/content/tutorials/best-practices.md

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
lastmod: 2021-06-07
2+
lastmod: 2021-09-21
33
date: 2016-10-16
44
menu:
55
main:
@@ -160,15 +160,11 @@ namespace MySqlConnector.Examples
160160
{
161161
public static async Task SleepOne()
162162
{
163-
using (var db = new AppDb())
164-
{
165-
await db.Connection.OpenAsync();
166-
using (var cmd = db.Connection.CreateCommand())
167-
{
168-
cmd.CommandText = @"SELECT SLEEP(1)";
169-
await cmd.ExecuteNonQueryAsync();
170-
}
171-
}
163+
using var db = new AppDb();
164+
await db.Connection.OpenAsync();
165+
using var cmd = db.Connection.CreateCommand();
166+
cmd.CommandText = @"SELECT SLEEP(1)";
167+
await cmd.ExecuteNonQueryAsync();
172168
}
173169
}
174170
}

0 commit comments

Comments
 (0)