Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将列表中的值显示在电子邮件行或非电子邮件行上_C#_List - Fatal编程技术网

C# 将列表中的值显示在电子邮件行或非电子邮件行上

C# 将列表中的值显示在电子邮件行或非电子邮件行上,c#,list,C#,List,所以我试图将从数据库中获取的数据显示在电子邮件中。问题是,我将数据库中的每一列都放在一个列表中,它将所有值放在一行中。我想知道,如果我有3个值,我会将这3个值显示在彼此下面,这是怎么可能的。我所做的表只是循环,一直这样做,直到列表的所有值都放在每一行下面。下面是我的代码 using System; using System.Collections.Generic; using System.Data.SqlClient; using System.Linq; using System.Text;

所以我试图将从数据库中获取的数据显示在电子邮件中。问题是,我将数据库中的每一列都放在一个列表中,它将所有值放在一行中。我想知道,如果我有3个值,我会将这3个值显示在彼此下面,这是怎么可能的。我所做的表只是循环,一直这样做,直到列表的所有值都放在每一行下面。下面是我的代码

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Mail;
using System.Net;

namespace sql_connection
{
class Program
 {
static void Main(string[] args) 
{





    string conn = null;
    SqlConnection connection;
    conn= ("Data Source=Database\\SQL2012;Initial Catalog=jobs;User ID=user;Password=passs");

    connection = new SqlConnection(conn);
    try{

        connection.Open();
        Console.WriteLine("Connection Open!");
        SqlCommand cmd = new SqlCommand("SELECT jobs.[dbo].[tb_work].whd_Date,jobs.[dbo].[tb_work].whd_FromTime,jobs.[dbo].[tb_work].whd_ToTime, jobs.[dbo].[tb_work].whd_User,jobs.[dbo].[tb_UserLogin].login_Email FROM jobs.[dbo].[tb_work]INNER JOIN jobs.[dbo].[tb_UserLogin] ON jobs.[dbo].[tb_work].whd_User = jobs.[dbo].[tb_UserLogin].login_LoginId WHERE  DATEDIFF(DAY,[whd_FromTime],GETDATE())<=7 AND   (whd_ToTime = '' OR whd_ToTime IS NULL) AND(whd_User=login_LoginId)");
        cmd.Connection = connection;
        SqlDataReader reader = cmd.ExecuteReader();
        var columns = Enumerable.Range(0, reader.FieldCount).Select(reader.GetName).ToList();

        var list = new List<string>();
        var col = new List<string>();

        while(reader.Read())
        {



            var s = string.Format(" {1}     {0}         {2}       {3} ",
               reader["whd_ToTime"] == DBNull.Value? "NULL" : reader["whd_ToTime"].ToString(), 
               reader["whd_FromTime"] == DBNull.Value? "NULL" : reader["whd_FromTime"].ToString(), 

               reader["whd_User"].ToString(),
               reader["login_Email"].ToString());

              Console.WriteLine(string.Join("   ", columns.ToArray()));

            Console.WriteLine(s);
            list.Add(s);




        }

        var sb = new StringBuilder();
        foreach (var s in list)
        {
            sb.AppendLine(s);
        }




        connection.Close();


        MailMessage message; 

        message=new MailMessage();
         MailAddress to = new MailAddress("xxxx@gmail.com");


        MailAddress from = new MailAddress("xxxx@gmail.com");

        MailMessage mail = new MailMessage(from, to);


        mail.Subject = ("missed punch clock");

    message.IsBodyHtml = true;


            StringBuilder html = new StringBuilder();

            html.AppendFormat("<!DOCTYPE html>");
            html.AppendFormat("<html><body><table>");
            html.AppendFormat("<tr><td>");

             html.Append("<table width=600px border=1 cellspacing=2 cellpadding=2 align=center bgcolor=White dir=ltr rules=all style=border-width: thin; border-style: solid; line-height: normal; vertical-align: baseline; text-align: center; font-family: Calibri; font-size: medium; font-weight: normal; font-style: normal; font-variant: normal; color: #000000; list-style-type: none;>");
             for (int rowind = 0; rowind < 1; rowind++)
             {
                 html.Append("<tr>");
                 html.Append("<td>");
                 html.Append("<table width=600px border=1 cellspacing=2 cellpadding=2 align=center bgcolor=White dir=ltr rules=all style=border-width: thin; border-style: solid; line-height: normal; vertical-align: baseline; text-align: center; font-family: Calibri; font-size: medium; font-weight: normal; font-style: normal; font-variant: normal; color: #000000; list-style-type: none;>");

                 for (int newrowind = 0; newrowind < 1; newrowind++)
                 {


                     html.AppendFormat("<tr>");
                     html.Append("<td colspan=1  style=font-weight:bold>");
                     html.Append("whd_FromTime");
                     html.Append("</td>");
                     html.Append("<td colspan=2 style=font-weight:bold>");
                     html.Append("whd_ToTime");
                     html.Append("</td>");
                     html.Append("<td colspan=3 style=font-weight:bold>");
                     html.Append("whd_User");
                     html.Append("</td>");
                     html.Append("</tr>");

                         html.AppendFormat("<tr>");

                         html.Append("<td colspan=1  style=font-weight:bold>");
                         for(int i=0;i <col.Count;i++) 
                         {
                             html.Append("</br>");
                             html.Append(pb.ToString());
                             html.Append("</br>");

                         }

                           html.Append("</td>");


                         html.Append("<td colspan=2  style=font-weight:bold>");
                         html.Append(sb.ToString());

                         html.Append("</td>");
                         html.Append("<td colspan=3  style=font-weight:bold>");
                         html.Append(cb.ToString());


                         html.Append("</td>");    
                     html.Append("</tr>");



                 }

                 html.Append("</tr>");
                 html.Append("</td>");
                 html.Append("</table>");
             }
             html.Append("</table>");
             html.AppendFormat("</td></tr>");
             html.AppendFormat("</table></html></body>");


                mail.Body= html.ToString();
                mail.IsBodyHtml = true;          SmtpClient smtp = new SmtpClient();
        smtp.Host = "smtp.gmail.com";
        smtp.Port = 587;

        smtp.Credentials = new NetworkCredential(
            "xxxx"gmail.com", xxxxxxxx");
        smtp.EnableSsl = true;
        Console.WriteLine("Sending email..");
        smtp.Send(mail);
    } 




    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }





}
}
 }
