/// <summary> /// DataRow转Entity /// </summary> /// <typeparam name="T"></typeparam> /// <param name="tableRow"></param> /// <returns></returns> public static T ConvertToEntity <T>(DataRow tableRow) where T : new() { if (tableRow == null) { return(default(T)); } Type objType = typeof(T); T obj = Activator.CreateInstance <T>(); foreach (DataColumn column in tableRow.Table.Columns) { PropertyInfo property = objType.GetProperty( column.ColumnName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase); if (null == property || !property.CanWrite || null == tableRow[property.Name] || tableRow[property.Name] == DBNull.Value) { continue; } // 类型强转,将table字段类型转为集合字段类型 try { object value = Convert.ChangeType(tableRow[property.Name], property.PropertyType); property.SetValue(obj, value, null); } catch (Exception ex) { Log4NetUtil.WriteErrorLog(ex.ToString()); throw; } } return(obj); }
/// <summary> /// 获取单个值 /// </summary> /// <param name="sql"></param> /// <returns></returns> public static object GetScalar(string sql) { object result; using (OracleConnection conn = new OracleConnection(ORACLECONN)) { try { conn.Open(); OracleCommand command = new OracleCommand(sql, conn); result = command.ExecuteScalar(); } catch (Exception ex) { result = false; Log4NetUtil.WriteErrorLog("错误参数:" + sql); } finally { conn.Close(); } return(result); } }
/// <summary> /// 执行语句 ExecuteSql /// </summary> /// <param name="sql"></param> /// <param name="param"></param> /// <returns></returns> public static bool ExecuteSql(string sql, params MySqlParameter[] param) { bool result = false; using (MySqlConnection conn = new MySqlConnection(MYSQLCONN)) { try { conn.Open(); MySqlCommand command = new MySqlCommand(sql, conn); if (null != param && param.Length > 0) { command.Parameters.AddRange(param); } result = command.ExecuteNonQuery() > 0; } catch (Exception ex) { result = false; Log4NetUtil.WriteErrorLog("错误参数:" + sql + "\n" + JsonConvert.SerializeObject(param) + "\n" + ex.ToString()); } finally { conn.Close(); } return(result); } }
/// <summary> /// 获取DataTable /// </summary> /// <param name="sql"></param> /// <param name="param"></param> /// <returns></returns> public static DataTable GetDataTable(string sql, params MySqlParameter[] param) { DataTable result = new DataTable(); using (MySqlConnection conn = new MySqlConnection(MYSQLCONN)) { try { conn.Open(); MySqlCommand command = new MySqlCommand(sql, conn); if (null != param && param.Length > 0) { command.Parameters.AddRange(param); } //MySqlDataAdapter adapter = new MySqlDataAdapter(command); MySqlDataReader reader = command.ExecuteReader(); result.Load(reader); reader.Close(); } catch (Exception ex) { result = null; Log4NetUtil.WriteErrorLog("错误参数:" + sql + "\n" + JsonConvert.SerializeObject(param) + "\n" + ex.ToString()); } finally { conn.Close(); } return(result); } }
/// <summary> /// DataTable转List<T> /// </summary> /// <typeparam name="T"></typeparam> /// <param name="dt"></param> /// <param name="isStoreDB"></param> /// <returns></returns> public static List <T> ConvertToList <T>(DataTable dt, bool isStoreDB = true) where T : new() { if (null == dt || dt.Rows.Count == 0) { return(default(List <T>)); } List <T> list = new List <T>(); Type type = typeof(T); // 集合属性数组 PropertyInfo[] porpertyArray = type.GetProperties(); foreach (DataRow row in dt.Rows) { // 新建对象实例 T entity = Activator.CreateInstance <T>(); foreach (PropertyInfo p in porpertyArray) { if (!dt.Columns.Contains(p.Name) || null == row[p.Name] || row[p.Name] == DBNull.Value) { // DataTable列中不存在集合属性或者字段内容为空则,跳出循环,进行下个循环 continue; } if (isStoreDB && p.PropertyType == typeof(DateTime) && Convert.ToDateTime(row[p.Name]) < Convert.ToDateTime("1753-01-01")) { continue; } try { // 类型强转,将table字段类型转为集合字段类型 var obj = Convert.ChangeType(row[p.Name], p.PropertyType); p.SetValue(entity, obj, null); } catch (Exception ex) { Log4NetUtil.WriteErrorLog(ex.ToString()); throw; } } list.Add(entity); } list.TrimExcess(); return(list); }
/// <summary> /// 邮件发送 /// </summary> /// <param name="host">服务器地址</param> /// <param name="from">邮箱账号</param> /// <param name="pwd">邮箱密码</param> /// <param name="tomailList">接收邮箱(Ps:多个时以英文“,”分开)</param> /// <param name="subject">标题</param> /// <param name="body">内容</param> /// <returns></returns> public static bool SendMail(string host, string from, string pwd, string tomailList, string subject, string body, string excelPath) { using (SmtpClient client = new SmtpClient()) { client.Host = host; client.UseDefaultCredentials = true; client.DeliveryMethod = SmtpDeliveryMethod.Network; client.Credentials = new System.Net.NetworkCredential(from, pwd); MailMessage Message = new MailMessage(); Message.From = new MailAddress(from); foreach (string str in tomailList.Split(',')) { if (!string.IsNullOrEmpty(str)) { Message.To.Add(str); } } Message.Subject = subject; if (!string.IsNullOrEmpty(excelPath)) { Message.Attachments.Add(new Attachment(excelPath)); } Message.SubjectEncoding = Encoding.UTF8; Message.BodyEncoding = Encoding.UTF8; Message.IsBodyHtml = true; try { Message.Body = body; client.Send(Message); return(true); } catch (Exception ex) { Log4NetUtil.WriteErrorLog("错误参数:" + tomailList + "\n" + ex.ToString()); return(false); } } }