FuncStateless.Mysql
1.0.0
dotnet add package FuncStateless.Mysql --version 1.0.0
NuGet\Install-Package FuncStateless.Mysql -Version 1.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="FuncStateless.Mysql" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="FuncStateless.Mysql" Version="1.0.0" />
<PackageReference Include="FuncStateless.Mysql" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add FuncStateless.Mysql --version 1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: FuncStateless.Mysql, 1.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package FuncStateless.Mysql@1.0.0
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=FuncStateless.Mysql&version=1.0.0
#tool nuget:?package=FuncStateless.Mysql&version=1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
UOS Func Stateless CRUD Mysql CSharp SDK
用法
1.安装 UOS Func Stateless CRUD Mysql CSharp SDK
dotnet add package FuncStateless.Mysql
2.引用本 SDK 并连接 Mysql Client
// If you do not specify a databaseId, the sdk will automatically search for and connect to an available database.
// 如果没有指定 databaseId 的话,sdk 会自动获取可用的数据库进行连接。
// Please note: if uos is authorized to store your database password in an encrypted form, this SDK will automatically fill in the password, if not authorized, you will need to specify the database password in your code.
// 注意:如果uos被授权以加密形式存储您的数据库密码,本sdk将自动填入密码,如果未被授权,您需要在代码中指定数据库密码。
// Please note: if there are multiple available databases, you must use the databaseId to specify the desired database you wish to connect to.
// 注意:如果有多个可用数据库的话,请在代码中指定要用于连接的数据库的 databaseId
var databaseId = "";
var databasePassword = "";
// 注意:Mysql连接需要使用using语句释放资源(或手动释放),以防止连接池占满
await using var conn = await MysqlClientManager.GetConnection(databaseId, databasePassword);
3.使用 Mysql Client 操作数据库
例(创建数据库):
try
{
await using var conn = await MysqlClientManager.GetConnection();
await using var cmd = new MySqlCommand($"CREATE DATABASE IF NOT EXISTS {databaseName};", conn);
await cmd.ExecuteNonQueryAsync();
Console.WriteLine($"Database '{databaseName}' created.");
}
catch (MySqlException ex)
{
Console.WriteLine($"MySQL error: {ex.Message}");
}
catch (Exception ex)
{
Console.WriteLine($"Unexpected error: {ex.Message}");
}
更多示例如下所示。
4.完整操作示例
/*
环境变量(自动生成):
Environment.GetEnvironmentVariable("UOS_APP_ID") = "8972b2ba-0000-0000-0000-1274083a2c09";
Environment.GetEnvironmentVariable("UOS_APP_SECRET") = "9e131ad6-0000-0000-0000-936df2b1e70a";
Environment.GetEnvironmentVariable("UOS_APP_SERVICE_SECRET") = "f4f57f44-0000-0000-0000-d469d7b45053";
*/
public class Sample
{
public static async Task Main()
{
// If you do not specify a databaseId, the sdk will automatically search for and connect to an available database.
// 如果没有指定 databaseId 的话,sdk 会自动获取可用的数据库进行连接。
// Please note: if uos is authorized to store your database password in an encrypted form, this SDK will automatically fill in the password, if not authorized, you will need to specify the database password in your code.
// 注意:如果uos被授权以加密形式存储您的数据库密码,本sdk将自动填入密码,如果未被授权,您需要在代码中指定数据库密码。
// Please note: if there are multiple available databases, you must use the databaseId to specify the desired database you wish to connect to.
// 注意:如果有多个可用数据库的话,请在代码中指定要用于连接的数据库的 databaseId。
try
{
await CreateDatabaseAsync("TestDatabase");
await CreateTableAsync("TestDatabase");
await InsertDataAsync("TestDatabase", "John Doe", 30);
await InsertDataAsync("TestDatabase", "Jane Smith", 25);
await GetDataAsync("TestDatabase");
}
catch (Exception e)
{
Console.Error.WriteLine(e);
}
}
private static async Task CreateDatabaseAsync(string databaseName)
{
try
{
await using var conn = await MysqlClientManager.GetConnection();
await using var cmd = new MySqlCommand($"CREATE DATABASE IF NOT EXISTS {databaseName};", conn);
await cmd.ExecuteNonQueryAsync();
Console.WriteLine($"Database '{databaseName}' created.");
}
catch (MySqlException ex)
{
Console.WriteLine($"MySQL error: {ex.Message}");
}
catch (Exception ex)
{
Console.WriteLine($"Unexpected error: {ex.Message}");
}
}
private static async Task CreateTableAsync(string databaseName)
{
try
{
await using var conn = await MysqlClientManager.GetConnection("", "", databaseName);
var createTableQuery = @"
CREATE TABLE IF NOT EXISTS People (
ID INT AUTO_INCREMENT PRIMARY KEY,
Name VARCHAR(100),
Age INT
);";
await using var cmd = new MySqlCommand(createTableQuery, conn);
await cmd.ExecuteNonQueryAsync();
Console.WriteLine("Table 'People' created.");
}
catch (MySqlException ex)
{
Console.WriteLine($"MySQL error: {ex.Message}");
}
catch (Exception ex)
{
Console.WriteLine($"Unexpected error: {ex.Message}");
}
}
private static async Task InsertDataAsync(string databaseName, string name, int age)
{
try
{
await using var conn = await MysqlClientManager.GetConnection("", "", databaseName);
var insertQuery = "INSERT INTO People (Name, Age) VALUES (@name, @age);";
await using MySqlCommand cmd = new MySqlCommand(insertQuery, conn);
cmd.Parameters.AddWithValue("@name", name);
cmd.Parameters.AddWithValue("@age", age);
await cmd.ExecuteNonQueryAsync();
Console.WriteLine($"Inserted {name}, {age} into 'People' table.");
}
catch (MySqlException ex)
{
Console.WriteLine($"MySQL error: {ex.Message}");
}
catch (Exception ex)
{
Console.WriteLine($"Unexpected error: {ex.Message}");
}
}
private static async Task GetDataAsync(string databaseName)
{
try
{
await using var conn = await MysqlClientManager.GetConnection("", "", databaseName);
var selectQuery = "SELECT * FROM People;";
await using var cmd = new MySqlCommand(selectQuery, conn);
await using var reader = await cmd.ExecuteReaderAsync();
while (await reader.ReadAsync())
{
var id = reader.GetInt32(reader.GetOrdinal("ID"));
var name = reader.GetString(reader.GetOrdinal("Name"));
var age = reader.GetInt32(reader.GetOrdinal("Age"));
Console.WriteLine($"ID: {id}, Name: {name}, Age: {age}");
}
}
catch (MySqlException ex)
{
Console.WriteLine($"MySQL error: {ex.Message}");
}
catch (Exception ex)
{
Console.WriteLine($"Unexpected error: {ex.Message}");
}
}
}
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.1 is compatible. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.1
- MySqlConnector (>= 2.3.7)
- Newtonsoft.Json (>= 13.0.3)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.0.0 | 148 | 12/30/2024 |