public static ConsoleMsg Start(string log, ConsoleColor errorColor = ConsoleColor.Red, bool isAddTime = true, int millisecond = 500, string modify = "正在{0}") { var sp = new StrPlus(); if (isAddTime) { sp.Append($"{DateTime.Now.ToString("HH:mm:ss")}\t"); } sp.Append(string.Format(modify + " ", log)); Console.Write(sp); return new ConsoleMsg(millisecond, errorColor); }
/// <summary> /// 转换成Json格式 x=x&x=x /// </summary> /// <param name="dic">Dictionary对象</param> /// <typeparam name="T1">Key</typeparam> /// <typeparam name="T2">Value</typeparam> public static string ToJson <T1, T2>(this Dictionary <T1, T2> dic) { var sp = new StrPlus(); foreach (var item in dic) { sp += string.Format("{0}={1}&", item.Key, item.Value); } return(sp.DelLastChar("&")); }
/// <summary> /// 迭代字符 /// </summary> /// <param name="str">要迭代的字符串</param> /// <param name="count">次数</param> public static string For(this string str, int count = 1) { var strs = new StrPlus(); for (var i = 0; i < count; i++) { strs += str; } return(strs); }
public virtual string GetFields() { if (SelectString.Length > 0) { return(SelectString.ToString()); } var str = new StrPlus(); foreach (var item in Map.ModelList.Where(o => o.Value.IsDbField)) { if (!dbProvider.IsField(item.Value.Column.Name)) { str += item.Value.Column.Name + " as " + item.Key.Name + ","; continue; } str += dbProvider.CreateTableAegis(item.Value.Column.Name) + ","; } return(str.Value.IsNullOrEmpty() ? "*" : str.DelLastChar(",")); }
/// <summary> /// 生成Excel文件 /// </summary> /// <param name="dt">数据源</param> /// <param name="fileName">保存的文件名</param> public static void CreateExcel(DataTable dt, string fileName) { var response = HttpContext.Current.Response; response.ContentEncoding = Encoding.GetEncoding("gb2312"); response.AppendHeader("content-disposition", "attachment;filename=\"" + HttpUtility.UrlEncode(fileName, Encoding.UTF8) + ".xls\""); response.ContentType = "application/ms-excel"; var SP = new StrPlus(); SP.Append("<table border='1' cellspacing='0' cellpadding='0'>"); SP.Append("<tr>"); foreach (DataColumn item in dt.Columns) { SP.AppendFormat( "<td style='font-size: 12px;text-align:center;background-color: #DCE0E2; font-weight:bold;' height='20'>{0}</td>", item.Caption); } SP.Append("</tr>"); //定义表对象与行对象,同时用DataSet对其值进行初始化 foreach (DataRow row in dt.Rows) { SP.Append("<tr>"); //在当前行中,逐列获得数据,数据之间以\t分割,结束时加回车符\n for (var i = 0; i < dt.Columns.Count; i++) { SP.AppendFormat("<td style='background-color: #E9ECED;font-size: 12px;'>{0}</td>", row[i].ToString()); } SP.Append("</tr>"); } SP.Append("</table>"); response.Write(SP); response.End(); }
/// <summary>显示菜单</summary> protected void ShowMenu() { var sp = new StrPlus(); sp.AppendLine(new string('*', 50)); sp.AppendLine("1 显示状态"); if (WinServer.IsInstalled(ServiceName) == true) { if (WinServer.IsRunning(ServiceName) == true) { sp.AppendLine("3 停止服务 -stop"); } else { sp.AppendLine("2 卸载服务 -u"); sp.AppendLine("3 启动服务 -start"); } } else { sp.AppendLine("2 安装服务 -i"); } if (_isTimingService) { sp.AppendLine("4 单步调试 -step"); } sp.AppendLine("5 循环调试 -run"); if (_isTimingService) { sp.AppendLine("6 任务列表 -show"); } sp.AppendLine("0 退出"); sp.AppendLine(new string('*', 50)); Consoles.WriteLine(sp.ToString(), ConsoleColor.Yellow); }
/// <summary> /// 分页函数(模板) /// </summary> /// <param name="pageTotal">总记录数</param> /// <param name="pageSize">每页显示记录数</param> /// <param name="pageIndex">页面索引</param> /// <param name="pageShowLength">显示页码长度</param> /// <param name="pageName">页面名称</param> public SplitResult Split(int pageTotal, int pageSize, int pageIndex, int pageShowLength, string pageName, string pageParam = "") { //总页数 var pageCount = 0; //下一页 var nextIndex = 0; //上一页 var preIndex = 0; //开始页码 var startCount = 0; //结束页码 var endCount = 0; // 将返回的结果 var sr = new SplitResult(); var sp = new StrPlus(); if (pageIndex < 1) { pageIndex = 1; } //计算总页数 if (pageSize != 0) { pageCount = (pageTotal / pageSize); pageCount = ((pageTotal % pageSize) != 0 ? pageCount + 1 : pageCount); pageCount = (pageCount == 0 ? 1 : pageCount); } nextIndex = pageIndex + 1; preIndex = pageIndex - 1; if (preIndex < 1) { preIndex = 1; } if (nextIndex > pageCount) { nextIndex = pageCount; } //中间页起始序号 startCount = (pageIndex + pageShowLength / 2) > pageCount ? pageCount - (pageShowLength - 1) : pageIndex - pageShowLength / 2; //中间页终止序号 endCount = pageIndex < pageShowLength / 2 ? pageShowLength : pageIndex + pageShowLength / 2; if (pageShowLength % 2 == 0) { startCount = startCount + 1; } //为了避免输出的时候产生负数,设置如果小于1就从序号1开始 if (startCount < 1) { startCount = 1; } //页码+5的可能性就会产生最终输出序号大于总页码,那么就要将其控制在页码数之内 if (pageCount < endCount) { endCount = pageCount; } // 首页 sp += Template(Template_First, pageTotal, pageCount, nextIndex, preIndex, pageSize, pageIndex, pageName, pageParam); // 上一页 sp += Template(Template_Pre, pageTotal, pageCount, nextIndex, preIndex, pageSize, pageIndex, pageName, pageParam); // 页码列表 for (var i = startCount; i <= endCount; i++) { if (i != pageIndex || string.IsNullOrWhiteSpace(Template_Active)) { sp += Template(Template_Default, pageTotal, pageCount, nextIndex, preIndex, pageSize, i, pageName, pageParam); } else { sp += Template(Template_Active, pageTotal, pageCount, nextIndex, preIndex, pageSize, i, pageName, pageParam); } } // 下一页 sp += Template(Template_Next, pageTotal, pageCount, nextIndex, pageIndex, pageSize, pageIndex, pageName, pageParam); // 尾页 sp += Template(Template_Last, pageTotal, pageCount, nextIndex, pageIndex, pageSize, pageIndex, pageName, pageParam); sr.Split = sp; sr.Record = Template(Template_Record, pageTotal, pageCount, nextIndex, preIndex, pageSize, pageIndex); return sr; }
/// <summary> /// 创建数据库连接字符串 /// </summary> /// <param name="dataType">数据库类型</param> /// <param name="userID">账号</param> /// <param name="passWord">密码</param> /// <param name="server">服务器地址</param> /// <param name="catalog">表名</param> /// <param name="dataVer">数据库版本</param> /// <param name="connectTimeout">链接超时时间</param> /// <param name="poolMinSize">连接池最小数量</param> /// <param name="poolMaxSize">连接池最大数量</param> /// <param name="port">端口</param> public static string CreateConnString(DataBaseType dataType, string userID, string passWord, string server, string catalog, string dataVer, int connectTimeout = 60, int poolMinSize = 16, int poolMaxSize = 100, string port = "") { switch (dataType) { case DataBaseType.MySql: { return(string.Format( "Data Source='{0}';User Id='{1}';Password='******';Database='{3}';charset='gbk'", server, userID, passWord, catalog)); } case DataBaseType.SqlServer: { string connString; if (userID.IsNullOrEmpty() && passWord.IsNullOrEmpty()) { connString = string.Format("Pooling=true;Integrated Security=True;"); } else { connString = string.Format("User ID={0};Password={1};Pooling=true;", userID, passWord); } connString += string.Format("Data Source={0};Initial Catalog={1};", server, catalog); if (poolMinSize > 0) { connString += string.Format("Min Pool Size={0};", poolMinSize); } if (poolMaxSize > 0) { connString += string.Format("Max Pool Size={0};", poolMaxSize); } if (connectTimeout > 0) { connString += string.Format("Connect Timeout={0};", connectTimeout); } return(connString); } case DataBaseType.OleDb: { string connString; switch (dataVer) { case "3.0": { connString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;"); break; //Extended Properties=Excel 3.0; } case "4.0": { connString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;"); break; //Extended Properties=Excel 4.0; } case "5.0": { connString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;"); break; //Extended Properties=Excel 5.0; } case "95": { connString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;"); break; //Extended Properties=Excel 5.0; } case "97": { connString = string.Format("Provider=Microsoft.Jet.OLEDB.3.51;"); break; } case "2003": { connString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;"); break; //Extended Properties=Excel 8.0; } default: //2007+ { //DR=YES connString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;"); break; //Extended Properties=Excel 12.0; } } connString += string.Format("Data Source={0};", GetFilePath(server)); if (!userID.IsNullOrEmpty()) { connString += string.Format("User ID={0};", userID); } if (!passWord.IsNullOrEmpty()) { connString += string.Format("Password={0};", passWord); } return(connString); } case DataBaseType.Xml: { return(server.IsNullOrEmpty() ? string.Empty : GetFilePath(server)); } case DataBaseType.SQLite: { var plus = new StrPlus(); plus.AppendFormat("Data Source={0};Min Pool Size={1};Max Pool Size={2};", GetFilePath(server), poolMinSize, poolMaxSize); if (!passWord.IsNullOrEmpty()) { plus.AppendFormat("Password={0};", passWord); } if (!dataVer.IsNullOrEmpty()) { plus.AppendFormat("Version={0};", dataVer); } return(plus.Value); } case DataBaseType.Oracle: { if (port.IsNullOrEmpty()) { port = "1521"; } return (string.Format( "Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST={0})(PORT={3})))(CONNECT_DATA=(SERVER=DEDICATED)(SID={4})));User Id={1};Password={2};", server, userID, passWord, port, catalog)); } default: return(string.Empty); } }
/// <summary> /// 分页函数(模板) /// </summary> /// <param name="pageTotal">总记录数</param> /// <param name="pageSize">每页显示记录数</param> /// <param name="pageIndex">页面索引</param> /// <param name="pageName">页面名称</param> public SplitResult Split(int pageTotal, int pageSize, int pageIndex, string pageName, string pageParam = "") { //总页数 var pageCount = 0; //下一页 var nextIndex = 0; //上一页 var preIndex = 0; //开始页码 var startCount = 0; //结束页码 var endCount = 0; // 将返回的结果 var sr = new SplitResult(); var sp = new StrPlus(); if (pageIndex < 1) { pageIndex = 1; } //计算总页数 if (pageSize != 0) { pageCount = (pageTotal / pageSize); pageCount = ((pageTotal % pageSize) != 0 ? pageCount + 1 : pageCount); pageCount = (pageCount == 0 ? 1 : pageCount); } nextIndex = pageIndex + 1; preIndex = pageIndex - 1; if (preIndex < 1) { preIndex = 1; } if (nextIndex > pageCount) { nextIndex = pageCount; } //中间页起始序号 startCount = (pageIndex + 5) > pageCount ? pageCount - 9 : pageIndex - 4; //中间页终止序号 endCount = pageIndex < 5 ? 10 : pageIndex + 5; //为了避免输出的时候产生负数,设置如果小于1就从序号1开始 if (startCount < 1) { startCount = 1; } //页码+5的可能性就会产生最终输出序号大于总页码,那么就要将其控制在页码数之内 if (pageCount < endCount) { endCount = pageCount; } // 首页 sp += Template(Template_First, pageTotal, pageCount, nextIndex, preIndex, pageSize, pageIndex, pageName, pageParam); // 上一页 sp += Template(Template_Pre, pageTotal, pageCount, nextIndex, preIndex, pageSize, pageIndex, pageName, pageParam); // 页码列表 for (var i = startCount; i <= endCount; i++) { if (i != pageIndex || Template_Active.IsNullOrEmpty()) { sp += Template(Template_Default, pageTotal, pageCount, nextIndex, preIndex, pageSize, i, pageName, pageParam); } else { sp += Template(Template_Active, pageTotal, pageCount, nextIndex, preIndex, pageSize, i, pageName, pageParam); } } // 下一页 sp += Template(Template_Next, pageTotal, pageCount, nextIndex, pageIndex, pageSize, pageIndex, pageName, pageParam); // 尾页 sp += Template(Template_Last, pageTotal, pageCount, nextIndex, pageIndex, pageSize, pageIndex, pageName, pageParam); sr.Split = sp; sr.Record = Template(Template_Record, pageTotal, pageCount, nextIndex, preIndex, pageSize, pageIndex); return(sr); }