C#实现连接mysql数据库

第一种:
using System;
using MySql.Data.MySqlClient;

class Program {
static void Main(string[] args) {
string connectionString = "server=127.0.0.1;database=database;uid=username;password=mypassword;";
using MySqlConnection connection = new MySqlConnection(connectionString);
try {
connection.Open();
Console.WriteLine("Connection Opened!");
string sqlQuery = "SELECT * FROM mytable";
using MySqlCommand command = new MySqlCommand(sqlQuery, connection);
MySqlDataReader reader = command.ExecuteReader();
while (reader.Read()) {
Console.WriteLine($"Column1: {reader[0]}, Column2: {reader[1]}");
}
} catch (Exception ex) {
Console.WriteLine("Error: " + ex.Message);
} finally {
connection.Close();
Console.WriteLine("Connection Closed!");
}
}
}

第二种:

using Microsoft.EntityFrameworkCore; public class MyDbContext : DbContext { public DbSet<Customer> Customers { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseMySQL("server=localhost;user=root;database=mydatabase;password=mypassword;"); } } // 使用上下文进行数据库操作 using (var dbContext = new MyDbContext()) { // 查询所有客户 var customers = dbContext.Customers.ToList(); // ... }

第三种:

using MySql.Data.MySqlClient;
// ...

string connectionString = "server=localhost;user=root;database=mydatabase;password=mypassword;";
using (var connection = new MySqlConnection(connectionString))
{
    connection.Open();

    // 创建命令对象
    var command = new MySqlCommand("SELECT * FROM customers", connection);

    // 执行查询
    using (var reader = command.ExecuteReader())
    {
        while (reader.Read())
        {
            // 处理每行数据
            string name = reader.GetString("name");
            int age = reader.GetInt32("age");
            // ...
        }
    }

    connection.Close();
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
C#连接 MySQL 数据库可以通过以下步骤: 1. 下载 MySQL Connector/NET 驱动程序:在官方网站上下载 MySQL Connector/NET 驱动程序,然后进行安装。 2. 引用命名空间:在 C# 代码中引用以下命名空间: ``` using MySql.Data.MySqlClient; ``` 3. 创建连接对象:创建一个 MySqlConnection 对象,用于连接MySQL 数据库。在创建 MySqlConnection 对象时,需要指定 MySQL 服务器的 IP 地址、用户名、密码和数据库名等信息。例如: ``` string connectionString = "server=localhost;uid=root;pwd=password;database=mydb;"; MySqlConnection connection = new MySqlConnection(connectionString); ``` 其中,server 指定 MySQL 服务器的 IP 地址或主机名,uid 指定连接 MySQL 数据库的用户名,pwd 指定连接 MySQL 数据库的密码,database 指定连接数据库名。 4. 打开连接:使用 MySqlConnection 对象的 Open 方法打开与 MySQL 数据库连接。例如: ``` connection.Open(); ``` 5. 执行 SQL 命令:使用 MySqlCommand 对象执行 SQL 命令,例如: ``` string sql = "SELECT * FROM mytable"; MySqlCommand command = new MySqlCommand(sql, connection); MySqlDataReader reader = command.ExecuteReader(); ``` 其中,sql 指定要执行的 SQL 命令,MySqlCommand 对象用于执行 SQL 命令,MySqlDataReader 对象用于读取执行结果。 6. 关闭连接:使用 MySqlConnection 对象的 Close 方法关闭与 MySQL 数据库连接。例如: ``` connection.Close(); ``` 完整代码示例: ``` using MySql.Data.MySqlClient; string connectionString = "server=localhost;uid=root;pwd=password;database=mydb;"; MySqlConnection connection = new MySqlConnection(connectionString); connection.Open(); string sql = "SELECT * FROM mytable"; MySqlCommand command = new MySqlCommand(sql, connection); MySqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { // 处理数据 } connection.Close(); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

试行

祝您生活愉快!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值