Skip to content

Use await using in ASP.NET Core tutorial doc #1580

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/content/api/MySqlConnector/MySqlBatchType.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/content/api/MySqlConnector/MySqlBulkCopyType.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/content/api/MySqlConnector/MySqlBulkLoaderType.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions docs/content/diagnostics/logging.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions docs/content/diagnostics/metrics.md
Original file line number Diff line number Diff line change
Expand Up @@ -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();
```
Expand All @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion docs/content/home.md
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
2 changes: 1 addition & 1 deletion docs/content/troubleshooting/connection-reuse.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;"),
Expand Down
5 changes: 2 additions & 3 deletions docs/content/tutorials/basic-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

2 changes: 1 addition & 1 deletion docs/content/tutorials/best-practices.md
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
6 changes: 3 additions & 3 deletions docs/content/tutorials/connect-to-mysql.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,16 @@ 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:

```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);
Expand Down
24 changes: 12 additions & 12 deletions docs/content/tutorials/net-core-mvc.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ public class BlogPostRepository(MySqlDataSource database)
{
public async Task<BlogPost?> 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());
Expand All @@ -103,24 +103,24 @@ public class BlogPostRepository(MySqlDataSource database)

public async Task<IReadOnlyList<BlogPost>> 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();
Expand All @@ -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);
Expand All @@ -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();
Expand Down
6 changes: 3 additions & 3 deletions src/MySqlConnector/docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down