public static void LoadLiPinMaFromFile(DBManager dbMgr, bool toAppend = false) { try { if (File.Exists("./礼品码_导入文件.txt")) { StreamReader sr = new StreamReader("./礼品码_导入文件.txt", Encoding.GetEncoding("gb2312")); if (null != sr) { if (!toAppend) { LiPinMaManager._LiPinMaDict = null; DBWriter.ClearAllLiPinMa(dbMgr); } Dictionary <string, LiPinMaItem> liPinMaDict = new Dictionary <string, LiPinMaItem>(); string line; while ((line = sr.ReadLine()) != null) { if (!string.IsNullOrEmpty(line)) { string[] sa = line.Split(new char[] { ' ' }); if (sa.Length == 5) { DBWriter.InsertNewLiPinMa(dbMgr, sa[0], sa[1], sa[2], sa[3], sa[4], "0"); LiPinMaItem liPinMaItem = new LiPinMaItem { LiPinMa = sa[0], HuodongID = Convert.ToInt32(sa[1]), MaxNum = Convert.ToInt32(sa[2]), UsedNum = 0, PingTaiID = Convert.ToInt32(sa[3]), PingTaiRepeat = Convert.ToInt32(sa[4]) }; liPinMaDict[liPinMaItem.LiPinMa] = liPinMaItem; } } } sr.Close(); if (!toAppend || null == LiPinMaManager._LiPinMaDict) { LiPinMaManager._LiPinMaDict = liPinMaDict; } else { Dictionary <string, LiPinMaItem> oldLiPinMaDict = LiPinMaManager._LiPinMaDict; foreach (string key in liPinMaDict.Keys) { LiPinMaItem liPinMaItem = liPinMaDict[key]; lock (LiPinMaManager.Mutex) { oldLiPinMaDict[key] = liPinMaItem; } } } File.Delete("./礼品码_导入文件.txt"); } } } catch (Exception ex) { DataHelper.WriteFormatExceptionLog(ex, "", false, false); } }
/// <summary> /// 从文件中重新导入礼品码(会清空现有的数据库中的礼品码的记录) /// </summary> /// <param name="dbMgr"></param> public static void LoadLiPinMaFromFile(DBManager dbMgr, bool toAppend = false) { try { //判断新的礼品码文件是否存在 if (!File.Exists("./礼品码_导入文件.txt")) { return; } StreamReader sr = new StreamReader("./礼品码_导入文件.txt", Encoding.GetEncoding("gb2312")); if (null == sr) { return; } //如果不是追加,则清空原有数据 if (!toAppend) { //先清空原有的礼品码 _LiPinMaDict = null; //清空礼品码的数据 DBWriter.ClearAllLiPinMa(dbMgr); } //查询礼品码 Dictionary <string, LiPinMaItem> liPinMaDict = new Dictionary <string, LiPinMaItem>(); string line = null; while ((line = sr.ReadLine()) != null) { if (string.IsNullOrEmpty(line)) { continue; } string[] sa = line.Split(' '); if (sa.Length != 5) { continue; } //插入一个新的礼品码的数据 DBWriter.InsertNewLiPinMa(dbMgr, sa[0], sa[1], sa[2], sa[3], sa[4]); LiPinMaItem liPinMaItem = new LiPinMaItem() { LiPinMa = sa[0], HuodongID = Convert.ToInt32(sa[1]), MaxNum = Convert.ToInt32(sa[2]), UsedNum = 0, PingTaiID = Convert.ToInt32(sa[3]), PingTaiRepeat = Convert.ToInt32(sa[4]), }; liPinMaDict[liPinMaItem.LiPinMa] = liPinMaItem; } sr.Close(); sr = null; //如果不是追加,则清空原有数据 if (!toAppend || null == _LiPinMaDict) { //重新赋值 _LiPinMaDict = liPinMaDict; } else { Dictionary <string, LiPinMaItem> oldLiPinMaDict = _LiPinMaDict; foreach (var key in liPinMaDict.Keys) { LiPinMaItem liPinMaItem = liPinMaDict[key]; //防止多用户重入 lock (Mutex) { oldLiPinMaDict[key] = liPinMaItem; } } } //将已经导入的礼品码文件删除 System.IO.File.Delete("./礼品码_导入文件.txt"); } catch (Exception ex) { //System.Windows.Application.Current.Dispatcher.Invoke((MethodInvoker)delegate //{ // 格式化异常错误信息 DataHelper.WriteFormatExceptionLog(ex, "", false); //throw ex; //}); } }