将嵌套的 select 值检索到数据表 c#
retrieve nested select values into datatable c#
我要从 SQL 数据库中填充数据table。 table 的 header 是项目名称和团队成员。我想从项目 table 和团队成员 User_Projects table 中检索项目名称,并将它们显示在数据 table 中。所有项目名称都已成功显示,但我不知道如何显示每个项目的团队成员。我想要这样的结果:
这是我的代码:
aspx:
<script type="text/javascript">
$(document).ready(function () {
$.ajax({
type: "POST",
dataType: "json",
url: "ProjectMembersService.asmx/GetProjects",
success: function (data) {
var datatableVariable = $('#projects-progress').DataTable({
data: data,
columns: [
{ 'data': 'project_name' },
{ 'data': 'developer_name' },
]
});
}
});
});
</script>
<table id="projects-progress" class="table table-striped projects">
<thead>
<tr>
<th style="width: 20%">Project Name</th>
<th>Team Members</th>
</tr>
</thead>
</table>
ProjectMembersService.asmx:
<%@ WebService Language="C#" CodeBehind="~/App_Code/ProjectMembersService.cs" Class="WebSite1.ProjectMembersService" %>
// using statements
namespace WebSite1
{
public class ProjectMembersService : System.Web.Services.WebService
{
[WebMethod]
public void GetProjects()
{
SqlConnection conn = new SqlConnection("Data Source=RC-8573\SQLEXPRESS;Initial Catalog=DataManagment;Integrated Security=SSPI");
var projects = new List<Projects>();
var developers = new List<Developers>();
using (conn)
{
var cmd = new SqlCommand("getProjects", conn) { CommandType = CommandType.StoredProcedure }; //select all projects inProjects table
conn.Open();
var dr = cmd.ExecuteReader();
while (dr.Read())
{
var Project = new Projects
{
project_number = Convert.ToInt32(dr[1].ToString()),
project_name = dr[2].ToString(),
};
projects.Add(Project); // This ok, it fill the datatable with all project_names
// here I want for each project to retrieve the team members and display them in Team Members column
SqlCommand com = new SqlCommand("SELECT user_name FROM Users WHERE user_id = (SELECT user_id FROM User_Projects WHERE project_number = @project_number)", conn); // retrieve all developers name
SqlDataAdapter da = new SqlDataAdapter(com);
DataSet ds = new DataSet();
da.Fill(ds);
var Developer = new Developers
{
developer_name = ds.Tables[0].Columns["user_name"].ToString();
};
developers.Add(Developer);
}
}
var js = new JavaScriptSerializer();
Context.Response.Write(js.Serialize(projects));
var js2 = new JavaScriptSerializer();
Context.Response.Write(js2.Serialize(developers));
}
}
}
您需要根据您所做的列定义将名为 developer_name
的字段添加到 Project
class。
class Projects
{
public int project_number { get; set; }
public string project_name { get; set; }
public string developer_name { get; set; }
}
并添加集合开发者名称
using (SqlConnection conn = new SqlConnection("Data Source=RC-8573\SQLEXPRESS;Initial Catalog=DataManagment;Integrated Security=SSPI;MultipleActiveResultSets=True"))
{
var projects = new List<Projects>();
conn.Open();
using (var cmd = new SqlCommand("getProjects", conn) { CommandType = CommandType.StoredProcedure })
using (SqlDataReader dr = cmd.ExecuteReader())
{
if (dr.HasRows)
while (dr.Read())
{
var Project = new Projects
{
project_number = Convert.ToInt32(dr[1].ToString()),
project_name = dr[2].ToString(),
};
projects.Add(Project);
// open new sqldatareader for each project_id >> need add MultipleActiveResultSets=True to connectionstring(added)
using (SqlCommand com = new SqlCommand("SELECT user_name FROM Users WHERE user_id IN (SELECT user_id FROM User_Projects WHERE project_number = @project_number)", conn))
{
com.Parameters.Add("@project_number", Project.project_number);
List<string> developerNames = new List<string>();
using (SqlDataReader rdrDev = com.ExecuteReader())
{
if (rdrDev.HasRows)
{
while (rdrDev.Read())
{
developerNames.Add((string)rdrDev["user_name"]);
}
}
}
// set names to project object
Project.developer_name = string.Join(", ", developerNames);
}
}
}
var js = new JavaScriptSerializer();
Context.Response.Write(js.Serialize(projects));
}
我要从 SQL 数据库中填充数据table。 table 的 header 是项目名称和团队成员。我想从项目 table 和团队成员 User_Projects table 中检索项目名称,并将它们显示在数据 table 中。所有项目名称都已成功显示,但我不知道如何显示每个项目的团队成员。我想要这样的结果:
这是我的代码:
aspx:
<script type="text/javascript">
$(document).ready(function () {
$.ajax({
type: "POST",
dataType: "json",
url: "ProjectMembersService.asmx/GetProjects",
success: function (data) {
var datatableVariable = $('#projects-progress').DataTable({
data: data,
columns: [
{ 'data': 'project_name' },
{ 'data': 'developer_name' },
]
});
}
});
});
</script>
<table id="projects-progress" class="table table-striped projects">
<thead>
<tr>
<th style="width: 20%">Project Name</th>
<th>Team Members</th>
</tr>
</thead>
</table>
ProjectMembersService.asmx:
<%@ WebService Language="C#" CodeBehind="~/App_Code/ProjectMembersService.cs" Class="WebSite1.ProjectMembersService" %>
// using statements
namespace WebSite1
{
public class ProjectMembersService : System.Web.Services.WebService
{
[WebMethod]
public void GetProjects()
{
SqlConnection conn = new SqlConnection("Data Source=RC-8573\SQLEXPRESS;Initial Catalog=DataManagment;Integrated Security=SSPI");
var projects = new List<Projects>();
var developers = new List<Developers>();
using (conn)
{
var cmd = new SqlCommand("getProjects", conn) { CommandType = CommandType.StoredProcedure }; //select all projects inProjects table
conn.Open();
var dr = cmd.ExecuteReader();
while (dr.Read())
{
var Project = new Projects
{
project_number = Convert.ToInt32(dr[1].ToString()),
project_name = dr[2].ToString(),
};
projects.Add(Project); // This ok, it fill the datatable with all project_names
// here I want for each project to retrieve the team members and display them in Team Members column
SqlCommand com = new SqlCommand("SELECT user_name FROM Users WHERE user_id = (SELECT user_id FROM User_Projects WHERE project_number = @project_number)", conn); // retrieve all developers name
SqlDataAdapter da = new SqlDataAdapter(com);
DataSet ds = new DataSet();
da.Fill(ds);
var Developer = new Developers
{
developer_name = ds.Tables[0].Columns["user_name"].ToString();
};
developers.Add(Developer);
}
}
var js = new JavaScriptSerializer();
Context.Response.Write(js.Serialize(projects));
var js2 = new JavaScriptSerializer();
Context.Response.Write(js2.Serialize(developers));
}
}
}
您需要根据您所做的列定义将名为 developer_name
的字段添加到 Project
class。
class Projects
{
public int project_number { get; set; }
public string project_name { get; set; }
public string developer_name { get; set; }
}
并添加集合开发者名称
using (SqlConnection conn = new SqlConnection("Data Source=RC-8573\SQLEXPRESS;Initial Catalog=DataManagment;Integrated Security=SSPI;MultipleActiveResultSets=True"))
{
var projects = new List<Projects>();
conn.Open();
using (var cmd = new SqlCommand("getProjects", conn) { CommandType = CommandType.StoredProcedure })
using (SqlDataReader dr = cmd.ExecuteReader())
{
if (dr.HasRows)
while (dr.Read())
{
var Project = new Projects
{
project_number = Convert.ToInt32(dr[1].ToString()),
project_name = dr[2].ToString(),
};
projects.Add(Project);
// open new sqldatareader for each project_id >> need add MultipleActiveResultSets=True to connectionstring(added)
using (SqlCommand com = new SqlCommand("SELECT user_name FROM Users WHERE user_id IN (SELECT user_id FROM User_Projects WHERE project_number = @project_number)", conn))
{
com.Parameters.Add("@project_number", Project.project_number);
List<string> developerNames = new List<string>();
using (SqlDataReader rdrDev = com.ExecuteReader())
{
if (rdrDev.HasRows)
{
while (rdrDev.Read())
{
developerNames.Add((string)rdrDev["user_name"]);
}
}
}
// set names to project object
Project.developer_name = string.Join(", ", developerNames);
}
}
}
var js = new JavaScriptSerializer();
Context.Response.Write(js.Serialize(projects));
}