使用系统;
使用System.Collections.Generic;
使用System.Data.SqlClient;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Net.Mail;
Net系统;
名称空间sql\u连接
{
班级计划
{
静态void Main(字符串[]参数)
{
字符串conn=null;
SqlConnection连接;
conn=(“数据源=数据库\\SQL2012;初始目录=作业;用户ID=用户;密码=密码”);
连接=新的SqlConnection(conn);
试一试{
connection.Open();
Console.WriteLine(“连接打开!”);

SqlCommand cmd=new-SqlCommand(“选择作业。[dbo].[tb_工作]).whd_日期,作业。[dbo].[tb_工作].whd_FromTime,作业。[dbo].[tb_工作].whd_用户,作业。[dbo].[tb_用户登录].login来自作业的电子邮件。[dbo].[tb_工作]内部联接作业。[dbo].[tb_用户登录]关于作业。[dbo].[tb_工作].whd_用户登录].login\u LoginId其中DATEDIFF(DAY,[whd\u FromTime],GETDATE())为什么你要重复你自己?因为我想完成这个,搜索了整个实习生,在每个下的列表中显示每个值,但是没有人回复,所以我用新代码做了一个新的,也许有人可以帮我做。这并不意味着你应该问两次-你可以编辑你的第一个问题。好的,很高兴知道。现在b有没有可能你知道如何为列表中的每个值创建一个新行并放入一个值?值?