如何将xampp mysql连接到asp.net mvc?

时间:2018-09-29 19:10:45

标签: c# mysql asp.net xampp

我一直试图将mysql xampp数据库连接到Visual Studio中的asp.net mvc项目,但是我一直失败。我已经能够在App_Data文件夹中添加连接,但是该连接无用。我想使用MySqlConnection连接到它,但是每当尝试它时,都会显示此错误:

 warning MSB3274: The primary reference "MySql.Data, Version=8.0.12.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d, processorArchitecture=MSIL" could not be resolved because it was built against the ".NETFramework,Version=v4.5.2" framework. This is a higher version than the currently targeted framework ".NETFramework,Version=v4.5".

这是我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MySql.Data.MySqlClient;
namespace Sendax.Controllers
{
    public class UsersController : Controller
    {
        // GET: Users
        public ActionResult Index()
        {

            string str = "Dsn=localhost;server=127.0.0.1;uid=root;database=test;port=3306";
            MySqlConnection con = new MySqlConnection(str);

            try { con.Open(); ViewBag.Exception = "Database Connected"; }
            catch (Exception e) { ViewBag.Exception = e.Message; }
            return View();
        }
    }
}

0 个答案:

没有答案