/// <summary> /// 根据订单号返回一个实体 /// </summary> public Dapper.Model.orders GetModel(string order_no) { StringBuilder strSql = new StringBuilder(); StringBuilder str1 = new StringBuilder(); Dapper.Model.orders model = new Dapper.Model.orders(); //利用反射获得属性的所有公共属性 PropertyInfo[] pros = model.GetType().GetProperties(); foreach (PropertyInfo p in pros) { //拼接字段,忽略List<T> if (!typeof(System.Collections.IList).IsAssignableFrom(p.PropertyType)) { str1.Append(p.Name + ",");//拼接字段 } } strSql.Append("select top 1 " + str1.ToString().Trim(',') + " from " + databaseprefix + "orders"); strSql.Append(" where order_no=@order_no"); SqlParameter[] parameters = { new SqlParameter("@order_no", SqlDbType.NVarChar, 100) }; parameters[0].Value = order_no; DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters); if (ds.Tables[0].Rows.Count > 0) { return(DataRowToModel(ds.Tables[0].Rows[0])); } else { return(null); } }
/// <summary> /// 更新一条数据 /// </summary> public bool Update(Dapper.Model.orders model) { StringBuilder strSql = new StringBuilder(); StringBuilder str1 = new StringBuilder(); //利用反射获得属性的所有公共属性 PropertyInfo[] pros = model.GetType().GetProperties(); List <SqlParameter> paras = new List <SqlParameter>(); strSql.Append("update " + databaseprefix + "orders set "); foreach (PropertyInfo pi in pros) { //如果不是主键则追加sql字符串 if (!pi.Name.Equals("id") && !typeof(System.Collections.IList).IsAssignableFrom(pi.PropertyType)) { //判断属性值是否为空 if (pi.GetValue(model, null) != null) { str1.Append(pi.Name + "=@" + pi.Name + ","); //声明参数 paras.Add(new SqlParameter("@" + pi.Name, pi.GetValue(model, null))); //对参数赋值 } } } strSql.Append(str1.ToString().Trim(',')); strSql.Append(" where id=@id"); paras.Add(new SqlParameter("@id", model.id)); return(DbHelperSQL.ExecuteSql(strSql.ToString(), paras.ToArray()) > 0); }
/// <summary> /// 将对象转换实体 /// </summary> public Dapper.Model.orders DataRowToModel(DataRow row) { Dapper.Model.orders model = new Dapper.Model.orders(); if (row != null) { #region 主表信息====================== //利用反射获得属性的所有公共属性 Type modelType = model.GetType(); for (int i = 0; i < row.Table.Columns.Count; i++) { //查找实体是否存在列表相同的公共属性 PropertyInfo proInfo = modelType.GetProperty(row.Table.Columns[i].ColumnName); if (proInfo != null && row[i] != DBNull.Value) { proInfo.SetValue(model, row[i], null);//用索引值设置属性值 } } #endregion #region 子表信息====================== StringBuilder strSql1 = new StringBuilder(); strSql1.Append("select * from " + databaseprefix + "order_goods"); strSql1.Append(" where order_id=@id"); SqlParameter[] parameters1 = { new SqlParameter("@id", SqlDbType.Int, 4) }; parameters1[0].Value = model.id; DataTable dt1 = DbHelperSQL.Query(strSql1.ToString(), parameters1).Tables[0]; if (dt1.Rows.Count > 0) { int rowsCount = dt1.Rows.Count; List <Dapper.Model.order_goods> models = new List <Dapper.Model.order_goods>(); Dapper.Model.order_goods modelt; for (int n = 0; n < rowsCount; n++) { modelt = new Dapper.Model.order_goods(); Type modeltType = modelt.GetType(); for (int i = 0; i < dt1.Rows[n].Table.Columns.Count; i++) { PropertyInfo proInfo = modeltType.GetProperty(dt1.Rows[n].Table.Columns[i].ColumnName); if (proInfo != null && dt1.Rows[n][i] != DBNull.Value) { proInfo.SetValue(modelt, dt1.Rows[n][i], null); } } models.Add(modelt); } model.order_goods = models; } #endregion } return(model); }
/// <summary> /// 增加一条数据 /// </summary> public int Add(Dapper.Model.orders model) { using (SqlConnection conn = new SqlConnection(DbHelperSQL.connectionString)) { conn.Open();//打开数据连接 using (SqlTransaction trans = conn.BeginTransaction()) { try { #region 主表信息========================== StringBuilder strSql = new StringBuilder(); StringBuilder str1 = new StringBuilder(); //数据字段 StringBuilder str2 = new StringBuilder(); //数据参数 //利用反射获得属性的所有公共属性 PropertyInfo[] pros = model.GetType().GetProperties(); List <SqlParameter> paras = new List <SqlParameter>(); strSql.Append("insert into " + databaseprefix + "orders("); foreach (PropertyInfo pi in pros) { //如果不是主键则追加sql字符串 if (!pi.Name.Equals("id") && !typeof(System.Collections.IList).IsAssignableFrom(pi.PropertyType)) { //判断属性值是否为空 if (pi.GetValue(model, null) != null) { str1.Append(pi.Name + ","); //拼接字段 str2.Append("@" + pi.Name + ","); //声明参数 paras.Add(new SqlParameter("@" + pi.Name, pi.GetValue(model, null))); //对参数赋值 } } } strSql.Append(str1.ToString().Trim(',')); strSql.Append(") values ("); strSql.Append(str2.ToString().Trim(',')); strSql.Append(") "); strSql.Append(";select @@IDENTITY;"); object obj = DbHelperSQL.GetSingle(conn, trans, strSql.ToString(), paras.ToArray());//带事务 model.id = Convert.ToInt32(obj); #endregion #region 订单商品列表====================== if (model.order_goods != null) { StringBuilder strSql2; //SQL字符串 StringBuilder strSql3; StringBuilder strSql4; StringBuilder str21; //数据库字段 StringBuilder str22; //声明参数 foreach (Dapper.Model.order_goods modelt in model.order_goods) { strSql2 = new StringBuilder(); str21 = new StringBuilder(); str22 = new StringBuilder(); PropertyInfo[] pros2 = modelt.GetType().GetProperties(); List <SqlParameter> paras2 = new List <SqlParameter>(); strSql2.Append("insert into " + databaseprefix + "order_goods("); foreach (PropertyInfo pi in pros2) { if (!pi.Name.Equals("id")) { if (pi.GetValue(modelt, null) != null) { str21.Append(pi.Name + ","); str22.Append("@" + pi.Name + ","); if (pi.Name.Equals("order_id")) { paras2.Add(new SqlParameter("@" + pi.Name, model.id));//将刚插入的父ID赋值 } else { paras2.Add(new SqlParameter("@" + pi.Name, pi.GetValue(modelt, null))); } } } } strSql2.Append(str21.ToString().Trim(',')); strSql2.Append(") values ("); strSql2.Append(str22.ToString().Trim(',')); strSql2.Append(") "); DbHelperSQL.ExecuteSql(conn, trans, strSql2.ToString(), paras2.ToArray()); //扣减商品库存 string channelName = new DAL.site_channel(databaseprefix).GetChannelName(modelt.channel_id);//查询频道的名称 strSql4 = new StringBuilder(); strSql4.Append("update " + databaseprefix + DTKeys.TABLE_CHANNEL_ARTICLE + channelName + " set "); strSql4.Append("stock_quantity=stock_quantity-@stock_quantity where id=@article_id"); SqlParameter[] parameters4 = { new SqlParameter("@stock_quantity", SqlDbType.Int, 4), new SqlParameter("@article_id", SqlDbType.Int, 4) }; parameters4[0].Value = modelt.quantity; parameters4[1].Value = modelt.article_id; DbHelperSQL.ExecuteSql(conn, trans, strSql4.ToString(), parameters4); } } #endregion trans.Commit(); //提交事务 } catch { trans.Rollback(); //回滚事务 return(0); } } } return(model.id); }