/// <summary> /// 得到一个对象实体 /// </summary> public Model.Order GetModel(string id) { StringBuilder strSql = new StringBuilder(); StringBuilder str1 = new StringBuilder(); Model.Order model = new Model.Order(); //利用反射获得属性的所有公共属性 PropertyInfo[] pros = model.GetType().GetProperties(); foreach (PropertyInfo p in pros) { str1.Append(p.Name + ",");//拼接字段 } strSql.Append("select top 1 " + str1.ToString().Trim(',')); strSql.Append(" from MS_Order"); strSql.Append(" where o_id=@id"); SqlParameter[] parameters = { new SqlParameter("@id", SqlDbType.VarChar, 11) }; parameters[0].Value = id; DataTable dt = DbHelperSQL.Query(strSql.ToString(), parameters).Tables[0]; if (dt.Rows.Count > 0) { return(DataRowToModel(dt.Rows[0])); } else { return(null); } }
/// <summary> /// 更新一条数据 /// </summary> public bool Update(Model.Order model) { StringBuilder strSql = new StringBuilder(); StringBuilder str1 = new StringBuilder(); //利用反射获得属性的所有公共属性 PropertyInfo[] pros = model.GetType().GetProperties(); List <SqlParameter> paras = new List <SqlParameter>(); strSql.Append("update MS_Order set "); foreach (PropertyInfo pi in pros) { //如果不是主键则追加sql字符串 if (!pi.Name.Equals("o_id")) { //判断属性值是否为空 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 o_id=@id "); paras.Add(new SqlParameter("@id", model.o_id)); return(DbHelperSQL.ExecuteSql(strSql.ToString(), paras.ToArray()) > 0); }
/// <summary> /// 将对象转换实体 /// </summary> public Model.Order DataRowToModel(DataRow row) { Model.Order model = new Model.Order(); if (row != null) { //利用反射获得属性的所有公共属性 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);//用索引值设置属性值 } } } #region 订单人员====================== StringBuilder strSql2 = new StringBuilder(); strSql2.Append("select * from MS_OrderPerson"); strSql2.Append(" where op_oid=@oid"); SqlParameter[] parameters2 = { new SqlParameter("@oid", SqlDbType.VarChar, 20) }; parameters2[0].Value = model.o_id; DataTable dt2 = DbHelperSQL.Query(strSql2.ToString(), parameters2).Tables[0]; if (dt2.Rows.Count > 0) { int rowsCount = dt2.Rows.Count; List <Model.OrderPerson> models = new List <Model.OrderPerson>(); Model.OrderPerson modelt; for (int n = 0; n < rowsCount; n++) { modelt = new Model.OrderPerson(); Type modeltType = modelt.GetType(); for (int i = 0; i < dt2.Rows[n].Table.Columns.Count; i++) { PropertyInfo proInfo = modeltType.GetProperty(dt2.Rows[n].Table.Columns[i].ColumnName); if (proInfo != null && dt2.Rows[n][i] != DBNull.Value) { proInfo.SetValue(modelt, dt2.Rows[n][i], null); } } models.Add(modelt); } model.personlist = models; } #endregion return(model); }
/// <summary> /// 增加一条数据 /// </summary> public bool AddOrder(Model.Order model) { #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 MS_Order("); foreach (PropertyInfo pi in pros) { //拼接字段,忽略List<T> if (!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(") "); #endregion bool result = false; using (SqlConnection conn = new SqlConnection(DbHelperSQL.connectionString)) { conn.Open();//打开数据连接 using (SqlTransaction trans = conn.BeginTransaction()) { try { #region 插入订单表====================== int obj = DbHelperSQL.ExecuteSql(conn, trans, strSql.ToString(), paras.ToArray()); #endregion #region 插入人员表========================== if (obj > 0) { string sql = "insert into MS_OrderPerson(op_oid,op_type,op_number,op_name,op_area,op_dstatus,op_addTime) values(@oid,@type,@number,@name,@area,@status,@addtime)"; foreach (OrderPerson person in model.personlist) { person.op_oid = model.o_id; SqlParameter[] meter = new SqlParameter[] { new SqlParameter("@oid", SqlDbType.VarChar, 11), new SqlParameter("@type", SqlDbType.TinyInt, 4), new SqlParameter("@number", SqlDbType.VarChar, 5), new SqlParameter("@name", SqlDbType.VarChar, 20), new SqlParameter("@area", SqlDbType.VarChar, 2), new SqlParameter("@status", SqlDbType.TinyInt, 4), new SqlParameter("@addtime", SqlDbType.DateTime, 20) }; meter[0].Value = person.op_oid; meter[1].Value = person.op_type; meter[2].Value = person.op_number; meter[3].Value = person.op_name; meter[4].Value = person.op_area; meter[5].Value = person.op_dstatus; meter[6].Value = person.op_addTime; DbHelperSQL.ExecuteSql(conn, trans, sql, meter); } trans.Commit(); result = true; } #endregion } catch (Exception err) { trans.Rollback(); //回滚事务 result = false; } } } return(result); }