/// <summary> /// 查询相对于的表的列名 /// </summary> /// <param name="loacalDataBase">数据库</param> /// <param name="loacaDataTable">表名</param> /// <returns>返回所有的列名</returns> private ArrayList GetCloumnByTable(string loacalDataBase, string loacaDataTable) { try { string strConnction = string.Concat(new string[] { "SERVER=", CacheGeneral.GetCache("key_localIP").ToString(), ";DATABASE=", loacalDataBase, ";UID=", CacheGeneral.GetCache("key_userAccount").ToString(), ";PWD=", CacheGeneral.GetCache("key_userPwd").ToString(), }); string strSql = string.Concat(new string[] { " SELECT COLUMN_NAME,DATA_TYPE,T.VALUE,T.IS_IDENTITY", " FROM ", loacalDataBase, ".INFORMATION_SCHEMA.COLUMNS ", " INNER JOIN (SELECT B.NAME,C.VALUE,B.IS_IDENTITY FROM SYS.TABLES A ", " INNER JOIN SYS.COLUMNS B ON A.OBJECT_ID = B.OBJECT_ID ", " INNER JOIN SYS.EXTENDED_PROPERTIES C ON C.MAJOR_ID = B.OBJECT_ID AND C.MINOR_ID = B.COLUMN_ID) T ON COLUMN_NAME=T.NAME ", " WHERE TABLE_NAME = '", loacaDataTable, "'" }); ArrayList fieldByConAndSql = dbHelper.GetFieldByConAndSql(strConnction, strSql); return(fieldByConAndSql); } catch (Exception ex) { Log4net.log4netCreate(typeof(GeneratedFileController), log4netPath) .Error("GetCloumnByTable(string loacalDataBase, string loacaDataTable) ---- > " + ex.Message); throw; } }
//合并文件 public ActionResult FileMerge() { var TempFolder = string.Empty; FileStream fileStream = null; try { TempFolder = Path.Combine(UploadPath, Request["guid"]); //临时文件夹 var filesName = System.IO.Directory.GetFiles(TempFolder); //获得下面的所有文件 var filesNamePath = Path.Combine(UploadPath, Request["fileName"]); //最终的文件名 fileStream = new FileStream(filesNamePath, FileMode.Create); foreach (var part in filesName.OrderBy(x => x.Length).ThenBy(x => x)) //排一下序,保证从0-N Write { var bytes = System.IO.File.ReadAllBytes(part); fileStream.Write(bytes, 0, bytes.Length); //删除分块 System.IO.File.Delete(part); } return(Json(new { message = "Success", content = string.Empty })); } catch (Exception ex) { Log4net.log4netCreate(typeof(FileUploadController), log4netPath) .Error("FileMerge() ----> " + ex.Message); return(Json(new { message = "Fail", content = ex.Message })); } finally { fileStream.Close(); fileStream.Dispose(); //删除文件夹 System.IO.Directory.Delete(TempFolder); } }
//登录到数据库 public JsonResult LoginDB(string userAccount, string userPwd, string localIP) { try { string strConnction = string.Concat(new string[] { "SERVER=", localIP, ";DATABASE=MASTER;UID=", userAccount, ";PWD=", userPwd }); string sqlStr = "SELECT NAME FROM SYSDATABASES ORDER BY NAME"; string message = (string)null; ArrayList dataByConAndSql = dbHelper.GetDataByConAndSql(strConnction, sqlStr, out message); if (dataByConAndSql.Count <= 0) { return(Json(new { message = "Fail", content = "没有对应的数据库 ! " })); } else { //暂时存入缓存 CacheGeneral.SetCache("key_userAccount", userAccount); CacheGeneral.SetCache("key_userPwd", userPwd); CacheGeneral.SetCache("key_localIP", localIP); return(Json(new { message = "Success", content = JsonConvert.SerializeObject(dataByConAndSql) })); } } catch (Exception ex) { //报错记录错误 Log4net.log4netCreate(typeof(GeneratedFileController), log4netPath) .Error("LoginDB(string userAccount, string userPwd, string localIP) ----> " + ex.Message); return(Json(new { message = "Fail", content = "登录失败 ! " })); } }
/// <summary> /// 写入 Txt 文件 /// </summary> /// <param name="path">写入文件路径,已写入缓存</param> /// <param name="content">内容</param> /// <returns>bool 是否成功</returns> private bool WriterTxt(string content, string path = "") { path = CacheGeneral.GetCache("TemplatePath").ToString(); if (string.IsNullOrEmpty(path)) { return(false); } StreamWriter stream = null; try { stream = new StreamWriter(path, false, Encoding.UTF8); stream.Write(content); return(true); } catch (Exception ex) { Log4net.log4netCreate(typeof(GeneratedFileController), log4netPath).Error(" WriterTxt(string path, string content) ----> " + ex.Message); return(false); } finally { stream.Close(); stream.Dispose(); } }
/// <summary> /// 读取 TxT 文件 /// </summary> /// <param name="pathTxt">要读取文件路径</param> /// <returns>string</returns> private string ReadTxt(string pathTxt) { StreamReader fileStream = null; try { string content = string.Empty; if (string.IsNullOrEmpty(pathTxt)) { return(string.Empty); } fileStream = new StreamReader(pathTxt, Encoding.UTF8); return(fileStream.ReadToEnd()); } catch (Exception ex) { Log4net.log4netCreate(typeof(GeneratedFileController), log4netPath) .Error("ReadTxt(string pathTxt) ----> " + ex.Message); return("读取模板错误 ! "); } finally { fileStream.Close(); fileStream.Dispose(); } }
/* * Application_Error * 全局捕获异常,记录到 Txt */ protected void Application_Error() { Exception lastError = Server.GetLastError(); if (lastError != null) { //异常信息 string strExceptionMessage = string.Empty; //异常类 string type = string.Empty; //出错位置 string position = string.Empty; //Log4net 配置文件路径 string log4netPath = Server.MapPath("~/App_Data/log4net/log4net.config"); strExceptionMessage = lastError.Message; type = lastError.TargetSite.DeclaringType.FullName; position = lastError.TargetSite.DeclaringType.FullName + "." + lastError.TargetSite.Name; /* * 错误记录到 Txt 文件 */ Log4net.log4netCreate(type, log4netPath).Error("【" + position + "】 全局捕获异常信息 —————————> " + strExceptionMessage); //对HTTP 404做额外处理,其他错误全部当成500服务器错误 HttpException httpError = lastError as HttpException; if (httpError != null) { //获取错误代码 int httpCode = httpError.GetHttpCode(); strExceptionMessage = httpError.Message; #region 错误 , 404 if (httpCode == 400 || httpCode == 404) { //跳转到指定的静态404信息页面 Response.StatusCode = 404; Response.Redirect("/Default/Error_404"); Server.ClearError(); return; } #endregion } /* * 跳转到指定的HTTP 500 错误信息页面 * 跳转到静态页面一定要用Response.WriteFile方法 */ Response.StatusCode = 500; //Response.Redirect("/Default/Error_500"); //一定要调用Server.ClearError() , 否则会触发错误详情页(就是黄页) Server.ClearError(); Response.Redirect("/Default/Error_500"); } }
/// <summary> /// 生成 Model 内容 /// </summary> /// <param name="loacalDataBase">数据库</param> /// <param name="loacaDataTable">表名</param> /// <returns>生成后的内容</returns> private string CreateModelFile(string loacalDataBase, string loacaDataTable) { try { //查询列名 ArrayList dbColnameAndType = GetCloumnByTable(loacalDataBase, loacaDataTable); StringBuilder stringBuilder = new StringBuilder(); foreach (string str in dbColnameAndType) { //private var arr = str.Split('|'); //切割内容 stringBuilder.Append("\t\tprivate "); stringBuilder.Append(arr[1].ToString()); //类型 stringBuilder.Append(" "); stringBuilder.Append(arr[0].ToString()); //字段 stringBuilder.AppendLine(";"); //说明 if (!string.IsNullOrEmpty(arr[2].ToString())) { stringBuilder.Append("\t\t/// <summary>"); stringBuilder.Append("\n\t\t/// " + arr[2].ToString()); stringBuilder.Append("\n\t\t/// </summary>"); } stringBuilder.Append("\n\t\t[DisplayName(\"" + arr[2].ToString() + "\")]"); //主键 if (Convert.ToBoolean(arr[3])) { stringBuilder.Append("\n\t\t[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]"); } //public stringBuilder.Append("\n\t\tpublic "); stringBuilder.Append(arr[1].ToString()); //类型 stringBuilder.Append(" "); stringBuilder.Append(arr[0].ToString().Substring(0, 1).ToUpper() + arr[0].ToString().Substring(1)); //字段 stringBuilder.Append("{"); stringBuilder.Append("\n\t\t\tget { return "); stringBuilder.Append(arr[0].ToString()); stringBuilder.Append("; }"); stringBuilder.Append("\n\t\t\tset { "); stringBuilder.Append(arr[0].ToString()); stringBuilder.Append(" = value; }"); stringBuilder.Append("\n\t\t}"); stringBuilder.AppendLine(); } return(stringBuilder.ToString()); } catch (Exception ex) { Log4net.log4netCreate(typeof(GeneratedFileController), log4netPath) .Error("CreateModelFile(string loacalDataBase, string loacaDataTable, string strNamespace) ---- > " + ex.Message); throw; } }
/// <summary> /// SURE_MENU - 获取全部信息 /// </summary> /// <returns>全部信息集合</returns> public static List <SURE_MENU> GET_SURE_MENU() { try { return(DbHelper.Database().FindList <SURE_MENU>()); } catch (Exception ex) { Log4net.log4netCreate(typeof(DEFAULTDAL), log4netPath) .Error("GET_SURE_MENU() ----> " + ex.Message); return(new List <SURE_MENU>()); } }
/// <summary> /// SURE_MENU - 修改信息 /// </summary> /// <param name="model">入参</param> /// <returns>是否成功</returns> public static int UPDATE_SURE_MENU(SURE_MENU model) { try { return(DbHelper.Database().Update <SURE_MENU>(model)); } catch (Exception ex) { Log4net.log4netCreate(typeof(DEFAULTDAL), log4netPath) .Error("UPDATE_SURE_MENU(SURE_MENU model) ----> " + ex.Message); return(0); } }
/// <summary> /// INSERT_SURE_GENERATED - 新增信息 /// </summary> /// <param name="model">入参</param> /// <returns>是否成功</returns> public static int INSERT_SURE_GENERATED(SURE_GENERATED model) { try { return(DbHelper.Database().Insert <SURE_GENERATED>(model)); } catch (Exception ex) { Log4net.log4netCreate(typeof(SURE_GENERATEDDAL), log4netPath) .Error("INSERT_SURE_GENERATED(SURE_GENERATED model) ----> " + ex.Message); return(0); } }
/// <summary> /// 创建生成文件 /// </summary> /// <param name="fileName">路径</param> /// <param name="content">内容</param> private void CreateFile(string fileName, string content) { try { string filePath = Server.MapPath(fileName); FileStream fileStream = System.IO.File.Create(filePath); //创建文件 fileStream.Close(); StreamWriter sw = new StreamWriter(filePath); //创建写入流 sw.Write(content); sw.Flush(); sw.Close(); } catch (Exception ex) { Log4net.log4netCreate(typeof(GeneratedFileController), log4netPath) .Error("CreateFile(string fileName, string content) ---- >" + ex.Message); throw; } }
//根据数据库联动表名 public JsonResult LoginTable(string localDB) { try { string strConnction = string.Concat(new string[] { "SERVER=", CacheGeneral.GetCache("key_localIP").ToString(), ";DATABASE=MASTER;UID=", CacheGeneral.GetCache("key_userAccount").ToString(), ";PWD=", CacheGeneral.GetCache("key_userPwd").ToString() }); string sqlStr = @"SELECT TABLE_NAME FROM " + localDB + ".INFORMATION_SCHEMA.TABLES ORDER BY TABLE_NAME"; string message = (string)null; ArrayList dataByConAndSql = dbHelper.GetDataByConAndSql(strConnction, sqlStr, out message); return(Json(new { message = "Success", content = JsonConvert.SerializeObject(dataByConAndSql) })); } catch (Exception ex) { Log4net.log4netCreate(typeof(GeneratedFileController), log4netPath) .Error("LoginTable(string localDB) ---- > " + ex.Message); return(Json(new { message = "Fail", content = "表查询错误," + ex.Message })); } }
/// <summary> /// 根据键值对替换值 /// </summary> /// <param name="str">需替换的字符串</param> /// <param name="dri">键值对集合</param> /// <returns>string 替换之后的字符串</returns> private string SplitStr(string str, Dictionary <string, string> dictionary) { try { string resultStr = string.Empty; if (dictionary.Count <= 0) { return(string.Empty); } foreach (var dirItem in dictionary) { str = str.Replace(dirItem.Key, dirItem.Value); resultStr = str; } return(resultStr); } catch (Exception ex) { Log4net.log4netCreate(typeof(GeneratedFileController), log4netPath) .Error("SplitStr(string str, Dictionary<string, string> dictionary) ----> " + ex.Message); return("替换错误 ! "); } }
/// <summary> /// 爬虫执行 /// </summary> public void Crawlers() { fileList = new List <Model.FileInfo>(); DateTime beginTime = DateTime.Now; int threadOrder = Convert.ToInt32(Thread.CurrentThread.Name); //线程ID long step = (maxValue - minValue + 1) / threadNumber; long beginValue = minValue + step * (threadOrder - 1); long endValue = beginValue + step; if (threadOrder == threadNumber) { endValue = beginValue + step + ((maxValue - minValue + 1) % threadNumber); } string msgUrl = string.Empty; for (long i = beginValue; i < endValue; i++) { try { string url = $"http://www.46ek.com/view/{listIndex[Convert.ToInt32(i) - 1]}.html"; string result = HTTPGeneral.HTTPCrawler(url, "GET"); string[] regexStrings = regexString.Split(','); Match match = null; for (int j = 0; j < regexStrings.Length; j++) { match = new Regex(regexStrings[j]).Match(result); if (match.Success) { break; } } if (!string.IsNullOrEmpty(match.Value)) { msgUrl = match.Value; //存储404链接 lock (o) { fileList.Add(new Model.FileInfo() { FileID = i.ToString(), //从1开始 FileName = match.Value.Substring(match.Value.LastIndexOf('/') + 1), FileSize = GetFileSize(match.Value), FileUrl = match.Value, SynProgress = "0%", Tag = "等待", SynSpeed = "0KB", DownPath = match.Value, Async = true }); Console.WriteLine(match.Value); Thread.Sleep(1000); } } } catch (Exception ex) { Log4net.log4netCreate(typeof(ThreadCrawler), log4netPath).Error($"爬取地址[{msgUrl}]访问错误,信息: " + ex.Message); Thread.Sleep(1000); continue; } } DateTime endTime = DateTime.Now; TimeSpan timeSpan = endTime - beginTime; string message = "线程ID:" + Thread.CurrentThread.Name + " ----> Start: " + beginValue.ToString() + " -- End:" + endValue.ToString() + ", 耗时:" + timeSpan.TotalMinutes.ToString() + "分钟。"; Console.WriteLine(message); Thread.CurrentThread.Abort(); }