コード例 #1
0
ファイル: GpsDataHelper.cs プロジェクト: hhahh2011/CH.Gps
        private static void InsertBatchHistoryData(List<GPSDataEntity> entityList)
        {
            HistoryStoreConfig config = CacheUtility.GetHistoryConfigByVehicleCodeFromHT(entityList[0].VehicleCode.ToString());
            if (config == null)
            {
                string log = string.Format("PES.GPS.GPSStorage.GPSDataHelper.InsertHistoryData:车辆编号为{0}找不到历史存储表。", entityList[0].VehicleCode);
                Loggers.LogInfo(log);
                return;
            }
            else
            {
                Loggers.LogInfo("数据库连接:" + config.AddConStr);
            }
            DbConnection dbConnection = null;
            string tableName = config.TableName;
            int beginIndex = 0;
            int endIndex = Math.Min(ExecuteSQLCount, entityList.Count);
            SQLCreator sqlCreator = new SQLCreator();
            //将list中的数据写入数据库,每次写入IntMinExcuteCount条,直到将list中的数据全部写入为止 
            StringBuilder stringBuilder = null;
            try
            {
                List<List<GPSDataEntity>> lists = new List<List<GPSDataEntity>>();

                if (entityList.Count <= ExecuteSQLCount)
                    lists.Add(entityList);
                else
                {
                    int count = entityList.Count / ExecuteSQLCount;
                    int orthercount = entityList.Count % ExecuteSQLCount;
                    for (int i = 0; i < count; i++)
                    {
                        List<GPSDataEntity> tempEntityList = null;
                        GPSDataEntity[] gpsdatas = new GPSDataEntity[ExecuteSQLCount];
                        entityList.CopyTo(i * ExecuteSQLCount, gpsdatas, 0, ExecuteSQLCount);
                        tempEntityList = new List<GPSDataEntity>(gpsdatas);
                        lists.Add(tempEntityList);
                    }
                    if (orthercount != 0)
                    {
                        int orthercountx = entityList.Count - count * ExecuteSQLCount;
                        GPSDataEntity[] gpsdatas = new GPSDataEntity[orthercountx];
                        entityList.CopyTo(count * ExecuteSQLCount, gpsdatas, 0, orthercountx);
                        List<GPSDataEntity> tempEntityList = new List<GPSDataEntity>(gpsdatas);
                        lists.Add(tempEntityList);
                    }
                }
                Logger.Warn("PES.GPS.GPSStorage.GPSDataHelper.InsertHistoryData   ConnStr=" + config.AddConStr.Trim());
                dbConnection = GetConnection(config.AddConStr.Trim());
                if (dbConnection == null)
                    return;
                if (dbConnection.State != ConnectionState.Open)
                    dbConnection.Open();
                foreach (List<GPSDataEntity> entitys in lists)
                {
                    stringBuilder = sqlCreator.BuildInsertHistSQL(config, entitys);
                    Logger.Warn("savehistorydata-Sql:" + stringBuilder.ToString());
                    if (stringBuilder != null)
                    {
                        DbTransaction trans = dbConnection.BeginTransaction();
                        DbCommand cmd = dbConnection.CreateCommand();
                        cmd.CommandText = stringBuilder.ToString();
                        cmd.Transaction = trans;
                        try
                        {
                            cmd.ExecuteNonQuery();
                            trans.Commit();
                            Loggers.LogInfo("PES.GPS.GPSStorage.GPSDataHelper.InsertHistoryData Commit");
                        }
                        catch (Exception ex)
                        {
                            trans.Rollback();
                            Loggers.LogInfo("PES.GPS.GPSStorage.GPSDataHelper.InsertHistoryData rollback:" + ex);
                        }
                    }
                    stringBuilder = null;
                }
            }
            catch (Exception ex)
            {
                Logger.Warn("PES.GPS.GPSStorage.GPSDataHelper.InsertHistoryData error:" + stringBuilder.ToString());
                Logger.Warn("PES.GPS.GPSStorage.GPSDataHelper.InsertHistoryData error:" + ex);
            }
            finally
            {
                CloseDBConnection(dbConnection);
            }

        }
