|
使用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(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();
}
}
}
来源:https://www.cnblogs.com/wugh8726254/archive/2023/03/02/17171306.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作! |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?立即注册
x
|