我正在寻找一些我无法解决的问题的帮助。
我创建了一个存储过程来从名为“applicant”的表中选择数据
并且效果很好
这是下面的代码:
USE [Josons]
GO
/****** Object: StoredProcedure [dbo].[PROC_GETALLAPPLICANTS] Script Date: 09/03/2013 10:50:58 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- Batch submitted through debugger: SQLQuery25.sql|7|0|C:\Users\supportadmin\AppData\Local\Temp\~vs29E1.sql
ALTER PROCEDURE [dbo].[PROC_GETALLAPPLICANTS]
AS
BEGIN
select ID,FIRSTNAME,LASTNAME,APPLYDATE from APPLICANT order by ID Asc
END
我添加了一个名为“AF”的新表,这两个表与一个唯一的列相关:“applicant_id”
如何使存储过程从2个表中进行选择,并在一个网格视图中以相同的顺序“ID Asc”显示它
修改
aspx文件:
<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPages/MainMasterPage.master"
AutoEventWireup="true" CodeFile="HR.aspx.cs" Inherits="Pages_HR" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<div>
<asp:Button ID="btnLogout" CssClass="btnLogoutStyle" runat="server"
Text="Logout" onclick="btnLogout_Click" />
</div>
<div class="divgvDisplayAllApplicantsStyle">
<asp:GridView
ID="gvDisplayAllApplicants"
CssClass="gridviewstyle"
runat="server"
OnRowDataBound="gvDisplayAllApplicants_RowDataBound"
AllowPaging="true"
PageSize="10"
OnPageIndexChanging="gvDisplayAllApplicants_PageIndexChanging"
OnSelectedIndexChanged="gvDisplayAllApplicants_SelectedIndexChanged"
DataKeyNames="ID"
Width="940"
AutoGenerateColumns="false">
<Columns>
<asp:ButtonField CommandName="Select" Visible="false" />
<asp:BoundField DataField="ID" HeaderText="ID" ReadOnly="True" />
<asp:TemplateField HeaderText="AF #">
<ItemTemplate>
<%# Eval("AF")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="First Name">
<ItemTemplate>
<%# Eval("FIRSTNAME")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Last Name">
<ItemTemplate>
<%# Eval("LASTNAME")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Apply Date">
<ItemTemplate>
<%# Eval("APPLYDATE")%>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</asp:Content>
和aspx.cs文件:
public partial class Pages_HR : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Session["DisplaySelectedApplicant"] = "DisplaySelectedApplicant";
try
{
if (Session["HR"].ToString() == "" && Request.QueryString["queryStringBackButton"].ToString() == null)
{
Session["DisplaySelectedApplicant"] = "";
Session["HR"] = "";
Response.Redirect("LoginPage.aspx");
}
}
catch (NullReferenceException)
{
Response.Redirect("LoginPage.aspx");
}
}
FillApplicantGridView();
}
#region METHODS
public void FillApplicantGridView()
{
string connstr = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
using (SqlConnection sqlconnection = new SqlConnection(connstr))
{
sqlconnection.Open();
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "PROC_GETALLAPPLICANTS";
cmd.Connection = sqlconnection;
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(cmd);
DataTable datatable = new DataTable();
sqlDataAdapter.Fill(datatable);
gvDisplayAllApplicants.DataSource = datatable;
gvDisplayAllApplicants.DataBind();
sqlconnection.Close();
}
}
}
protected void gvDisplayAllApplicants_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// Get reference to button field in the gridview.
LinkButton _singleClickButton = (LinkButton)e.Row.Cells[0].Controls[0];
string _jsSingle = ClientScript.GetPostBackClientHyperlink(_singleClickButton, "Select$" + e.Row.RowIndex);
e.Row.Style["cursor"] = "hand";
e.Row.Attributes["onclick"] = _jsSingle;
e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#93A3B0'; this.style.color='White'; this.style.cursor='pointer'");
e.Row.Attributes.Add("onmouseout", String.Format("this.style.color='Black';this.style.backgroundColor='White';", gvDisplayAllApplicants.RowStyle.BackColor.ToKnownColor()));
}
}
}
protected void gvDisplayAllApplicants_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow selectedRow = gvDisplayAllApplicants.SelectedRow;
int rowIndex = selectedRow.RowIndex;
string Applicant_ID = gvDisplayAllApplicants.DataKeys[rowIndex].Values["ID"].ToString();
Response.Redirect("DisplaySelectedApplicant.aspx?ID=" + Applicant_ID);
}
protected override void Render(HtmlTextWriter writer)
{
foreach (GridViewRow row in gvDisplayAllApplicants.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
ClientScript.RegisterForEventValidation(((LinkButton)row.Cells[0].Controls[0]).UniqueID, "Select$" + row.RowIndex);
}
}
base.Render(writer);
}
protected void gvDisplayAllApplicants_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gvDisplayAllApplicants.PageIndex = e.NewPageIndex;
gvDisplayAllApplicants.ShowFooter = false;
gvDisplayAllApplicants.EditIndex = -1;
FillApplicantGridView();
}
#endregion
protected void btnLogout_Click(object sender, EventArgs e)
{
Response.Redirect("LoginPage.aspx");
}
}
Thanks,
答案 0 :(得分:0)
ALTER PROCEDURE [dbo].[PROC_GETALLAPPLICANTS] AS
BEGIN
select a.ID,a.FIRSTNAME,a.LASTNAME,a.APPLYDATE,af.* from APPLICANT a join AF af on a.applicant_id = af.applicant_id order by a.ID Asc
END
答案 1 :(得分:0)
如果您想要两个表都有application_id的所有记录,请使用INNER JOIN
...
SELECT ap.ID ,
ap.FIRSTNAME ,
ap.LASTNAME ,
ap.APPLYDATE ,
AF.FieldNames
FROM APPLICANT ap
INNER JOIN AF ON ap.applicant_id = AF.applicant_id
ORDER BY ap.ID ASC
如果您想要退回所有申请人,即使AF中没有对应关系,请使用LEFT JOIN
...
SELECT ap.ID ,
ap.FIRSTNAME ,
ap.LASTNAME ,
ap.APPLYDATE ,
AF.FieldNames
FROM APPLICANT ap
LEFT JOIN AF ON ap.applicant_id = AF.applicant_id
ORDER BY ap.ID ASC
如果没有记录,则使用ISNULL的示例......
SELECT ap.ID ,
ap.FIRSTNAME ,
ap.LASTNAME ,
ap.APPLYDATE ,
ISNULL(AF.AppFormCourseName, 'No Application as of yet')
FROM APPLICANT ap
LEFT JOIN AF ON ap.applicant_id = AF.applicant_id
ORDER BY ap.ID ASC
考虑更好地调用AF表,我猜它可能是 ApplicationForm ?
请参阅此处有关Inner Join和Left Join的说明。