コード例 #2
0
ファイル: GpsDataHelper.cs プロジェクト: hhahh2011/CH.Gps
        /// <summary> 保存单个车辆的历史记录
        /// 
        /// </summary>
        private static void InsertHistoryData(GPSDataEntity entity)
        {
            List<GPSDataEntity> entityList = new List<GPSDataEntity>();
            entityList.Add(entity);

            HistoryStoreConfig config = CacheUtility.GetGPSHistoryStoreTable(entity.VehicleCode.ToString());
            if (config == null)
            {
                string log = string.Format("PES.GPS.GPSStorage.GPSDataHelper.InsertHistoryData:车辆编号为{0}找不到历史存储表。", entity.VehicleCode);
                Loggers.LogInfo(log);
                return;
            }
            //DbConnection dbConnection = GetHistoryConnection(config);
            DbConnection dbConnection = null;
            string tableName = config.TableName;

            SQLCreator sqlCreator = new SQLCreator();
            //将list中的数据写入数据库,每次写入IntMinExcuteCount条,直到将list中的数据全部写入为止 
            StringBuilder stringBuilder = null;
            try
            {
                stringBuilder = sqlCreator.BuildInsertHistSQL(config, entityList);
                Logger.Warn("PES.GPS.GPSStorage.GPSDataHelper.InsertHistoryData:" + stringBuilder.ToString());
                if (stringBuilder != null)
                {
                    dbConnection = GetConnection(EnumDBOperationType.HistoryDataDB);
                    if (dbConnection == null)
                        return;
                    if (dbConnection.State != ConnectionState.Open)
                        dbConnection.Open();
                    DbTransaction trans = dbConnection.BeginTransaction();

                    DbCommand cmd = dbConnection.CreateCommand();
                    cmd.Transaction = trans;
                    cmd.CommandText = stringBuilder.ToString();
                    try
                    {
                        cmd.ExecuteNonQuery();
                        trans.Commit();
                    }
                    catch (Exception ex)
                    {
                        trans.Rollback();
                        Loggers.LogInfo("PES.GPS.GPSStorage.GPSDataHelper.InsertHistoryData rollback:" + ex);
                    }
                }
            }
            catch (Exception ex)
            {
                Loggers.LogInfo("PES.GPS.GPSStorage.GPSDataHelper.InsertHistoryData error:" + stringBuilder.ToString());
                Loggers.LogInfo("PES.GPS.GPSStorage.GPSDataHelper.InsertHistoryData error:" + ex);
            }
            finally
            {
                stringBuilder = null;
                CloseDBConnection(dbConnection);
            }
        }
コード例 #3
0
ファイル: FetchData.cs プロジェクト: hhahh2011/CH.Gps
        /// <summary>
        /// 保存历史表数据
        /// </summary>
        /// <param name="entityList"></param>
        /// <param name="HistoryConfigHT"></param>
        public void InsertHistoryData(List<GPSDataEntity> entityList, IDictionary<string, HistoryStoreConfig> HistoryConfigHT)
        {
            Dictionary<string, List<GPSDataEntity>> HistoryDataDict = new Dictionary<string, List<GPSDataEntity>>();
            DbConnection dbConnection = null;

            foreach (GPSDataEntity entity in entityList)
            {
                string vehicleCode = entity.VehicleCode.ToString();
                if (string.IsNullOrEmpty(vehicleCode))
                {
                    Logger.Info(string.Format("GPSCODE[{0}]InsertHistoryData(List<GPSDataEntity> entityList):找不到车辆编号.", entity.GPSCode));
                    continue;
                }

                HistoryStoreConfig config = HistoryConfigHT[vehicleCode];
                if (config == null) continue;
                if (HistoryDataDict.ContainsKey(config.TableName))
                {
                    List<GPSDataEntity> list = HistoryDataDict[config.TableName];
                    list.Add(entity);
                }
                else
                {
                    List<GPSDataEntity> list = new List<GPSDataEntity>();
                    list.Add(entity);
                    HistoryDataDict.Add(config.TableName, list);
                }
            }

            foreach (string key in HistoryDataDict.Keys)
            {
                //InsertBatchHistoryData(HistoryDataDict[key].ToList<GPSDataEntity>());
                List<GPSDataEntity> HistoryList = HistoryDataDict[key];


                HistoryStoreConfig config = HistoryConfigHT[HistoryList[0].VehicleCode.ToString()];
                if (config == null)
                {
                    string log = string.Format("PES.GPS.GPSStorage.GPSDataHelper.InsertHistoryData:车辆编号为{0}找不到历史存储表。", entityList[0].VehicleCode);
                    Logger.Info(log);
                    return;
                }

                SQLCreator sqlCreator = new SQLCreator();
                StringBuilder stringBuilder = sqlCreator.BuildInsertHistSQL(config, HistoryList);
                if (stringBuilder == null)
                {
                    continue;
                }

                using (dbConnection = GetConnection(config.AddConStr.Trim()))
                {
                    try
                    {
                        DbCommand cmd = dbConnection.CreateCommand();
                        cmd.CommandText = stringBuilder.ToString();
                        cmd.ExecuteNonQuery();
                    }
                    catch (Exception ex)
                    {
                        //Liuhuaihai Update on 2012/4/25                        
                        try
                        {
                            string commandTxt = stringBuilder.ToString();
                            Logger.Trace("InsertHistoryData, before insert into history: " + commandTxt);
                            string[] splitCommandTxt = commandTxt.Split(',');
                            string reportTimeString = splitCommandTxt[22].Substring(1, splitCommandTxt[22].Length - 2);
                            DateTime reportTime = DateTime.Parse(reportTimeString);
                            reportTime = reportTime.AddSeconds(1);
                            commandTxt = commandTxt.Replace(splitCommandTxt[22], "'" + reportTime.ToString() + "'");
                            Logger.Trace("InsertHistoryData, after insert into history: " + commandTxt);
                            DbCommand cmd = dbConnection.CreateCommand();
                            cmd.CommandText = commandTxt;
                            cmd.ExecuteNonQuery();
                        }
                        catch (Exception ex2)
                        {
                            Logger.Error(ex2);
                        }
                        //Logger.Error(ex);
                        //
                    }
                }
            }
        }