赞
踩
sqlsugar是一款非常轻量级并且特别强大的ORM,支持常见的关系型数据库(Oracle , sqlserver , MySQL等等等等),本文示例的是SqlServer,更多一起玩耍的朋友可以关注鄙人的公众号,获取更多源码哦
思路和流程如下
using SqlSugar; using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace WebApplication1.Helper { public class dbContext { private static SqlSugarClient _db = null; /// <summary> /// test是数据库名 /// </summary> public static string ConnectionString = "Data Source=localhost;Initial Catalog=test;User id=sa;Password=123456"; public static SqlSugarClient CretClient() { _db = new SqlSugarClient(new ConnectionConfig() { ConnectionString = ConnectionString, //数据库连接字符串 DbType = DbType.SqlServer, //必填 IsAutoCloseConnection = false, //默认false InitKeyType = InitKeyType.Attribute }); return _db; } } }
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace WebApplication1.Helper
{
public partial class test
{
public int ID { get; set; }
public string usernage { get; set; }
public string password { get; set; }
}
}
using SqlSugar; using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using WebApplication1.Helper; namespace WebApplication1.Controllers { public class HomeController : Controller { public static SqlSugarClient DbContext = dbContext.CretClient(); public ActionResult Index() { var results = DbContext.GetSimpleClient<test>().GetList().Where(i => i.ID == 1); var sqlResults = DbContext.SqlQueryable<test>("select * from test where ID = 1").ToList(); return View(); } public ActionResult About() { ViewBag.Message = "Your application description page."; return View(); } public ActionResult Contact() { ViewBag.Message = "Your contact page."; return View(); } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。