-1

首先,我是 C# 的新手,如果有任何帮助或建议,我将不胜感激。在其中一个应用程序中,我正在使用一些数据,这些数据是从字符串 SQL 字符串创建的,并保存为 DataSet() 中的变量,而我想要完成的是将这些数据发送到 DataSet (); 作为附件 csv 扩展名,在这里我既有创建文件的时候,也有想通过电子邮件发送的时候,我也不想物理地创建 CSV 文件,我只想通过电子邮件发送保存的任何数据。任何帮助,将不胜感激。

public partial class CreateFile: Page
{
public string CreateData(string cdata)
    {
        string getVals = "";
        try
        {
            ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true;
            Configuration rootwebconfig;
            rootwebconfig = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/MyWebSiteRoot");
            var conn = new SqlConnection();
            var cmd = new SqlCommand();
            SqlDataReader dr;
            string yesno = "No";
            string theName = "";
            conn.ConnectionString = rootwebconfig.ConnectionStrings.ConnectionStrings["MyConnection"].ToString();
            cmd = new SqlCommand("Select something" + dp_props.SelectedValue.ToString() + "'", conn);
            conn.Open();
            dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                Configuration rootwebconfig1;
                rootwebconfig1 = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/MyWebSiteRoot");
                string sql = "Here creates the csv by selecting neccesary columns and calculations";
                var connection1 = new SqlConnection();
                connection1.ConnectionString = rootwebconfig1.ConnectionStrings.ConnectionStrings["MyConnection"].ToString();
                var dataadapter1 = new SqlDataAdapter(sql, connection1);
                var dss1 = new DataSet();
                connection1.Open();
                dataadapter1.Fill(dss1, "csvtable");
                connection1.Close();
            }

            conn.Close();
        }
        catch (Exception ex)
        {
            getVals = ex.ToString();
        }

        return getVals;
    }

public void SendEmail(string props)
    {   
        try
        {
            // Sending the email
            var mailMessage = new MailMessage();
            mailMessage.From = new MailAddress("admin@something.com");
            mailMessage.Subject = "This is a test" + props + DateTime.Now +;
            System.Net.Mail.Attachment attachment = new System.Net.Mail.Attachment(dss1);
            mailMessage.Attachments.Add(attachment);
            attachment.Name = "csvtable.csv";
            mailMessage.Body = "Something";
            mailMessage.IsBodyHtml = true;
            
            mailMessage.To.Add(new MailAddress("user1@something.com"));

            mailMessage.Bcc.Add(new MailAddress("user1@something.com.com"));
            var smtp = new SmtpClient();
            smtp.Host = "host.com";
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtp.UseDefaultCredentials = false;
            smtp.Credentials = new NetworkCredential("username.com", "password");
            smtp.Port = 999;
            smtp.EnableSsl = true;
            smtp.Send(mailMessage);
        }
        catch (Exception ex)
        {
            string smsg;
            smsg = ex.ToString();
        }
    }

}

4

1 回答 1

0

有一个名为 CsvHelper 的开源项目 - 您可能希望使用它从 DataSet 中的 DataTable 创建一个 csv 文件 - stackoverflow.com/questions/49433486/

*** 更新

    var dss1 = new DataSet();
    var table = new DataTable("csvdata");
    dss1.Tables.Add(table);
    table.Columns.Add(new DataColumn("Name"));
    table.Columns.Add(new DataColumn("Height", typeof(int)));

    var row = table.NewRow();
    row["Name"] = "Rick";
    row["Height"] = 72;

    table.Rows.Add(row);


    Sender.SendEmail("",dss1);  
    
public class Sender
{
    public static void SendEmail(string props, DataSet dss1)
    {
        try
        {
            using (var stream = new MemoryStream())
            {
                // Sending the email
                var mailMessage = new MailMessage();
                mailMessage.From = new MailAddress("admin@something.com");
                mailMessage.Subject = "This is a test" + props + DateTime.Now ;

                // writes dataset to xml string, stored in a stream
                dss1.WriteXml(stream);
                
                System.Net.Mail.Attachment attachment = new System.Net.Mail.Attachment(stream, "text/csv"); // should be text/xml actually
                mailMessage.Attachments.Add(attachment);
                attachment.Name = "csvtable.csv";
                mailMessage.Body = "Something";
                mailMessage.IsBodyHtml = true;

                mailMessage.To.Add(new MailAddress("user1@something.com"));

                mailMessage.Bcc.Add(new MailAddress("user1@something.com.com"));
                var smtp = new SmtpClient();
                smtp.Host = "host.com";
                smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                smtp.UseDefaultCredentials = false;
                smtp.Credentials = new NetworkCredential("username.com", "password");
                smtp.Port = 999;
                smtp.EnableSsl = true;
                smtp.Send(mailMessage);
            }
        }
        catch (Exception ex)
        {
            string smsg;
            smsg = ex.ToString();
        }

    }
}
于 2021-06-07T17:02:01.073 回答