/// <summary> /// 得到一个对象实体 /// </summary> public Model.Customer GetModel(int id) { StringBuilder strSql = new StringBuilder(); StringBuilder str1 = new StringBuilder(); Model.Customer model = new Model.Customer(); //利用反射获得属性的所有公共属性 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_Customer"); strSql.Append(" where c_id=@id"); SqlParameter[] parameters = { new SqlParameter("@id", SqlDbType.Int, 4) }; 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.Customer model, SqlConnection conn = null, SqlTransaction tran = null) { StringBuilder strSql = new StringBuilder(); StringBuilder str1 = new StringBuilder(); //利用反射获得属性的所有公共属性 PropertyInfo[] pros = model.GetType().GetProperties(); List <SqlParameter> paras = new List <SqlParameter>(); strSql.Append("update MS_Customer set "); foreach (PropertyInfo pi in pros) { //如果不是主键则追加sql字符串 if (!pi.Name.Equals("c_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 c_id=@id "); paras.Add(new SqlParameter("@id", model.c_id)); if (tran == null) { return(DbHelperSQL.ExecuteSql(strSql.ToString(), paras.ToArray()) > 0); } else { return(DbHelperSQL.ExecuteSql(conn, tran, strSql.ToString(), paras.ToArray()) > 0); } }
/// <summary> /// 将对象转换实体 /// </summary> public Model.Customer DataRowToModel(DataRow row) { Model.Customer model = new Model.Customer(); 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);//用索引值设置属性值 } } } return(model); }
public string insertData(Model.Customer model, Model.Contacts contact, SqlConnection conn, SqlTransaction trans, out int cid) { cid = 0; DataSet ds = null; #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_Customer("); foreach (PropertyInfo pi in pros) { //如果不是主键则追加sql字符串 if (!pi.Name.Equals("c_id")) { //判断属性值是否为空 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;"); #endregion #region 添加客户========================== int obj = 0; obj = Convert.ToInt32(DbHelperSQL.GetSingle(conn, trans, strSql.ToString(), paras.ToArray()));//带事务 #endregion #region 添加联系人====================== if (obj > 0) { //检查联系人号码是否与其他客户的联系人号码一样 if (model.c_type != 2) { string sql = "select * from MS_customer c left join MS_contacts co on c.c_id = co.co_cid where c.c_type<>2 and co.co_number='" + contact.co_number + "'"; ds = DbHelperSQL.Query(conn, trans, sql); if (ds != null && ds.Tables[0].Rows.Count > 0) { return("客户【" + ds.Tables[0].Rows[0]["c_name"] + "】,联系人【" + ds.Tables[0].Rows[0]["co_name"] + "】的联系号码也是【" + contact.co_number + "】,请查实:是否为同一客户!"); } } cid = obj; contact.co_cid = obj; int oobj = new DAL.Contacts().Add(contact, conn, trans); if (oobj > 0) { return(""); } } #endregion return("添加失败"); }