Example #1
0
        public QueryResultList <TInfo> GetItems(SearchCondition search)
        {
            QueryResultList <TInfo> result;

            try
            {
                LDB_DataContext ldb = LDB_DataContextFactory.CreateLDB(ConnectStr);
                List <TInfo>    infoes;
                if (search != null)
                {
                    infoes = GetingItems(ldb, search);
                }
                else
                {
                    infoes = GetingAllItems(ldb);
                }
                result = new QueryResultList <TInfo>(ResultCode.Successful, successMsg, infoes);
            }
            catch (Exception ex)
            {
                result = new QueryResultList <TInfo>(ResultCode.Fail, "从数据库获取数据时发生错误!", new List <TInfo>());
                ExceptionPolicy.HandleException(ex, this.GetType().Name + "." + "GetItems()");
            }
            return(result);
        }
Example #2
0
        public CommandResult Insert(TInfo info)
        {
            CommandResult result;

            try
            {
                LDB_DataContext ldb = LDB_DataContextFactory.CreateLDB(ConnectStr);
                InsertingItem(info, ldb);
                ldb.SubmitChanges();
                result = new CommandResult(ResultCode.Successful, successMsg);
            }
            catch (System.Data.SqlClient.SqlException ex)
            {
                if (ex.Number == 2627)
                {
                    result = new CommandResult(ResultCode.Fail, "不能插入重复ID的数据!");
                }
                else
                {
                    result = new CommandResult(ResultCode.Fail, "数据在写入数据库时发生错误!");
                }
                ExceptionPolicy.HandleException(ex, this.GetType().Name + "." + "Insert()");
            }
            catch (Exception ex)
            {
                result = new CommandResult(ResultCode.Fail, "数据在写入数据库时发生错误!");
                ExceptionPolicy.HandleException(ex, this.GetType().Name + "." + "Insert()");
            }
            return(result);
        }
Example #3
0
        public CommandResult Delete(TInfo info)
        {
            CommandResult result;

            try
            {
                LDB_DataContext ldb = LDB_DataContextFactory.CreateLDB(ConnectStr);
                DeletingItem(info, ldb);
                ldb.SubmitChanges();
                result = new CommandResult(ResultCode.Successful, successMsg);
            }
            catch (Exception ex)
            {
                result = new CommandResult(ResultCode.Fail, "从数据库删除数据时发生错误!");
                ExceptionPolicy.HandleException(ex, this.GetType().FullName + "." + "Delete()");
            }
            return(result);
        }
Example #4
0
        public CommandResult Update(TInfo newVal, TInfo original)
        {
            CommandResult result;

            try
            {
                LDB_DataContext ldb = LDB_DataContextFactory.CreateLDB(ConnectStr);
                UpdatingItem(newVal, original, ldb);
                ldb.SubmitChanges();
                result = new CommandResult(ResultCode.Successful, successMsg);
            }
            catch (Exception ex)
            {
                result = new CommandResult(ResultCode.Fail, "数据在写入数据库时发生错误!");
                ExceptionPolicy.HandleException(ex, this.GetType().FullName + "." + "Update()");
            }
            return(result);
        }
Example #5
0
        public QueryResult <TInfo> GetByID(TID id)
        {
            QueryResult <TInfo> result;

            try
            {
                LDB_DataContext ldb  = LDB_DataContextFactory.CreateLDB(ConnectStr);
                TInfo           info = GetingItemByID(id, ldb);
                if (info != null)
                {
                    result = new QueryResult <TInfo>(ResultCode.Successful, successMsg, info);
                }
                else
                {
                    result = new QueryResult <TInfo>(ResultCode.Fail, string.Format("没有找到ID={0}的数据!", id.ToString()), info);
                }
            }
            catch (Exception ex)
            {
                result = new QueryResult <TInfo>(ResultCode.Fail, "从数据库获取数据时发生错误!", null);
                ExceptionPolicy.HandleException(ex, this.GetType().Name + "." + "GetByID()");
            }
            return(result);
        }
Example #6
0
 public LDB_UnitWork(string connStr)
 {
     _LDB = LDB_DataContextFactory.CreateLDB(connStr);
 }