C# 基础:DataTable操作、发邮件

时间:2023-03-10 01:58:42
C#  基础:DataTable操作、发邮件

本文出自:https://www.cnblogs.com/2186009311CFF/p/6865909.html

DataTable操作

据参数删除为0的列:包括遍历、删除、取值

   public static DataTable GetNonzeroColDataTable(DataTable dt, Dictionary<string, string> chargeWayDic= null )
{
DataTable newdt = new DataTable();
try
{
#region 对筛选结果全部为0的列进行删除
List<string> delColAlist = new List<string>();//列值为0的列名的动态数组
bool IsZero = false;//列值为0的标志
for (int ic = 0; ic < dt.Columns.Count; ic++)
{
string strColumnName = dt.Columns[ic].ColumnName;
for (int jr = 0; jr < dt.Rows.Count; jr++)
{
if (chargeWayDic != null)//指定了一些列进行检查
{
#region 指定了一些列进行检查
if (!chargeWayDic.Keys.Contains(strColumnName)) continue;
double ivCompare = double.Parse(dt.Rows[jr][ic].ToString());
if (ivCompare == 0)
{
IsZero = true;
if ((jr == (dt.Rows.Count - 1)) && (IsZero))
{
if ((!delColAlist.Contains(strColumnName)) && (chargeWayDic.Keys.Contains(strColumnName)))
delColAlist.Add(strColumnName);
}
continue;
}
else
{
IsZero = false;
break;
}
#endregion
}
else//对所有列进行检查
{
try
{
double ivCompare = double.Parse(dt.Rows[jr][ic].ToString());
if (ivCompare == 0)
{
IsZero = true;
if ((jr == (dt.Rows.Count - 1)) && (IsZero))
{
if (!delColAlist.Contains(strColumnName))
delColAlist.Add(strColumnName);
}
continue;
}
else
{
IsZero = false;
break;
}
}
catch //不是数字的列忽略
{
break;
}
} }
}
newdt=GetNewDataTable(dt, delColAlist);
delColAlist.Clear();
#endregion
return newdt;//返回的查询结果
}
catch
{
return dt;
}
}

  

 public static DataTable GetNewDataTable(DataTable dt, List<string> deleteColList)
{
DataTable newdt = new DataTable();
try
{
if (deleteColList != null)
{
if (dt != null)
{
foreach (string sdeleteCol in deleteColList)
{
if (!String.IsNullOrEmpty(sdeleteCol))
{
if (dt.Columns.Contains(sdeleteCol))
{
dt.Columns.Remove(sdeleteCol);//删除某列
}
} }
}
deleteColList.Clear();
} newdt = dt.Clone();
DataRow[] dr = dt.Select();
for (int i = 0; i < dr.Length; i++)
{
newdt.ImportRow((DataRow)dr[i]);
}
return newdt;//返回的查询结果
}
catch (Exception ex)
{
// MessageBox.Show(ex.Message);
return newdt;
}
}

  包括遍历、删除、取值、列位置设置、新增列

 DataColumn dataColumn = new DataColumn(列名变量);
