C# MVC链接mysql数据库

1.需要引用 MySql.Data.dll ,所以要去下载安装

  传送链接:  点击打开链接

2.下载后打开安装


C# MVC链接mysql数据库_第1张图片

安装完成就可以使用了

3.打开 VS开发工具,新建一个MVC项目

C# MVC链接mysql数据库_第2张图片

4.项目新建完成后,添加引用,引用路径(之前程序安装目录):C:\Program Files (x86)\MySQL\MySQL Connector Net 6.10.6\Assemblies\v4.5.2

C# MVC链接mysql数据库_第3张图片

C# MVC链接mysql数据库_第4张图片

C# MVC链接mysql数据库_第5张图片

5.新建一个连接数据库的类 MysqlUtil.cs  和一个 User类,MysqlUtil用于数据库连接,User为实体类

using Microsoft.VisualStudio.TestTools.UnitTesting;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WebApplication1.Models;

namespace WebApplication1.Util
{
    //mysql 连接
    public  class MysqlUtil
    {


        //数据库连接
         string constructorString = "Database=test;Data Source=127.0.0.1;User Id=root;Password=root;pooling=false;CharSet=utf8;port=3306";
         MySqlConnection myConnnect;


        /// 
        /// 查询
        /// 
        /// sql语句
        /// 
        public List Select(String sqlcontnet)
        {
            List lists = new List();
            
            MySqlConnection myConnnect = new MySqlConnection(constructorString);
            myConnnect.Open();
            MySqlCommand myCmd = new MySqlCommand(sqlcontnet, myConnnect);
            MySqlDataReader reader = myCmd.ExecuteReader();
            while (reader.Read())
            {
                if (reader.HasRows)
                {
                    lists.Add(new User(reader.GetInt32(0), reader.GetString(1), reader.GetString(2)));
                 }
            }

            return lists;
        }

        /// 
        /// 查询
        /// 
        /// sql语句
        /// 
        public int Insert(String sqlcontnet)
        {
            
            MySqlConnection myConnnect = new MySqlConnection(constructorString);
            myConnnect.Open();
            MySqlCommand myCmd = new MySqlCommand(sqlcontnet, myConnnect);
            Console.WriteLine(myCmd.CommandText);
            if (myCmd.ExecuteNonQuery() > 0)
            {
                Console.WriteLine("数据插入成功!");
            }
            return myCmd.ExecuteNonQuery();

        }


        /// 
        /// 查询
        /// 
        /// sql语句
        /// 
        public int Update(String sqlcontnet)
        {

            MySqlConnection myConnnect = new MySqlConnection(constructorString);
            myConnnect.Open();
            MySqlCommand myCmd = new MySqlCommand(sqlcontnet, myConnnect);
            Console.WriteLine(myCmd.CommandText);
            if (myCmd.ExecuteNonQuery() > 0)
            {
                Console.WriteLine("修改插入成功!");
            }
            return myCmd.ExecuteNonQuery();

        }


        /// 
        /// 查询
        /// 
        /// sql语句
        /// 
        public int Delete(String sqlcontnet)
        {

            MySqlConnection myConnnect = new MySqlConnection(constructorString);
            myConnnect.Open();
            MySqlCommand myCmd = new MySqlCommand(sqlcontnet, myConnnect);
            Console.WriteLine(myCmd.CommandText);
            if (myCmd.ExecuteNonQuery() > 0)
            {
                Console.WriteLine("删除成功!");
            }
            return myCmd.ExecuteNonQuery();

        }
        
    }
}

控制器引用

        // GET: Main
        public ActionResult Index()
        {
            Util.MysqlUtil  db= new Util.MysqlUtil();
            List lists=db.Select("select * from User");
            ViewBag.lists = lists;
            User user = new User();
            int i= user.UserID1;
            return View();
        }

页面显示

 
    @foreach (var stakerholder in ViewBag.lists ) {
  • @stakerholder.UserID   : @stakerholder.UserName
  • }
参考链接: 点击打开链接


你可能感兴趣的:(C#,MVC,.Net)