123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- using Npgsql;
- using System;
- using System.Collections.Generic;
- using System.Data.SqlClient;
- using System.Data;
- using System.Linq;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Threading.Tasks;
- using System.Configuration;
- namespace DataTransmission
- {
- public class CommonTool
- {
- public static string Name = string.Empty;
- public static string CenterId = string.Empty;
- public static string Paseword = string.Empty;
- public SqlConnection GetConSqlServer()
- {
- return new SqlConnection(ConfigurationManager.AppSettings["SQLServerDB"]);
- }
- public NpgsqlConnection GetConPgSql()
- {
- return new NpgsqlConnection(ConfigurationManager.AppSettings["PGSQLDB"]);
- }
- public DataTable GetDataTable(string sql, SqlConnection conn)
- {
- conn.Open();
- SqlCommand cmd = new SqlCommand(sql, conn);
- SqlDataAdapter sda = new SqlDataAdapter(cmd);
- DataSet dataSet = new DataSet();
- sda.Fill(dataSet, "table");
- conn.Close();
- return dataSet.Tables[0];
- }
- public DataTable GetDataTable(string sql, NpgsqlConnection conn)
- {
- conn.Open();
- NpgsqlCommand cmd = new NpgsqlCommand(sql, conn);
- NpgsqlDataAdapter sda = new NpgsqlDataAdapter(cmd);
- DataSet dataSet = new DataSet();
- sda.Fill(dataSet, "table");
- conn.Close();
- return dataSet.Tables[0];
- }
- public int Execute(string sql, NpgsqlConnection conn)
- {
- conn.Open();
- var i = 0;
- try
- {
- NpgsqlCommand cmd = new NpgsqlCommand(sql, conn);
- i = cmd.ExecuteNonQuery();
- }
- catch (Exception e)
- {
- Console.WriteLine(e);
- SaveLog(e.ToString() + "/n SQL:" + sql, 1);
- }
- conn.Close();
- return i;
- }
- public string MaskPhoneNumber(string phoneNumber)
- {
- if (String.IsNullOrEmpty(phoneNumber)) return "";
- Regex regex = new Regex(@"(\d{3})(\d{4})(\d{4})");
- Match match = regex.Match(phoneNumber);
- if (!match.Success || match.Groups.Count != 4) return phoneNumber;
- string frontCode = match.Groups[1].Value;
- string areaCode = match.Groups[2].Value;
- string prefixAndSuffix = match.Groups[3].Value;
- string maskedAreaCode = String.Join("", Enumerable.Repeat('*', areaCode.Length));
- string maskedPrefixAndSuffix = String.Join("", Enumerable.Repeat('*', prefixAndSuffix.Length - 4).Concat(prefixAndSuffix.Substring(prefixAndSuffix.Length - 4)));
- return $"{frontCode}{maskedAreaCode}{maskedPrefixAndSuffix}";
- }
- public void SaveLog(string LogMessage, int Level, string PreLogDir = "")
- {
- int LogLevel = 0;
- int.TryParse(ConfigurationManager.AppSettings["LogLevel"], out LogLevel);
- if (Level <= LogLevel)
- {
- try
- {
- string dirpath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\\" + PreLogDir + "log";
- if (!Directory.Exists(dirpath))
- {
- Directory.CreateDirectory(dirpath);
- }
- if (File.Exists(dirpath + "\\log.txt") && new FileInfo(dirpath + "\\log.txt").Length > 512 * 1024)
- {
- File.Move(dirpath + "\\log.txt", dirpath + "\\log" + DateTime.Now.ToString("yyyy-MM-ddHHmmssfff") + ".txt");
- }
- foreach (string LogFile in Directory.GetFiles(dirpath, "*.txt"))
- {
- if (File.GetLastWriteTime(LogFile) < DateTime.Now.AddDays(-3))
- {
- File.Delete(LogFile);
- }
- }
- File.AppendAllText(dirpath + "\\log.txt", DateTime.Now.ToString("yyyy-MM-ddHHmmssfff") + "_" + LogMessage);
- File.AppendAllText(dirpath + "\\log.txt", "\r\n");
- }
- catch { }
- }
- }
- }
- }
|