一、实体类的建立
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace Peixun.Model{ public class Banqi { public int id{get;set;} public string bqid{get;set;} public string xxdw{get;set;} public string bqmc { get; set; } public string zygz { get; set; } public string pxdd { get; set; } public string skjs { get; set; } public DateTime pxks { get; set; } public DateTime pxjs { get; set; } public int bqrs { get; set; } public DateTime create_time { get; set; } public DateTime update_time { get; set; } }}
二、数据访问层DAL的建立
1、传入参数:实体类
2、SQL语句
3、实体类的属性值赋值给SQL参数
4、获得自动插入的id,并赋给实体id,返回实体类id
//增加数据 public int Add(Model.Banqi model) { StringBuilder strSql = new StringBuilder(); strSql.Append("insert into banqi("); strSql.Append("[bqid] ,[xxdw] ,[bqmc] ,[zygz] ,[pxdd] ,[skjs] ,[pxks] ,[pxjs],[bqrs],[create_time])"); strSql.Append(" values ("); strSql.Append("@bqid,@xxdw,@bqmc,@zygz,@pxdd,@skjs,@pxks,@pxjs,@bqrs,@create_time)"); strSql.Append(";select @@IDENTITY"); SqlParameter[] parameters = { new SqlParameter("@bqid", SqlDbType.NVarChar,50), new SqlParameter("@xxdw", SqlDbType.NVarChar,50), new SqlParameter("@bqmc", SqlDbType.NVarChar,50), new SqlParameter("@zygz", SqlDbType.NVarChar,50), new SqlParameter("@pxdd", SqlDbType.NVarChar,50), new SqlParameter("@skjs", SqlDbType.NVarChar,50), new SqlParameter("@pxks", SqlDbType.DateTime), new SqlParameter("@pxjs", SqlDbType.DateTime), new SqlParameter("@bqrs", SqlDbType.TinyInt), new SqlParameter("@create_time", SqlDbType.DateTime) }; parameters[0].Value = model.bqid; parameters[1].Value = model.xxdw; parameters[2].Value = model.bqmc; parameters[3].Value = model.zygz; parameters[4].Value = model.pxdd; parameters[5].Value = model.skjs; parameters[6].Value = model.pxks; parameters[7].Value = model.pxjs; parameters[8].Value = model.bqrs; parameters[9].Value = model.create_time; //添加主表数据 object obj = new SqlHelper().ExecuteScalar(strSql.ToString(), parameters, CommandType.Text); model.id = Convert.ToInt32(obj); return model.id; }
三、web层后台代码
建立内部添加数据方法,实例化实体类并将前台页面控件值赋予其属性值。在添加数据按钮单击事件中,调用该方法,返回true代表添加成功,关闭弹出窗口并刷新父窗口(layui弹出窗口及其应用实例)。
//增加操作 private bool DoAdd() { bool result = false; Model.Banqi model = new Model.Banqi(); model.bqid = txtBqid.Text.Trim(); model.xxdw = txtXxdw.Text.Trim(); model.bqmc = txtBqmc.Text.Trim(); model.zygz = txtZygz.Text.Trim(); model.pxdd = txtPxdd.Text.Trim(); model.skjs = txtSkjs.Text.Trim(); model.pxks = Convert.ToDateTime(txtPxks.Text.Trim()); model.pxjs = Convert.ToDateTime(txtPxjs.Text.Trim()); model.bqrs = Convert.ToInt32(txtPxrs.Text.Trim()); model.create_time = DateTime.Now; if (new DAL.banqi().Add(model) > 0) { result = true; } return result; } protected void Button1_Click(object sender, EventArgs e) { if (!DoAdd()) { Response.Write(""); return; } else { Response.Write(""); } }