/// <summary> /// 根据键值设置Config文件里的AppSetting里的数据 /// </summary> /// <param name="appKey">键值</param> /// <param name="appKeyValue">应用程序信息</param> /// <returns>ToolResult对象</returns> public static ToolResult setConfigForAppSettings(string appKey, string appKeyValue) { ToolResult toolResult = new ToolResult(); try { bool flag = false; foreach (string str in ConfigurationManager.AppSettings) { if (string.Compare(str, appKey) == 0) { flag = true; } } System.Configuration.Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); if (flag) { configuration.AppSettings.Settings.Remove(appKey); } configuration.AppSettings.Settings.Add(appKey, appKeyValue); configuration.Save(ConfigurationSaveMode.Modified); ConfigurationManager.RefreshSection("appSettings"); toolResult.IsSucess = true; }catch (Exception ex) { toolResult.IsSucess = false; toolResult.StrErrMessage = ex.Message; } return(toolResult); }
/// <summary> /// 数据库连接测试 /// </summary> /// <param name="Server">服务器地址</param> /// <param name="PortNum">端口号,默认为1433</param> /// <param name="Database">数据库名称</param> /// <param name="UID">用户名</param> /// <param name="Pwd">密码</param> /// <returns>BOOl值,True为连接成功,False为连接失败</returns> public ToolResult databaseTest(string Server, string PortNum, string Database, string UID, string Pwd) { ToolResult toolResult = new ToolResult(); if (string.IsNullOrEmpty(PortNum)) { PortNum = "1433"; } SqlConnection connection = new SqlConnection(string.Format("server={0},{1};database={2};UID={3};pwd={4}", Server, PortNum, Database, UID, Pwd)); try { connection.Open(); toolResult.ObjResult = true; toolResult.IsSucess = true; }catch (Exception ex) { toolResult.IsSucess = false; toolResult.StrErrMessage = ex.Message; }finally { connection.Close(); } return(toolResult); }
/// <summary> /// 根据键值设置Config文件里的ConnectionString里的数据 /// </summary> /// <param name="appKey">键值</param> /// <param name="appKeyValue">连接字符串</param> /// <returns>ToolResult对象</returns> public static ToolResult setConfigForConnectionStrings(string appKey, string appKeyValue) { ToolResult toolResult = new ToolResult(); try { bool flag = false; for (int i = 0; i < ConfigurationManager.ConnectionStrings.Count; i++) { if (string.Compare(appKey, ConfigurationManager.ConnectionStrings[i].Name) == 0) { flag = true; } } System.Configuration.Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); if (flag) { configuration.ConnectionStrings.ConnectionStrings.Remove(appKey); } ConnectionStringSettings settings = new ConnectionStringSettings(appKey, appKeyValue); configuration.ConnectionStrings.ConnectionStrings.Add(settings); configuration.Save(ConfigurationSaveMode.Modified); ConfigurationManager.RefreshSection("connectionStrings"); toolResult.IsSucess = true; }catch (Exception ex) { toolResult.IsSucess = false; toolResult.StrErrMessage = ex.Message; } return(toolResult); }
/// <summary> /// Base64解密 /// </summary> /// <param name="str">Base64编码的字符串</param> /// <returns>解密后的字符串</returns> public ToolResult Base64DecryptString(string str) { ToolResult toolResult = new ToolResult(); try { if ((str.Length % 4) != 0) { throw new ArgumentException("不是正确的BASE64编码,请检查。", "str"); } if (!Regex.IsMatch(str, "^[A-Z0-9/+=]*$", RegexOptions.IgnoreCase)) { throw new ArgumentException("包含不正确的BASE64编码,请检查。", "str"); } string str2 = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+/="; int num = str.Length / 4; ArrayList list = new ArrayList(num * 3); char[] chArray = str.ToCharArray(); for (int i = 0; i < num; i++) { byte[] buffer = new byte[] { (byte)str2.IndexOf(chArray[i * 4]), (byte)str2.IndexOf(chArray[(i * 4) + 1]), (byte)str2.IndexOf(chArray[(i * 4) + 2]), (byte)str2.IndexOf(chArray[(i * 4) + 3]) }; byte[] buffer2 = new byte[3]; buffer2[0] = (byte)((buffer[0] << 2) ^ ((buffer[1] & 0x30) >> 4)); if (buffer[2] != 0x40) { buffer2[1] = (byte)((buffer[1] << 4) ^ ((buffer[2] & 60) >> 2)); } else { buffer2[2] = 0; } if (buffer[3] != 0x40) { buffer2[2] = (byte)((buffer[2] << 6) ^ buffer[3]); } else { buffer2[2] = 0; } list.Add(buffer2[0]); if (buffer2[1] != 0) { list.Add(buffer2[1]); } if (buffer2[2] != 0) { list.Add(buffer2[2]); } } byte[] bytes = (byte[])list.ToArray(Type.GetType("System.Byte")); toolResult.ObjResult = Encoding.Default.GetString(bytes); toolResult.IsSucess = true; }catch (Exception ex) { toolResult.IsSucess = false; toolResult.StrErrMessage = ex.Message; } return(toolResult); }
//返回DataReader /// <summary> /// 返回DataReader,cmdText为SQL语句 /// </summary> /// <param name="cmdText">SQL语句</param> /// <returns>SqlDataReader对象的实例对象</returns> public ToolResult ExecuteReader(string cmdText) { ToolResult toolResult = new ToolResult(); try { SqlCommand command = new SqlCommand(cmdText, Connection); toolResult.ObjResult = command.ExecuteReader(); toolResult.IsSucess = true; }catch (Exception ex) { toolResult.IsSucess = false; toolResult.StrErrMessage = ex.Message; } return(toolResult); }
/// <summary> /// 返回自定义样式的当前日期 /// </summary> /// <param name="strFormat">日期样式</param> /// <returns></returns> public static ToolResult GetDate(string strFormat) { ToolResult toolResult = new ToolResult(); try { toolResult.ObjResult = DateTime.Now.ToString(strFormat); toolResult.IsSucess = true; } catch (Exception ex) { toolResult.IsSucess = false; toolResult.StrErrMessage = ex.Message; } return(toolResult); }
// Methods /// <summary> /// 根据键值取得Config文件里的AppSetting里的数据 /// </summary> /// <param name="appKey">键值</param> ///<returns>ToolResult对象</returns> public static ToolResult getConfigForAppSettings(string appKey) { ToolResult toolResult = new ToolResult(); try { toolResult.ObjResult = ConfigurationManager.AppSettings[appKey].ToString(); toolResult.IsSucess = true; }catch (Exception ex) { toolResult.IsSucess = false; toolResult.ObjResult = null; toolResult.StrErrMessage = ex.Message; } return(toolResult); }
/// <summary> /// 返回DataReader,cmdText为SQL语句,values为SQL语句中的参数 /// </summary> /// <param name="cmdText">SQL语句</param> /// <param name="values">SQL语句中的参数</param> /// <returns>SqlDataReader对象的实例对象</returns> public ToolResult ExecuteReader(string cmdText, params SqlParameter[] values) { ToolResult toolResult = new ToolResult(); try { SqlCommand command = new SqlCommand(cmdText, Connection); command.Parameters.AddRange(values); toolResult.ObjResult = command.ExecuteReader(); toolResult.IsSucess = true; }catch (Exception ex) { toolResult.IsSucess = false; toolResult.StrErrMessage = ex.Message; } return(toolResult); }
/// <summary> /// 执行insert,update,delete,返回影响的行数 /// </summary> /// <param name="cmdText">sql语句</param> /// <returns>int类型:影响的行数</returns> public ToolResult MysqlExecuteNonQuery(string cmdText) { ToolResult _result = new ToolResult(); try { MySqlCommand cmd = new MySqlCommand(cmdText, MySql_Connection); _result.ObjResult = cmd.ExecuteNonQuery(); _result.IsSucess = true; } catch (Exception ex) { _result.IsSucess = false; _result.StrErrMessage = ex.Message; } return(_result); }
//返回DataTable /// <summary> /// 返回DataTable,cmdText为SQL语句 /// </summary> /// <param name="cmdText">SQL语句</param> /// <returns>DataTable</returns> public ToolResult GetDataSet(string cmdText) { ToolResult toolResult = new ToolResult(); try { DataSet ds = new DataSet(); SqlDataAdapter da = new SqlDataAdapter(cmdText, Connection); da.Fill(ds); toolResult.ObjResult = ds.Tables[0]; toolResult.IsSucess = true; }catch (Exception ex) { toolResult.IsSucess = false; toolResult.StrErrMessage = ex.Message; } return(toolResult); }
/// <summary> /// 执行select,返回MySqlDataReader对象 /// </summary> /// <param name="cmdText">sql语句</param> /// <param name="cmdType">sql语句类型:文本、存储过程</param> /// <param name="strError">错误信息</param> /// <returns>MySqlDataRader对象</returns> public ToolResult MysqlExecuteReader(string cmdText, CommandType cmdType, ref string strError) { ToolResult _result = new ToolResult(); try { MySqlCommand cmd = new MySqlCommand(cmdText, MySql_Connection); cmd.CommandType = cmdType; _result.ObjResult = cmd.ExecuteReader(); _result.IsSucess = true; } catch (Exception ex) { _result.IsSucess = false; _result.StrErrMessage = ex.Message; } return(_result); }
/// <summary> /// 执行select,返回DataTable /// </summary> /// <param name="cmdText">sql语句</param> /// <param name="strError">错误信息</param> /// <returns>DataTable</returns> public ToolResult MysqlDataSet(string cmdText, ref string strError) { ToolResult _result = new ToolResult(); try { DataSet dataSet = new DataSet(); MySqlCommand cmd = new MySqlCommand(cmdText, MySql_Connection); new MySqlDataAdapter(cmd).Fill(dataSet); _result.ObjResult = dataSet.Tables[0]; _result.IsSucess = true; } catch (Exception ex) { _result.IsSucess = false; _result.StrErrMessage = ex.Message; } return(_result); }
/// <summary> /// 加载默认设置 /// </summary> private void LoadDefault() { //程序名称 ToolResult tRe = ToolAppConfig.getConfigForAppSettings("name"); this.Text = tRe.IsSucess?tRe.ObjResult.ToString():"C# WinForm工具集"; //农历日期 ToolDate.toolChineseDateTime = new ToolChineseDateTime(DateTime.Now); lbl_chineseDate.Text = string.Format("{0} {1} {2} {3} {4} {5} ||{6}", ToolDate.toolChineseDateTime.ToChineseEraString(), ToolDate.toolChineseDateTime.ChineseWeek, ToolDate.toolChineseDateTime.ChineseZodiac, ToolDate.toolChineseDateTime.ToChineseString(), ToolDate.toolChineseDateTime.SolarTerm, ToolDate.toolChineseDateTime.ToDateTime(), ToolDate.toolChineseDateTime.ToString()); //公历日期 lbl_date.Text = string.Format("{0} {1} {2} {3}", ToolDate.GetDate().ObjResult, "", "", ""); }
/// <summary> /// 返回DataTable,cmdText为SQL语句,values为SQL语句中的参数 /// </summary> /// <param name="cmdText">SQL语句</param> /// <param name="values">SQL语句中的参数</param> /// <returns>DataTable</returns> public ToolResult GetDataSet(string cmdText, params SqlParameter[] values) { ToolResult toolResult = new ToolResult(); try { DataSet ds = new DataSet(); SqlCommand command = new SqlCommand(cmdText, Connection); command.Parameters.AddRange(values); SqlDataAdapter da = new SqlDataAdapter(command); da.Fill(ds); toolResult.ObjResult = ds.Tables[0]; toolResult.IsSucess = true; } catch (Exception ex) { toolResult.IsSucess = false; toolResult.StrErrMessage = ex.Message; } return(toolResult); }
/// <summary> /// 执行insert,update,delete,返回影响的行数 /// </summary> /// <param name="cmdText">sql语句</param> /// <param name="cmdType">sql语句类型:文本、存储过程</param> /// <param name="strError">错误信息</param> /// <returns>int类型:影响的行数</returns> public ToolResult MysqlExecuteNonQuery(string cmdText, CommandType cmdType, ref string strError, params MySqlParameter[] values) { ToolResult _result = new ToolResult(); try { MySqlCommand cmd = new MySqlCommand(cmdText, MySql_Connection); cmd.CommandType = cmdType; if (values != null) { cmd.Parameters.AddRange(values); } _result.ObjResult = cmd.ExecuteNonQuery(); _result.IsSucess = true; } catch (Exception ex) { _result.IsSucess = false; _result.StrErrMessage = ex.Message; } return(_result); }
/// <summary> /// 执行select,返回DataTable /// </summary> /// <param name="cmdText">sql语句</param> /// <param name="cmdType">sql语句类型:文本、存储过程</param> /// <param name="strError">错误信息</param> /// <returns>DataTable</returns> public ToolResult MysqlDataSet(string cmdText, CommandType cmdType, ref string strError, params MySqlParameter[] values) { ToolResult _result = new ToolResult(); try { DataSet dataSet = new DataSet(); MySqlCommand cmd = new MySqlCommand(cmdText, MySql_Connection); cmd.CommandType = cmdType; if (values != null) { cmd.Parameters.AddRange(values); } new MySqlDataAdapter(cmd).Fill(dataSet); _result.ObjResult = dataSet.Tables[0]; _result.IsSucess = true; } catch (Exception ex) { _result.IsSucess = false; _result.StrErrMessage = ex.Message; } return(_result); }
/// <summary> /// 计算两个日期间相差的天数 /// </summary> /// <param name="_beginDate">开始日期</param> /// <param name="_endDate">结束日期</param> /// <returns>ToolResult对象</returns> public static ToolResult CalculateDays(DateTime _beginDate, DateTime _endDate) { ToolResult toolResult = new ToolResult(); try { if (_beginDate == null || _endDate == null) { toolResult.IsSucess = false; toolResult.StrErrMessage = "日期变量未赋值。"; toolResult.ObjResult = null; } else { TimeSpan span = _endDate - _beginDate; if (span.Days < 0) { toolResult.IsSucess = false; toolResult.StrErrMessage = "起始日期大于结束日期。"; toolResult.ObjResult = null; } else { toolResult.ObjResult = span.Days + 1; toolResult.IsSucess = true; toolResult.StrErrMessage = ""; } } }catch (Exception ex) { toolResult.IsSucess = false; toolResult.StrErrMessage = ex.Message; toolResult.ObjResult = null; } return(toolResult); }
/// <summary> /// Base64加密 /// </summary> /// <param name="str">需要加密的字符串</param> /// <returns>加密后的字符串</returns> public ToolResult Base64EncryptString(string str) { ToolResult toolResult = new ToolResult(); try { int num5; char[] chArray = new char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/', '=' }; byte num = 0; ArrayList list = new ArrayList(Encoding.Default.GetBytes(str)); int count = list.Count; int num3 = count / 3; int num4 = 0; num4 = count % 3; if (num4 > 0) { for (num5 = 0; num5 < (3 - num4); num5++) { list.Add(num); } num3++; } StringBuilder builder = new StringBuilder(num3 * 4); for (num5 = 0; num5 < num3; num5++) { byte[] buffer = new byte[] { (byte)list[num5 * 3], (byte)list[(num5 * 3) + 1], (byte)list[(num5 * 3) + 2] }; int[] numArray = new int[4]; numArray[0] = buffer[0] >> 2; numArray[1] = ((buffer[0] & 3) << 4) ^ (buffer[1] >> 4); if (!buffer[1].Equals(num)) { numArray[2] = ((buffer[1] & 15) << 2) ^ (buffer[2] >> 6); } else { numArray[2] = 0x40; } if (!buffer[2].Equals(num)) { numArray[3] = buffer[2] & 0x3f; } else { numArray[3] = 0x40; } builder.Append(chArray[numArray[0]]); builder.Append(chArray[numArray[1]]); builder.Append(chArray[numArray[2]]); builder.Append(chArray[numArray[3]]); } toolResult.ObjResult = builder.ToString(); toolResult.IsSucess = true; }catch (Exception ex) { toolResult.IsSucess = false; toolResult.StrErrMessage = ex.Message; } return(toolResult); }