using System; using System.Data; using System.Collections.Generic; using System.Text; using DSWeb.MvcShipping.Models.CodePackage; using Microsoft.Practices.EnterpriseLibrary.Data; using DSWeb.Areas.CommMng.Models; using HcUtility.Comm; namespace DSWeb.MvcShipping.DAL.MsCodePackage { public class MsCodePackageDAL { #region Inquery DataList static public List GetDataList(string strCondition, string sort = null) { var strSql = new StringBuilder(); strSql.Append("SELECT "); strSql.Append("GID,CEXPLAIN,PKGS,EDICODE,AFRCODE"); strSql.Append(" from code_package "); if (!string.IsNullOrEmpty(strCondition)) { strSql.Append(" WHERE " + strCondition); } var sortstring = DatasetSort.Getsortstring(sort); if (!string.IsNullOrEmpty(sortstring)) { strSql.Append(" order by " + sortstring); } else { strSql.Append(" order by PKGS"); } return SetData(strSql); } static public CodePackage GetData(string condition) { CodePackage data = null; var list = GetDataList(condition); if (list.Count > 0) data = list[0]; if (data == null) { data = new CodePackage(); } return data; } 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()) { CodePackage data = new CodePackage(); #region Set DB data to Object data.GID = Convert.ToString(reader["GID"]); data.PKGS = Convert.ToString(reader["PKGS"]); data.CEXPLAIN = Convert.ToString(reader["CEXPLAIN"]); data.EDICODE = Convert.ToString(reader["EDICODE"]); #endregion headList.Add(data); } reader.Close(); } return headList; } #endregion public static DBResult SaveDetail(List bodyList) { var result = new DBResult(); Database db = DatabaseFactory.CreateDatabase(); using (var conn = db.CreateConnection()) { conn.Open(); var tran = conn.BeginTransaction(); try { var cmdInsert = db.GetSqlStringCommand( @"insert into code_package (GID,PKGS,CEXPLAIN,EDICODE) values (@GID,@PKGS,@CEXPLAIN,@EDICODE) "); var cmdUpdate = db.GetSqlStringCommand( @"update code_package set PKGS=@PKGS,CEXPLAIN=@CEXPLAIN,EDICODE=@EDICODE where GID=@GID "); if (bodyList != null) { foreach (var enumValue in bodyList) { if (enumValue.GID == "*" || enumValue.GID == "") { cmdInsert.Parameters.Clear(); db.AddInParameter(cmdInsert, "@GID", DbType.String, Guid.NewGuid().ToString()); db.AddInParameter(cmdInsert, "@PKGS", DbType.String, enumValue.PKGS); db.AddInParameter(cmdInsert, "@CEXPLAIN", DbType.String, enumValue.CEXPLAIN); db.AddInParameter(cmdInsert, "@EDICODE", DbType.String, enumValue.EDICODE); db.ExecuteNonQuery(cmdInsert, tran); } else { cmdUpdate.Parameters.Clear(); db.AddInParameter(cmdUpdate, "@GID", DbType.String, enumValue.GID); db.AddInParameter(cmdUpdate, "@PKGS", DbType.String, enumValue.PKGS); db.AddInParameter(cmdUpdate, "@CEXPLAIN", DbType.String, enumValue.CEXPLAIN); db.AddInParameter(cmdUpdate, "@EDICODE", DbType.String, enumValue.EDICODE); db.ExecuteNonQuery(cmdUpdate, tran); } } } tran.Commit(); } catch (Exception) { tran.Rollback(); result.Success = false; result.Message = "保存出现错误,请重试或联系系统管理员"; return result; } } result.Success = true; result.Message = "保存成功" + result.Message; return result; } #region 参照部分 static public int GetRdCount(string strCondition) { var strSql = new StringBuilder(); strSql.Append("SELECT Count(GID) AS CT from code_package (NOLOCK) "); if (strCondition.Trim() != String.Empty) { strSql.Append(" where " + strCondition); } var ct = 0; Database db = DatabaseFactory.CreateDatabase(); using (IDataReader reader = db.ExecuteReader(CommandType.Text, strSql.ToString())) { while (reader.Read()) { ct = Convert.ToInt16(reader["CT"]); } reader.Close(); } return ct; } #endregion } }