打开VS,工具》NuGet包管理器》管理解决方案的NuGet程序包,搜索MySql.Data并安装

NetCore连接MySQL

测试连接MySQL的代码:

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using MySql.Data.MySqlClient;

namespace ZZTCore.Controllers
{
    public class LoginController : Controller
    {
        public IActionResult Index()
        {
            DataTable dt = new DataTable();
            DataSet ds = new DataSet();
            try
            {
                using (MySqlConnection con = new MySqlConnection("Data Source=localhost;User ID=root;Password=zt102545;Database=employees;Allow User Variables=True;Charset=utf8;"))
                {
                    if (con.State != ConnectionState.Open)
                        con.Open();
                    MySqlDataAdapter sda = new MySqlDataAdapter("select * from titles", con);
                    sda.Fill(ds);
                    con.Close();
                }
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
            return View();
        }
    }
}

 

相关文章:

  • 2022-12-23
  • 2021-04-30
  • 2021-12-26
  • 2022-01-10
  • 2021-08-03
  • 2021-11-28
  • 2021-12-30
猜你喜欢
  • 2021-05-23
  • 2021-12-16
  • 2022-12-23
  • 2022-12-23
  • 2021-10-06
相关资源
相似解决方案