Dapper试用简例
1.选择3.5以上框架在新建项目中引用Dapper.dll。
2.在后台写代码,代码写出来后感觉以前学的都白学了。
3.
using Dapper; using System; using System.Collections.Generic; using System.Configuration; using System.Data; using System.Data.SqlClient; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace WebApplication1 { public partial class WebForm1 : System.Web.UI.Page { /// <summary> /// 实体化一个对象 /// </summary> public class Area { public int AreaId { get; set; } public string AreaName { get; set; } public int AreaPid { get; set; } } protected void Page_Load(object sender, EventArgs e) { if (IsPostBack) { return; } ConnSql(); //ExcuteSql(new Area //{ // AreaName = "河南省" // , // AreaPid = 0 //}); } /// <summary> /// 连接数据库 /// </summary> /// <returns></returns> private SqlConnection OpenConnection() { string connectionString = ConfigurationManager.ConnectionStrings["Sql"].ConnectionString; SqlConnection connection = new SqlConnection(connectionString); connection.Open(); return connection; } /// <summary> /// 查询操作 /// </summary> private void ConnSql() { using (IDbConnection connection = OpenConnection()) { string sql = @"select AreaId,AreaName from TblArea "; var parameter = new DynamicParameters(); parameter.Add("@AreaName", null, null, null); IEnumerable<Area> links = connection.Query<Area>(sql, parameter); GridView1.DataSource = links; GridView1.DataBind(); } } /// <summary> /// 执行性语句 /// </summary> /// <param name="AreaMode"></param> private void ExcuteSql(Area AreaMode) { using (IDbConnection connection = OpenConnection()) { string sql = @"insert into TblArea (AreaName,AreaPid) values(@AreaName,@AreaPid) "; var parament = new DynamicParameters(); parament.Add("@AreaName", AreaMode.AreaName, null, null, null); parament.Add("@AreaPid", AreaMode.AreaPid, null, null, null); int links = connection.Execute(sql, parament); if (links > 0) { Response.Write(links.ToString()); } } } } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。