我有問題,以顯示一個GridView中的多個sql命令。也許我不需要兩個sqlcommands來顯示兩個表,但我不知道該怎麼做。 第一個命令是讓所有員工在兩個日期之間休假。 我用它通過ID檢索日期的第二個命令。但我不知道如何將它們綁定到一個GridView以顯示爲附加圖像。先謝謝你。asp.net多個sqlcommands在一個GridView
我能得到什麼現在是
阿爾貝2016年3月16日
阿爾貝2016年3月17日
阿爾貝2016年3月18日
約翰娜2016年3月17日
約翰娜2016-03- 18
埃裏克·2016年3月18日
而不是
阿爾貝2016年3月16日,2016年3月17日,2016年3月18日
喬hanna 2016-03-17,2016-03-18
Eric 2016-03-18
我想我必須在兩個while語句之間循環,也許用一個sqlcommand?
我的代碼是:
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();
您是否需要將信息作爲列處理?爲什麼不使用他們在單個nvarchar(max)列中進行假期的所有日期,並在它們之間添加逗號?我這樣說是因爲你試圖達到這個目標的方式會變得一團糟,因爲所有的日期都不匹配僱員,你可以使用一個動態的SQL數據透視表,但這會讓你留下一堆空(或空白)空格。 – thepanch
@thepanch感謝您的回覆但我需要在表格或GridView中顯示結果。那可能嗎?對於動態SQL數據透視表,即使這樣也會留下一堆空(或空白)空格。那可能嗎?再次感謝你。 –
@thepanch:我的代碼工作,如果我使用Response.Write(.....)例如: With Response.Write((dr [1])。ToString()+「」); And Response.Write(Convert.ToDateTime(dr2 [0])); 我可以根據需要進行修改,但我不希望它按原樣寫入頁面。 我想在一個表中顯示它們,GridView或動態SQL數據透視表 –