From 28665f7586fd822d37ef6d42d035236c623ead55 Mon Sep 17 00:00:00 2001 From: Jesper Noordsij Date: Sat, 26 Jul 2025 13:37:26 +0200 Subject: [PATCH] Use 'await using' in various doc examples Signed-off-by: Jesper Noordsij --- .../api/MySqlConnector/MySqlBatchType.md | 2 +- .../api/MySqlConnector/MySqlBulkCopyType.md | 2 +- .../api/MySqlConnector/MySqlBulkLoaderType.md | 2 +- docs/content/diagnostics/logging.md | 8 +++---- docs/content/diagnostics/metrics.md | 4 ++-- docs/content/home.md | 2 +- .../troubleshooting/connection-reuse.md | 2 +- docs/content/tutorials/basic-api.md | 5 ++-- docs/content/tutorials/best-practices.md | 2 +- docs/content/tutorials/connect-to-mysql.md | 6 ++--- docs/content/tutorials/net-core-mvc.md | 24 +++++++++---------- src/MySqlConnector/docs/README.md | 6 ++--- 12 files changed, 32 insertions(+), 33 deletions(-) diff --git a/docs/content/api/MySqlConnector/MySqlBatchType.md b/docs/content/api/MySqlConnector/MySqlBatchType.md index c31bbe8d5..d4c050907 100644 --- a/docs/content/api/MySqlConnector/MySqlBatchType.md +++ b/docs/content/api/MySqlConnector/MySqlBatchType.md @@ -11,7 +11,7 @@ When using MariaDB (10.2 or later), the commands will be sent in a single batch, Example usage: ```csharp -using var connection = new MySqlConnection("...connection string..."); +await using var connection = new MySqlConnection("...connection string..."); await connection.OpenAsync(); using var batch = new MySqlBatch(connection) diff --git a/docs/content/api/MySqlConnector/MySqlBulkCopyType.md b/docs/content/api/MySqlConnector/MySqlBulkCopyType.md index 747e4d4a4..657b90a25 100644 --- a/docs/content/api/MySqlConnector/MySqlBulkCopyType.md +++ b/docs/content/api/MySqlConnector/MySqlBulkCopyType.md @@ -18,7 +18,7 @@ Example code: var dataTable = GetDataTableFromExternalSource(); // open the connection -using var connection = new MySqlConnection("...;AllowLoadLocalInfile=True"); +await using var connection = new MySqlConnection("...;AllowLoadLocalInfile=True"); await connection.OpenAsync(); // bulk copy the data diff --git a/docs/content/api/MySqlConnector/MySqlBulkLoaderType.md b/docs/content/api/MySqlConnector/MySqlBulkLoaderType.md index 7fe80a253..f78453768 100644 --- a/docs/content/api/MySqlConnector/MySqlBulkLoaderType.md +++ b/docs/content/api/MySqlConnector/MySqlBulkLoaderType.md @@ -9,7 +9,7 @@ title: MySqlBulkLoader Example code: ```csharp -using var connection = new MySqlConnection("...;AllowLoadLocalInfile=True"); +await using var connection = new MySqlConnection("...;AllowLoadLocalInfile=True"); await connection.OpenAsync(); var bulkLoader = new MySqlBulkLoader(connection) { diff --git a/docs/content/diagnostics/logging.md b/docs/content/diagnostics/logging.md index f6f7d6f02..021b51a29 100644 --- a/docs/content/diagnostics/logging.md +++ b/docs/content/diagnostics/logging.md @@ -101,16 +101,16 @@ var loggerFactory = LoggerFactory.Create(builder => ); // now create a MySqlDataSource and configure it with the LoggerFactory -using var dataSource = new MySqlDataSourceBuilder(yourConnectionString) +await using var dataSource = new MySqlDataSourceBuilder(yourConnectionString) .UseLoggerFactory(loggerFactory) .Build(); // create all MySqlConnection objects via the MySqlDataSource, not directly -// DON'T: using var connection = new MySqlConnection(yourConnectionString); -using var connection = dataSource.CreateConnection(); +// DON'T: await using var connection = new MySqlConnection(yourConnectionString); +await using var connection = dataSource.CreateConnection(); // you can also create open connections -using var connection = await dataSource.OpenConnectionAsync(); +await using var connection = await dataSource.OpenConnectionAsync(); ``` ### Deprecated Logging Framework diff --git a/docs/content/diagnostics/metrics.md b/docs/content/diagnostics/metrics.md index f62f7f569..cf9a6119f 100644 --- a/docs/content/diagnostics/metrics.md +++ b/docs/content/diagnostics/metrics.md @@ -59,7 +59,7 @@ A connection pool name can be specified by one of the following means: The `MySqlDataSourceBuilder.UseName` method can be used to specify a name for the connection pool: ```csharp -using var dataSource = new MySqlDataSourceBuilder("...connection string...") +await using var dataSource = new MySqlDataSourceBuilder("...connection string...") .UseName("MyPoolName") .Build(); ``` @@ -86,7 +86,7 @@ builder.Services.AddKeyedMySqlDataSource("MyPoolName", Finally, the connection pool name can be specified by setting the `Application Name` connection string option: ```csharp -using var connection = new MySqlConnection("server=dbserver;...;Application Name=MyPoolName"); +await using var connection = new MySqlConnection("server=dbserver;...;Application Name=MyPoolName"); ``` If `UseName` is used, it will override the `Application Name` connection string option. diff --git a/docs/content/home.md b/docs/content/home.md index cb1899de0..3594c95d5 100644 --- a/docs/content/home.md +++ b/docs/content/home.md @@ -48,7 +48,7 @@ MySqlConnector also fully supports asynchronous I/O. The C# example above can be await using var connection = new MySqlConnection("Server=myserver;User ID=mylogin;Password=mypass;Database=mydatabase"); await connection.OpenAsync(); -using var command = new MySqlCommand("SELECT field FROM table;", connection); +await using var command = new MySqlCommand("SELECT field FROM table;", connection); await using var reader = await command.ExecuteReaderAsync(); while (await reader.ReadAsync()) Console.WriteLine(reader.GetString(0)); diff --git a/docs/content/troubleshooting/connection-reuse.md b/docs/content/troubleshooting/connection-reuse.md index 352c87653..711a18ae1 100644 --- a/docs/content/troubleshooting/connection-reuse.md +++ b/docs/content/troubleshooting/connection-reuse.md @@ -22,7 +22,7 @@ an open `MySqlDataReader`. You may not execute multiple operations in parallel, for example: ```csharp -using var connection = new MySqlConnection("..."); +await using var connection = new MySqlConnection("..."); await connection.OpenAsync(); await Task.WhenAll( // don't do this connection.ExecuteAsync("SELECT 1;"), diff --git a/docs/content/tutorials/basic-api.md b/docs/content/tutorials/basic-api.md index 2948fa999..3d9cb3ee7 100644 --- a/docs/content/tutorials/basic-api.md +++ b/docs/content/tutorials/basic-api.md @@ -30,11 +30,10 @@ using (var cmd = new MySqlCommand()) } // Retrieve all rows -using var command = new MySqlCommand("SELECT some_field FROM data", connection); -using var reader = await cmd.ExecuteReaderAsync(); +await using var command = new MySqlCommand("SELECT some_field FROM data", connection); +await using var reader = await cmd.ExecuteReaderAsync(); while (await reader.ReadAsync()) Console.WriteLine(reader.GetString(0)); ``` You can find more info about the ADO.NET API in the [MSDN documentation](https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/ado-net-overview) or in many tutorials on the Internet. - diff --git a/docs/content/tutorials/best-practices.md b/docs/content/tutorials/best-practices.md index 696b16df0..7693b0d02 100644 --- a/docs/content/tutorials/best-practices.md +++ b/docs/content/tutorials/best-practices.md @@ -159,7 +159,7 @@ public class Controllers { using var db = new AppDb(); await db.Connection.OpenAsync(); - using var cmd = db.Connection.CreateCommand(); + await using var cmd = db.Connection.CreateCommand(); cmd.CommandText = @"SELECT SLEEP(1)"; await cmd.ExecuteNonQueryAsync(); } diff --git a/docs/content/tutorials/connect-to-mysql.md b/docs/content/tutorials/connect-to-mysql.md index 1a23fed24..d89892397 100644 --- a/docs/content/tutorials/connect-to-mysql.md +++ b/docs/content/tutorials/connect-to-mysql.md @@ -72,7 +72,7 @@ In ASP.NET Core, the `MySqlConnection` object will be dependency-injected into y other kinds of projects, you may need to explicitly create the connection: ```csharp -using var connection = new MySqlConnection(yourConnectionString); +await using var connection = new MySqlConnection(yourConnectionString); ``` You can then open the connection and execute a query: @@ -80,8 +80,8 @@ You can then open the connection and execute a query: ```csharp await connection.OpenAsync(); -using var command = new MySqlCommand("SELECT field FROM table;", connection); -using var reader = await command.ExecuteReaderAsync(); +await using var command = new MySqlCommand("SELECT field FROM table;", connection); +await using var reader = await command.ExecuteReaderAsync(); while (await reader.ReadAsync()) { var value = reader.GetValue(0); diff --git a/docs/content/tutorials/net-core-mvc.md b/docs/content/tutorials/net-core-mvc.md index fec267d11..9b2ccf99f 100644 --- a/docs/content/tutorials/net-core-mvc.md +++ b/docs/content/tutorials/net-core-mvc.md @@ -93,8 +93,8 @@ public class BlogPostRepository(MySqlDataSource database) { public async Task FindOneAsync(int id) { - using var connection = await database.OpenConnectionAsync(); - using var command = connection.CreateCommand(); + await using var connection = await database.OpenConnectionAsync(); + await using var command = connection.CreateCommand(); command.CommandText = @"SELECT `Id`, `Title`, `Content` FROM `BlogPost` WHERE `Id` = @id"; command.Parameters.AddWithValue("@id", id); var result = await ReadAllAsync(await command.ExecuteReaderAsync()); @@ -103,24 +103,24 @@ public class BlogPostRepository(MySqlDataSource database) public async Task> LatestPostsAsync() { - using var connection = await database.OpenConnectionAsync(); - using var command = connection.CreateCommand(); + await using var connection = await database.OpenConnectionAsync(); + await using var command = connection.CreateCommand(); command.CommandText = @"SELECT `Id`, `Title`, `Content` FROM `BlogPost` ORDER BY `Id` DESC LIMIT 10;"; return await ReadAllAsync(await command.ExecuteReaderAsync()); } public async Task DeleteAllAsync() { - using var connection = await database.OpenConnectionAsync(); - using var command = connection.CreateCommand(); + await using var connection = await database.OpenConnectionAsync(); + await using var command = connection.CreateCommand(); command.CommandText = @"DELETE FROM `BlogPost`"; await command.ExecuteNonQueryAsync(); } public async Task InsertAsync(BlogPost blogPost) { - using var connection = await database.OpenConnectionAsync(); - using var command = connection.CreateCommand(); + await using var connection = await database.OpenConnectionAsync(); + await using var command = connection.CreateCommand(); command.CommandText = @"INSERT INTO `BlogPost` (`Title`, `Content`) VALUES (@title, @content);"; BindParams(command, blogPost); await command.ExecuteNonQueryAsync(); @@ -129,8 +129,8 @@ public class BlogPostRepository(MySqlDataSource database) public async Task UpdateAsync(BlogPost blogPost) { - using var connection = await database.OpenConnectionAsync(); - using var command = connection.CreateCommand(); + await using var connection = await database.OpenConnectionAsync(); + await using var command = connection.CreateCommand(); command.CommandText = @"UPDATE `BlogPost` SET `Title` = @title, `Content` = @content WHERE `Id` = @id;"; BindParams(command, blogPost); BindId(command, blogPost); @@ -139,8 +139,8 @@ public class BlogPostRepository(MySqlDataSource database) public async Task DeleteAsync(BlogPost blogPost) { - using var connection = await database.OpenConnectionAsync(); - using var command = connection.CreateCommand(); + await using var connection = await database.OpenConnectionAsync(); + await using var command = connection.CreateCommand(); command.CommandText = @"DELETE FROM `BlogPost` WHERE `Id` = @id;"; BindId(command, blogPost); await command.ExecuteNonQueryAsync(); diff --git a/src/MySqlConnector/docs/README.md b/src/MySqlConnector/docs/README.md index 855145b43..a99bfd9a0 100644 --- a/src/MySqlConnector/docs/README.md +++ b/src/MySqlConnector/docs/README.md @@ -17,16 +17,16 @@ var builder = new MySqlConnectionStringBuilder }; // open a connection asynchronously -using var connection = new MySqlConnection(builder.ConnectionString); +await using var connection = new MySqlConnection(builder.ConnectionString); await connection.OpenAsync(); // create a DB command and set the SQL statement with parameters -using var command = connection.CreateCommand(); +await using var command = connection.CreateCommand(); command.CommandText = @"SELECT * FROM orders WHERE order_id = @OrderId;"; command.Parameters.AddWithValue("@OrderId", orderId); // execute the command and read the results -using var reader = await command.ExecuteReaderAsync(); +await using var reader = await command.ExecuteReaderAsync(); while (reader.Read()) { var id = reader.GetInt32("order_id");