一個應用要運行起來,往往需要讀取很多的預設好的配置信息,根據約定好的信息或方式執行一定的行為。 配置的本質就是軟體運行的參數,在一個軟體實現中需要的參數非常多,如果我們以 Hard Code(硬編碼)的方式寫在應用代碼中,這樣配置就會很亂,而且後續也不容易修改。亂而多,而且不容易修改,這就需要一個統 ...
使用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();
}
}
}
本文來自博客園,作者:.net&new,轉載請註明原文鏈接:https://www.cnblogs.com/wugh8726254/p/17171306.html