framework4.8 使用sqlsugar
使用nuget安装mysqlhttps://img2023.cnblogs.com/blog/1055725/202303/1055725-20230302114451839-606648981.png
安装sqlsugar
https://img2023.cnblogs.com/blog/1055725/202303/1055725-20230302114517065-1657662022.png
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(new Student()
{
Name = "李白",
SchoolId = 1
}).ExecuteCommand();
return View();
}
public static SqlSugarClientConnectionSqlsugar()
{
//创建数据库对象
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个属性
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();
}
}
}
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
页:
[1]