framework4.8 使用sqlsugar

使用nuget安装mysql

 

 

安装sqlsugar 

 

 

using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace sqlsugar_demo.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
//建表
var db = ConnectionSqlsugar();
db.Open();

db.Insertable<Student>(new Student()
{
Name = "李白",
SchoolId = 1
}).ExecuteCommand();

return View();
}


public static SqlSugarClient ConnectionSqlsugar()
{

//创建数据库对象
SqlSugarClient Db = new SqlSugarClient(new ConnectionConfig()
{
ConnectionString = "Server=localhost;Port=3308;Database=comcms2;Uid=root;Pwd=123456;",
DbType = DbType.MySql,
IsAutoCloseConnection = true
},
db => {
db.Aop.OnLogExecuting = (sql, pars) =>
{
Console.WriteLine(sql);
};
});

return Db;
}


//实体与数据库结构一样
public class Student
{
//数据是自增需要加上IsIdentity
//数据库是主键需要加上IsPrimaryKey
//注意:要完全和数据库一致2个属性
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
public int Id { get; set; }
public int? SchoolId { get; set; }
public string Name { get; set; }
}


public ActionResult About()
{
ViewBag.Message = "Your application description page.";

return View();
}

public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";

return View();
}
}
}

posted @ 2023-03-02 11:46  .net&new  阅读(545)  评论(0编辑  收藏  举报