var colIndex = table.Columns.IndexOf("列名");
table.Columns.Add(dataColumn);
dataColumn.SetOrdinal(colIndex);//列的位置
foreach (DataRow dR in table.Rows)
{
double dpaper = 0;
foreach (DataColumn col in table.Columns)
{
if (col.ColumnName.Contains ("元"))
{
string strColumnName = col.ColumnName.Substring(0, col.ColumnName.Length - 3); ;//列名去掉(元)
if (Dictionary.Keys.Contains(strColumnName))
{
dpaper += (double.Parse(dR[col.ColumnName].ToString()));
}
}
}
dR[列名变量] = (double.Parse(dR["列名"].ToString())) - dpaper;
}
table.Columns.Remove("删除列");

 发邮件:实际操作注重灵活运用,需要建公共类的思想

 static void Main(string[] args)
{ Addresser m_Addresser = new Addresser();
m_Addresser.User = "发件人账户";
m_Addresser.SendMailType = "qq";//发件人账户类型,此处为qq邮箱
m_Addresser.AuthorizationCode = "发件人授权码";//设置里开启STMP发信息获取
Addressee m_Addressee = new Addressee();
m_Addressee.User = "收件人账户";
m_Addressee.SendMailType = "163";//收件人账户类型,此处为163邮箱
MailContent m_MailContent = new MailContent();
m_MailContent.Tittle = "邮件主题";
m_MailContent.Subject = "邮件副标题";
m_MailContent.Body = "邮件内容"; SendQQMail.SendMail(m_Addresser,m_Addressee, m_MailContent);//发送邮件
}

  

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;
using static System.Net.Mime.MediaTypeNames; namespace SchemeOptimization.SendMail
{
public class SendQQMail
{
public static void SendMail(Addresser m_Addresser,Addressee m_Addressee, MailContent m_mailContent)
{
try
{
MailMessage msg = new MailMessage();
string addressee = m_Addressee.User + "@" + m_Addressee.SendMailType + ".com";
string addresser = m_Addresser.User + "@" + m_Addresser.SendMailType + ".com";
msg.To.Add(addressee);//收件人
msg.CC.Add(addressee);//抄送人 msg.From = new MailAddress(addresser, m_mailContent.Tittle ); msg.Subject = m_mailContent.Subject;
//标题格式为UTF8
msg.SubjectEncoding = Encoding.UTF8; msg.Body = m_mailContent.Body ;
//内容格式为UTF8
msg.BodyEncoding = Encoding.UTF8; SmtpClient client = new SmtpClient();
//SMTP服务器地址
client.Host = "smtp."+ m_Addresser.SendMailType + ".com";//发件人的邮件类型
//SMTP端口,QQ邮箱填写587
if (m_Addresser.SendMailType == "qq")//发件人的邮件类型对应的端口号
{
client.Port = 587;
}
else {
client.Port = 25;
} //启用SSL加密
client.EnableSsl = true; client.Credentials = new NetworkCredential(addresser, m_Addresser.AuthorizationCode);
//发送邮件
client.Send(msg);
//client.SendAsync(msg);
msg.Dispose();
}
catch (Exception ex)
{
WriteException(ex);
} }
/// <summary>
/// 写错误日志
/// </summary>
/// <param name="ex">异常</param>
/// <param name="msg">其他内容</param>
/// <param name="typestr">文件名</param>
public static void WriteException(Exception ex, string msg = "", string typestr = "")
{
try
{
if (!Directory.Exists("日志"))
Directory.CreateDirectory("日志");
using (
FileStream fs = File.Open(string.Format(".//日志\\Log_{0}.txt", DateTime.Now.ToString("yyyyMMdd")), FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
fs.Seek(0, SeekOrigin.End);
byte[] buffer = Encoding.Default.GetBytes("-------------------------\r\n");
fs.Write(buffer, 0, buffer.Length); buffer = Encoding.Default.GetBytes(DateTime.Now.ToString() + "\r\n");
fs.Write(buffer, 0, buffer.Length); buffer = Encoding.Default.GetBytes("信息:" + msg + "\r\n");
fs.Write(buffer, 0, buffer.Length); if (ex != null)
{
buffer = Encoding.Default.GetBytes("成员名: " + ex.TargetSite + "\r\n");
fs.Write(buffer, 0, buffer.Length); buffer = Encoding.Default.GetBytes("引发异常的类: " + ex.TargetSite.DeclaringType + "\r\n");
fs.Write(buffer, 0, buffer.Length); buffer = Encoding.Default.GetBytes("异常信息: " + ex.Message + "\r\n");
fs.Write(buffer, 0, buffer.Length); buffer = Encoding.Default.GetBytes("引发异常的程序集或对象: " + ex.Source + "\r\n");
fs.Write(buffer, 0, buffer.Length); buffer = Encoding.Default.GetBytes("栈:" + ex.StackTrace + "\r\n");
fs.Write(buffer, 0, buffer.Length);
} fs.Close();
}
}
catch
{ }
} }
/// <summary>
/// 收件人
/// </summary>
public class Addressee
{
/// <summary>
/// 账号
/// </summary>
public string User;
/// <summary>
/// 发送邮件类型
/// </summary>
public string SendMailType;
}
/// <summary>
/// 发件人
/// </summary>
public class Addresser
{
/// <summary>
///
/// </summary>
public string User;
/// <summary>
///
/// </summary>
public string SendMailType;
/// <summary>
/// 授权码,在设置里开启SMIP服务
/// 服务器响应为:mail from address must be same as authorization user
/// 是因为没有设置开启SMIP服务
/// </summary>
public string AuthorizationCode; }
public class MailContent
{
/// <summary>
///
/// </summary>
public string Tittle;
/// <summary>
///
/// </summary>
public string Subject;
/// <summary>
///
/// </summary>
public string Body; }