asp.net multiple sqlcommands in one GridView

若如初见. 提交于 2019-12-25 04:33:14

问题


I have problem to show multiple sql command in one GridView. Maybe I don't need two sqlcommands to show from two tables but I don't know how to do. The first command is to get all employees that have vacation between two dates. The second command I am using it to retrieve dates by ID. But I don't know how to Bind them both to one GridView to show as attached image. Thank you in advance.

What I get Now is
Albert 2016-03-16
Albert 2016-03-17

Albert 2016-03-18
Johanna 2016-03-17

Johanna 2016-03-18
Eric 2016-03-18

Instead of
Albert 2016-03-16, 2016-03-17, 2016-03-18
Johanna 2016-03-17, 2016-03-18
Eric 2016-03-18
I think I have to loop between two While statment and maybe with one sqlcommand?

My code is:

    using (SqlConnection con = new SqlConnection(connection))
{
 con.Open();
 SqlCommand cmd = new SqlCommand(" SELECT distinct E.EmployeeId, E.FirstName  
 FROM Employee E INNER JOIN Vacation V ON E.EmployeeId = V.EmployeeId " +
  " WHERE ((V.Dates >= @Start AND V.Dates <= @End) ) ", con);
  cmd.Parameters.AddWithValue("@Start", (Calendar1.SelectedDates[0]).Date.ToShortDateString());
 cmd.Parameters.AddWithValue("@End", (Calendar1.SelectedDates[Calendar1.SelectedDates.Count - 1]).Date.ToShortDateString());
using (SqlDataReader dr = cmd.ExecuteReader())
 {
   while (dr.Read())
    {

 Response.Write((dr[1]).ToString() + "  "); // Cheack if retrivs Employeename
 // Now By Id I want to get all dates belong to specifik employee

SqlCommand cmd2 = new SqlCommand(" SELECT V.Dates FROM Vacation V " +
" WHERE ((V.Dates >= @Start AND V.Dates <= @End) ) ", con);
cmd2.Parameters.AddWithValue("@Start", (Calendar1.SelectedDates[0]).Date.ToShortDateString());
cmd2.Parameters.AddWithValue("@End", (Calendar1.SelectedDates[Calendar1.SelectedDates.Count - 1]).Date.ToShortDateString());
cmd2.Parameters.AddWithValue("@EmployeeId", Convert.ToInt32(dr[0]));                                    
using (SqlDataReader dr2 = cmd2.ExecuteReader())
{
 while (dr2.Read())
    {
    //Response.Write(Convert.ToDateTime(dr2[0]));
     GridView7.DataSource = cmd2.ExecuteReader();
GridView7.DataBind();
    }

   }
       Response.Write("<br/>");
 } 


  }
    con.close();
}
GridView7.DataSource = cmd.ExecuteReader();
GridView7.DataBind();

回答1:


The FOR XML PATH syntax allows your query to group several values in a single one:

SELECT 
    E.EmployeeId, 
    E.FirstName,
    REPLACE(STUFF((
        SELECT 
            COALESCE('¶' + V.Dates, '')
        FROM 
            Vacation V 
        WHERE 
            V.EmployeeId = E.EmployeeId AND V.Dates >= @Start AND V.Dates <= @End
        FOR XML PATH('')), 1, 1, ''), '¶', ', ') AS VacationDates
FROM 
    Employee E

You can replace the ', ' separator by something else if you want.

Note: Sorry for the multipe edits. I am just not sure how you connect the employees, vacations and dates. This piece of code basically shows the idea for the FOR XML PATH syntax.



来源:https://stackoverflow.com/questions/36018174/asp-net-multiple-sqlcommands-in-one-gridview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!