c# 连接 MySql
创建工程
略
安装依赖
- Dapper
- MysqlData
代码
using Dapper;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
namespace Mysql
{
class Program
{
static void Main(string[] args)
{
using (IDbConnection conn = MySqlConnection())
{
List<dynamic> resultLst = conn.Query<dynamic>(@"SELECT * FROM t_table").ToList();
foreach (var item in resultLst)
{
Console.WriteLine(item.id);
}
conn.Close();
}
}
public static MySqlConnection MySqlConnection()
{
var connection = new MySqlConnection("server=127.0.0.1;User=root;password=psw;Database=db;");
connection.Open();
return connection;
}
}
}