コード例 #1
0
ファイル: DbAcess.cs プロジェクト: wpmyj/EicWebPlatform
 /// <summary>
 /// 将实体模型数据添加到文件中,文件中会记录实体模型对应的表名称
 /// </summary>
 /// <typeparam name="TEntity"></typeparam>
 /// <param name="entity"></param>
 /// <param name="filePath"></param>
 public static void AppendFile <TEntity>(TEntity entity, string filePath) where TEntity : class, new()
 {
     try
     {
         Type           tity = entity.GetType();
         PropertyInfo[] Pis  = tity.GetProperties();
         if (Pis.Length > 0)
         {
             StringBuilder sb        = new StringBuilder();
             string        tableName = GetTableNameFrom <TEntity>(entity);
             if (!File.Exists(filePath))
             {
                 sb.AppendLine(string.Format("leeTableName:{0}", tableName));
             }
             foreach (PropertyInfo pi in Pis)
             {
                 if (pi.Name.ToUpper() == "ENTITYSTATE" || pi.Name.ToUpper() == "ENTITYKEY" || pi.Name.ToUpper() == "ID_KEY")
                 {
                 }
                 else
                 {
                     object piProxyValue = pi.GetValue(entity, null);
                     sb.AppendFormat("{0}*{1},", pi.Name, piProxyValue);
                 }
             }
             filePath.AppendFile(sb.ToString().TrimEnd(','));
         }
     }
     catch (Exception ex)
     {
         ErrorMessageTracer.LogErrorMsgToFile("AppendFile<TEntity>", ex);
     }
 }
コード例 #2
0
ファイル: DbAcess.cs プロジェクト: wpmyj/EicWebPlatform
        /// <summary>
        /// 从文件中读取内容转换为实体字典对象,包含表的名称
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static List <Dictionary <string, object> > GetEntityDicFromTxtFile(string filePath)
        {
            List <Dictionary <string, object> > listDatas = new List <Dictionary <string, object> >();

            if (!File.Exists(filePath))
            {
                ErrorMessageTracer.LogMsgToFile("GetEntityDicFromTxtFile", string.Format("{0}不存在!", filePath));
                return(listDatas);
            }
            try
            {
                using (StreamReader sr = new StreamReader(filePath, Encoding.Default))
                {
                    Dictionary <string, object> dataDic = null;
                    string   line      = sr.ReadLine();
                    string[] tblFields = line.Split(':');
                    if (tblFields.Length != 2)
                    {
                        ErrorMessageTracer.LogMsgToFile("GetEntityDicFromTxtFile", "文件中没有包含表名称的信息");
                        return(listDatas);
                    }
                    if (tblFields[0].ToString().ToUpper() != "LEETABLENAME")
                    {
                        ErrorMessageTracer.LogMsgToFile("GetEntityDicFromTxtFile", "文件中没有包含表名称键值的信息");
                        return(listDatas);
                    }
                    string tblKey  = tblFields[0].ToString().Trim();
                    string tblName = tblFields[1].ToString().Trim();
                    while (!string.IsNullOrEmpty(line))
                    {
                        line = sr.ReadLine();
                        string[] fieldPair = line.Split(',');
                        if (fieldPair.Length > 0)
                        {
                            dataDic = new Dictionary <string, object>();
                            dataDic.Add(tblKey, tblName);
                            foreach (string fp in fieldPair)
                            {
                                string[] fv = fp.Split('*');
                                if (fv.Length == 2)
                                {
                                    dataDic.Add(fv[0].Trim(), fv[1].Trim());
                                }
                            }
                            listDatas.Add(dataDic);
                        }
                    }
                }
            }
            catch (System.Exception ex)
            {
                ErrorMessageTracer.LogErrorMsgToFile("GetEntityDicFromFile", ex);
            }
            return(listDatas);
        }