SQL查询-如何使用gridview的XML路径查询显示单行
本文关键字:查询 XML 路径 单行 显示 gridview 何使用 SQL | 更新日期: 2023-09-27 18:11:30
我有3个表,我从(ModuleStaff
, Staff
, Roles
)检索。我打算做的是在单行中显示gridview中的数据。例如,负责组号1和组号2的staff(adrian)
显示为:
StaffName | RoleName | GroupNumber
Adrian | Lecturer | 1,2
现在,我有相同的staff(adrian)
问题,但有不同的角色名称,有groupnumbers (1 and 2)
,即使它的数据库数据为导师和领导者空白。
我现在得到了什么:
StaffName | RoleName | GroupNumber
Adrian | Lecturer | 1,2
Adrian | Tutor | 1,2
Adrian | Leader | 1,2
应该是什么:
StaffName | RoleName | GroupNumber
Adrian | Lecturer | 1,2
Adrian | Tutor |
Adrian | Leader |
下面是我的代码:
public DataTable DisplayModuleStaffGroup(string year, string module, string diploma)
{
try
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MARSConnectionString"].ConnectionString))
{
using (SqlCommand cmd = con.CreateCommand())
{
cmd.CommandText = "SELECT ms.ModuleCode, ms.ModuleYear, ms.DiplomaID, sr.RoleName, s.StaffName, s.StaffStatus, GroupNumber = ";
cmd.CommandText += " substring((SELECT ( ', ' + GroupNumber ) FROM ModuleStaff ms2, Staff s2 ";
cmd.CommandText += " WHERE ms.StaffID = ms2.StaffID AND ms2.StaffID = s2.StaffID ";
cmd.CommandText += " ORDER BY s2.StaffName, ms2.GroupNumber ";
cmd.CommandText += " FOR XML PATH( '' )), 3, 1000) ";
cmd.CommandText += " FROM ModuleStaff ms ";
cmd.CommandText += " INNER JOIN StaffRoles sr ON ms.RoleID = sr.RoleID ";
cmd.CommandText += " INNER JOIN Diploma d ON ms.DiplomaID = d.DiplomaID ";
cmd.CommandText += " INNER JOIN Staff s ON ms.StaffID = s.StaffID ";
cmd.CommandText += " WHERE (ms.DiplomaID = @dip) AND (ms.ModuleYear = @year) AND (ms.ModuleCode = @code) ";
cmd.CommandText += " ORDER BY ms.RoleID DESC ";
cmd.Parameters.AddWithValue("@dip", diploma);
cmd.Parameters.AddWithValue("@year", year);
cmd.Parameters.AddWithValue("@code", module);
DataSet dSet = new DataSet();
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
da.Fill(dSet, "DisplayModuleStaffGroup");
return dSet.Tables["DisplayModuleStaffGroup"];
}
}
}
}
catch
{
throw;
}
finally
{
}
}
我不太确定我应该如何查询XML路径/sql语句给我基于rolename
和staffname
的组号。有人能帮我一下吗?谢谢你。
您可以在sql中遍历循环。下面是你可以应用
的想法declare @totalStaff int
declare @totalRoles int
declare @staffCount int
declare @roleCount int
declare @staffId int
declare @staffName varchar(max)
declare @roleId int
declare @roleName varchar(max)
declare @groupCount int
declare @table table(staffName varchar(max),roleName varchar(max),roles varchar(max))
set @staffCount=1
set @roleCount=1
set @totalStaff=(select count(*) from staff)
while(@staffCount<=@totalStaff)
begin
set @staffId=select a.ID from
(select row_number() over(order by ID) as sr,ID from satff)a
where a.sr=@staffCount
set @staffName=(select staffName from staff where staffId=@staffId)
set @totalRoles=(select count(*) from roles where staffId=@staffId)
while(@roleCount<=@totalRoles)
begin
set @roleId=select a.ID from
(select row_number() over(order by ID) as sr,ID from roles)a
where a.sr=@roleCount
set @roleName=(select roleName from roles where roleId=@roleId)
--now same as above go through the loop for group
end
end