.NET Core 中使用 ADO.NET 连接并操作 MySQL 数据库可以按照以下步骤进行。你需要安装 MySQL 数据库驱动程序(MySql.Data)并编写代码来执行数据库操作。

1、安装 MySQL 数据库驱动程序
在你的 .NET Core 项目中,打开命令行并运行以下命令来安装 MySql.Data 包:
dotnet add package MySql.Data
2、编写代码连接 MySQL 数据库
下面是一个示例代码,展示如何使用 ADO.NET 连接到 MySQL 数据库,并执行基本的 CRUD 操作(创建、读取、更新、删除)。
using System;
using System.Data;
using MySql.Data.MySqlClient;

class Program
{
    static void Main()
    {
        string connectionString = "Server=your_server;Database=your_database;User ID=your_user;Password=your_password;";

        using (MySqlConnection connection = new MySqlConnection(connectionString))
        {
            try
            {
                connection.Open();
                Console.WriteLine("Connection successful!");

                // 创建表
                CreateTable(connection);

                // 插入数据
                InsertData(connection, "John Doe", 30);
                InsertData(connection, "Jane Smith", 25);

                // 读取数据
                ReadData(connection);

                // 更新数据
                UpdateData(connection, 1, "John Doe", 31);

                // 删除数据
                DeleteData(connection, 2);

                // 读取数据
                ReadData(connection);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"An error occurred: {ex.Message}");
            }
        }
    }

    static void CreateTable(MySqlConnection connection)
    {
        string createTableQuery = @"
            CREATE TABLE IF NOT EXISTS Persons (
                PersonID INT AUTO_INCREMENT PRIMARY KEY,
                Name VARCHAR(255) NOT NULL,
                Age INT NOT NULL
            )";
        MySqlCommand cmd = new MySqlCommand(createTableQuery, connection);
        cmd.ExecuteNonQuery();
        Console.WriteLine("Table created successfully.");
    }

    static void InsertData(MySqlConnection connection, string name, int age)
    {
        string insertQuery = "INSERT INTO Persons (Name, Age) VALUES (@Name, @Age)";
        MySqlCommand cmd = new MySqlCommand(insertQuery, connection);
        cmd.Parameters.AddWithValue("@Name", name);
        cmd.Parameters.AddWithValue("@Age", age);
        cmd.ExecuteNonQuery();
        Console.WriteLine("Data inserted successfully.");
    }

    static void ReadData(MySqlConnection connection)
    {
        string selectQuery = "SELECT PersonID, Name, Age FROM Persons";
        MySqlCommand cmd = new MySqlCommand(selectQuery, connection);
        using (MySqlDataReader reader = cmd.ExecuteReader())
        {
            while (reader.Read())
            {
                Console.WriteLine($"ID: {reader["PersonID"]}, Name: {reader["Name"]}, Age: {reader["Age"]}");
            }
        }
    }

    static void UpdateData(MySqlConnection connection, int personId, string name, int age)
    {
        string updateQuery = "UPDATE Persons SET Name = @Name, Age = @Age WHERE PersonID = @PersonID";
        MySqlCommand cmd = new MySqlCommand(updateQuery, connection);
        cmd.Parameters.AddWithValue("@PersonID", personId);
        cmd.Parameters.AddWithValue("@Name", name);
        cmd.Parameters.AddWithValue("@Age", age);
        cmd.ExecuteNonQuery();
        Console.WriteLine("Data updated successfully.");
    }

    static void DeleteData(MySqlConnection connection, int personId)
    {
        string deleteQuery = "DELETE FROM Persons WHERE PersonID = @PersonID";
        MySqlCommand cmd = new MySqlCommand(deleteQuery, connection);
        cmd.Parameters.AddWithValue("@PersonID", personId);
        cmd.ExecuteNonQuery();
        Console.WriteLine("Data deleted successfully.");
    }
}

相关文档:.NET Core 使用ADO.NET连接操作MySQL数据库-CJavaPy

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