using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Common.Config { /// /// 基于XML的自定义数据操作 /// public class CustomDataBaseUtil { public static string CXPATH_BUFFER_FILE_PATH = System.Configuration.ConfigurationManager.AppSettings["BufferFilePath"].ToString(); public const string GLOBAL_CONFIGUATION_FILE_PATH = "app.config"; /// /// 缓存表的主键 /// public const string CXPATH_BUFFER_TABLE_KEY = "Configuration/CommonConfiguration/BufferTableKey"; /// /// 缓存表的主键2 /// public const string CXPATH_BUFFER_TABLE_KEY2 = "Configuration/CommonConfiguration/BufferTableKey2"; /// /// 返回数据文件是否存在 /// /// /// public static bool IsDBFileExists(string fileName) { //"D:\\T99_FileServer\\'" + user + "'.buf" #region //if (System.IO.File.Exists(Config.ConfigurationUtil.ReadConfig(CXPATH_BUFFER_FILE_PATH) + @"\" + priUser + ".buf")) if (System.IO.File.Exists("D:\\T99_FileServer\\'" + fileName + "'.buf")) { return true; } return false; #endregion } /// /// 获取缓存数据 /// /// /// internal static System.Data.DataTable GetData(string fileName) { #region if (!IsDBFileExists(fileName)) { return null; } System.Data.DataTable table = null; using (System.Data.DataSet ds = new System.Data.DataSet()) { ds.ReadXml("D:\\T99_FileServer\\'" + fileName + "'.buf", System.Data.XmlReadMode.ReadSchema); table = ds.Tables[0].Copy(); } return table; #endregion } /// /// 删除缓存中指定项 /// /// /// public static void DeleteData(string fileName, string[] key) { #region using (System.Data.DataTable table = GetData(fileName)) { if (table != null) { System.Data.DataRow row = table.Rows.Find(key); if (row != null) { //移除数据 table.Rows.Remove(row); if (table.Rows.Count == 0) { //删除空文件 DeleteFile(fileName); } else { //保存文件 SaveFile(fileName, table); } } else { //缓存中不存在要删除的数据 //throw new Exception(Config.ConfigurationUtil.ReadConfig("缓存中不存在要删除的数据")); //throw new Exception("缓存中不存在要删除的数据"); return; } } else { //缓存文件不存在 //throw new Exception(Config.ConfigurationUtil.ReadConfig("缓存文件不存在")) //throw new Exception("缓存文件不存在"); return; } } #endregion } /// /// 返回指定项是否存在 /// /// /// /// public static bool IsItemExists(string fileName, string[] key) { #region using (System.Data.DataTable table = GetData(fileName)) { if (table != null) { System.Data.DataRow row = table.Rows.Find(key); if (row != null) { return true; } } } return false; #endregion } /// /// 保存缓存文件 /// /// /// internal static void SaveFile(string fileName, System.Data.DataTable data) { #region using (System.Data.DataSet ds = new System.Data.DataSet()) { //添加数据 ds.Tables.Add(data.Copy()); //删除缓存文件 DeleteFile(fileName); //保存文件 ds.WriteXml("D:\\T99_FileServer\\'" + fileName + "'.buf", System.Data.XmlWriteMode.WriteSchema); } #endregion } /// /// 添加数据项 /// /// 文件名 /// /// public void AddItem(string fileName, System.Data.DataTable data, int parmCount) { #region for (int i = data.Rows.Count - 1; i >= 0; i--) { string key1 = data.Rows[i][Common.Config.ConfigurationUtil.ReadConfig(CXPATH_BUFFER_TABLE_KEY)].ToString(); if (parmCount == 2) { string key2 = data.Rows[i][Common.Config.ConfigurationUtil.ReadConfig(CXPATH_BUFFER_TABLE_KEY2)].ToString(); string[] key = { key1, key2 }; if (IsItemExists(fileName, key)) { data.Rows.RemoveAt(i); } } else { string[] key = { key1 }; if (IsItemExists(fileName, key)) { data.Rows.RemoveAt(i); } } } using (System.Data.DataTable table = GetData(fileName)) { if (table != null) { table.Merge(data, true, System.Data.MissingSchemaAction.Ignore); SaveFile(fileName, table); } else { SaveFile(fileName, data); } } #endregion } /// /// 删除缓存文件 /// /// internal static void DeleteFile(string fileName) { #region if (IsDBFileExists(fileName)) { System.IO.File.Delete("D:\\T99_FileServer\\'" + fileName + "'.buf"); } #endregion } } }