ASP.NET在一个GridView中显示多个表中的数据

本文关键字:显示 数据 GridView 一个 NET ASP | 更新日期: 2023-09-27 18:15:46


我正试图在GridView中显示以下代码,但我不知道该怎么做,或者我不知道我做得是否正确。。。。请帮忙!

string cs = ConfigurationManager.ConnectionStrings["MyDb"].ConnectionString;
using (SqlConnection connection = new SqlConnection(cs))
{
    SqlCommand cmd = new SqlCommand((@"select e.FirstName,
                        count(case when v.VacationDates = @day1 THEN 1 END),
                        count(case when v.VacationDates = @day2 THEN 1 END),
                        count(case when v.VacationDates = @day3 THEN 1 END),
                        count(case when v.VacationDates = @day4 THEN 1 END),
                        count(case when v.VacationDates = @day5 THEN 1 END),
                        count(case when v.VacationDates = @day6 THEN 1 END),
                        count(case when v.VacationDates = @day7 THEN 1 END)
                        from Employee e
                        left join Vacation v
                        on e.EmployeeId = v.EmployeeId
                        group by e.FirstName"), connection) ;
 if (Calendar1.SelectedDates.Count > 6)
                {
                    cmd5.Parameters.AddWithValue("@day1", Calendar1.SelectedDates[0].ToShortDateString());
                    cmd5.Parameters.AddWithValue("@day2", Calendar1.SelectedDates[1].ToShortDateString());
                    cmd5.Parameters.AddWithValue("@day3", Calendar1.SelectedDates[2].ToShortDateString());
                    cmd5.Parameters.AddWithValue("@day4", Calendar1.SelectedDates[3].ToShortDateString());
                    cmd5.Parameters.AddWithValue("@day5", Calendar1.SelectedDates[4].ToShortDateString());
                    cmd5.Parameters.AddWithValue("@day6", Calendar1.SelectedDates[5].ToShortDateString());
                    cmd5.Parameters.AddWithValue("@day7", Calendar1.SelectedDates[6].ToShortDateString());


                        connection.Open();
                        GridView12.DataSource = cmd5.ExecuteReader();
                        GridView12.DataBind();

               }
}

Calendar1.SelectedDates[0]。ToShortDateString((Calendar1.SelectedDates[1].ToShortDateString((…是从日历中获取的值。非常感谢!

ASP.NET在一个GridView中显示多个表中的数据

我测试了你的代码,它似乎是正确的,但你有一个问题需要处理,那就是如果用户没有选择查询所需的七(7(个日期,程序将抛出"System.ArgumentOutOfRangeException"。为了避免"System.ArgementOutOfrangeException"异常,我建议你包括:

if (Calendar1.SelectedDates.Count > 6)
{
(your code)
}