using System; using System.Data; using System.Collections.Generic; using System.Text; using DSWeb.TruckMng.Models.CodeTruckPort; using Microsoft.Practices.EnterpriseLibrary.Data; using DSWeb.Areas.CommMng.Models; using HcUtility.Comm; namespace DSWeb.TruckMng.DAL.MsCodeTruckPort { public class MsCodeTruckPortDAL { #region Inquery DataList static public List GetDataList(string strCondition, string companyid, string sort = null) { var strSql = new StringBuilder(); strSql.Append("SELECT "); strSql.Append("PORTID,PORTCODE,PORT,PROVINCE,PORTCODE+'-'+PORT CODEANDNAME"); strSql.Append(" from code_truckport "); 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 PORTCODE"); } return SetData(strSql); } static public CodeTruckPort GetData(string condition, string companyid) { CodeTruckPort data = null; var list = GetDataList(condition,companyid); if (list.Count > 0) data = list[0]; if (data == null) { data = new CodeTruckPort(); } 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()) { CodeTruckPort data = new CodeTruckPort(); #region Set DB data to Object data.PORTID = Convert.ToString(reader["PORTID"]); data.PORTCODE = Convert.ToString(reader["PORTCODE"]); data.PORT = Convert.ToString(reader["PORT"]); data.PROVINCE = Convert.ToString(reader["PROVINCE"]); data.CODEANDNAME = Convert.ToString(reader["CODEANDNAME"]); #endregion headList.Add(data); } reader.Close(); } return headList; } #endregion public static DBResult SaveDetail(List bodyList, string companyid) { 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_truckport (PORTID,PORTCODE,PORT,PROVINCE) values (@PORTID,@PORTCODE,@PORT,@PROVINCE) "); var cmdUpdate = db.GetSqlStringCommand( @"update code_truckport set PORTCODE=@PORTCODE,PORT=@PORT,PROVINCE=@PROVINCE where PORTID=@PORTID "); if (bodyList != null) { foreach (var enumValue in bodyList) { if (enumValue.PORTID == "*" || enumValue.PORTID == "") { cmdInsert.Parameters.Clear(); db.AddInParameter(cmdInsert, "@PORTID", DbType.String, Guid.NewGuid().ToString()); db.AddInParameter(cmdInsert, "@PORTCODE", DbType.String, enumValue.PORTCODE); db.AddInParameter(cmdInsert, "@PORT", DbType.String, enumValue.PORT); db.AddInParameter(cmdInsert, "@PROVINCE", DbType.String, enumValue.PROVINCE); db.ExecuteNonQuery(cmdInsert, tran); } else { cmdUpdate.Parameters.Clear(); db.AddInParameter(cmdUpdate, "@PORTID", DbType.String, enumValue.PORTID); db.AddInParameter(cmdUpdate, "@PORT", DbType.String, enumValue.PORT); db.AddInParameter(cmdUpdate, "@PROVINCE", DbType.String, enumValue.PROVINCE); db.AddInParameter(cmdUpdate, "@PORTCODE", DbType.String, enumValue.PORTCODE); 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 参照部分 #endregion } }