using System; using System.Data; using System.Collections.Generic; using System.Text; using DSWeb.MvcShipping.Models.MsCodeSource; using Microsoft.Practices.EnterpriseLibrary.Data; using DSWeb.EntityDA; using DSWeb.Areas.CommMng.Models; using HcUtility.Comm; namespace DSWeb.MvcShipping.DAL.MsCodeSourceDAL { public class MsCodeSourceDAL { #region Inquery DataList static public List GetDataList(string strCondition) { var strSql = new StringBuilder(); strSql.Append("SELECT "); strSql.Append("SOURCEID,SOURCENAME,CORPID,SOURCECODE,ENAME"); strSql.Append(" from code_source "); if (!string.IsNullOrEmpty(strCondition)) { strSql.Append(" where " + strCondition); } strSql.Append(" order by SOURCECODE"); return SetData(strSql); } static public MsCodeSource GetData(string condition) { var list = GetDataList(condition); if (list.Count > 0) return list[0]; return new MsCodeSource(); } private static List SetData(StringBuilder strSql) { var headList = new List(); Database db = DatabaseFactory.CreateDatabase(); using (IDataReader reader = db.ExecuteReader(CommandType.Text, strSql.ToString())) { while (reader.Read()) { MsCodeSource data = new MsCodeSource(); #region Set DB data to Object data.SOURCEID = Convert.ToString(reader["SOURCEID"]); data.SOURCENAME = Convert.ToString(reader["SOURCENAME"]); data.CORPID = Convert.ToString(reader["CORPID"]); data.SOURCECODE = Convert.ToString(reader["SOURCECODE"]); data.ENAME = Convert.ToString(reader["ENAME"]); #endregion headList.Add(data); } reader.Close(); } return headList; } #endregion #region 明细表 static public List GetBodyList(string strCondition) { var strSql = new StringBuilder(); strSql.Append("SELECT "); strSql.Append("SOURCEDETAILID,SOURCEID,SOURCEDETAIL,ENAME"); strSql.Append(" from code_source_detail "); if (strCondition.Trim() != String.Empty) { strSql.Append(" where " + strCondition); } return SetBodyData(strSql); } private static List SetBodyData(StringBuilder strSql) { var bodyList = new List(); Database db = DatabaseFactory.CreateDatabase(); using (IDataReader reader = db.ExecuteReader(CommandType.Text, strSql.ToString())) { while (reader.Read()) { MsCodeSourceDetail data = new MsCodeSourceDetail(); #region Set DB data to Object data.SOURCEDETAILID = Convert.ToString(reader["SOURCEDETAILID"]); data.SOURCEID = Convert.ToString(reader["SOURCEID"]); data.SOURCEDETAIL = Convert.ToString(reader["SOURCEDETAIL"]); data.ENAME = Convert.ToString(reader["ENAME"]); #endregion bodyList.Add(data); } reader.Close(); } return bodyList; } public static DBResult SaveDetail(MsCodeSource headData, List bodyList) { var result = new DBResult(); Database db = DatabaseFactory.CreateDatabase(); using (var conn = db.CreateConnection()) { conn.Open(); var tran = conn.BeginTransaction(); try { var cmdDelete = db.GetSqlStringCommand("delete from code_source_detail where SOURCEID='" + headData.SOURCEID + "'"); db.ExecuteNonQuery(cmdDelete, tran); var cmdInsert = db.GetSqlStringCommand( @"insert into code_source_detail (SOURCEID, SOURCEDETAILID, ENAME,SOURCEDETAIL) values (@SOURCEID, @SOURCEDETAILID, @ENAME,@SOURCEDETAIL) "); if (bodyList != null) { foreach (var enumValue in bodyList) { cmdInsert.Parameters.Clear(); db.AddInParameter(cmdInsert, "@SOURCEID", DbType.String, headData.SOURCEID); db.AddInParameter(cmdInsert, "@SOURCEDETAILID", DbType.String, enumValue.SOURCEDETAILID); db.AddInParameter(cmdInsert, "@ENAME", DbType.String, enumValue.ENAME); db.AddInParameter(cmdInsert, "@SOURCEDETAIL", DbType.String, enumValue.SOURCEDETAIL); db.ExecuteNonQuery(cmdInsert, tran); } } tran.Commit(); } catch (Exception e) { tran.Rollback(); result.Success = false; result.Message = "保存出现错误,请重试或联系系统管理员"+e.Message; return result; } } result.Success = true; result.Message = "保存成功"; return result; } #endregion } }