通过从两个不同的表中选择数据来绑定到网格?

时间:2012-02-29 09:10:33

标签: asp.net sql-server-2008 c#-4.0

我的数据库中有两个表名为...请求和余额跟踪器没有任何关系....但我想从两个表中选择数据并将其分为两个网格......

          EmpID   |EmpRqsts|EmpDescription|ApproverID
           1      |asdfsb  |sadbfsbdf     |1
           2      |asbfd   |sjkfbsd       |1

余额跟踪器

          EmpId|BalanceAmnt|LastUpdated
         | 1   |5000       |sdfbk
         | 2   |3000       |sjbfsh

员工表

      EmpId|EmpName
       1   |Anil
       2   |Raghu

现在Balance tracker有EmployeeTable的ForeignKey列...我想要的是从[Requests]中选择EmpTable [EmpRqsts]中的[EmpName]和从[Balance Tracker]中选择[BalanceAmnt] [LastUpdated]并将其绑定到grid < / p>

这是我用来检索数据的存储过程

create procedure SP_GetEmployeeRequests
    (@ApproverName varchar (50))
as
begin
    select 
         EmployeeDetails.Emp_Username,
         RequestDetails.Request_Amount,
         RequestDetails.Request_description,
         BalanceTracker.Balance_Amount,
         BalanceTracker.LastApproval,
         BalanceTracker.LastUdated
     from 
         EmployeeDetails, RequestDetails, BalanceTracker
     where 
         EmployeeDetails.Emp_ID = RequestDetails.Emp_ID
         and BalanceTracker.Emp_ID = RequestDetails.Emp_ID
         and RequestDetails.Approved_ID = (select Approved_ID 
                                           from ApprovalDetails  
                                           where Approved_By = @ApproverName)
end

此处一切正常,但此查询仅检索列名称。但不是表格的价值......任何人都可以帮我解决我的错误。

2 个答案:

答案 0 :(得分:1)

我解决了我的回答,感谢那位试图帮助我的人。我正在发布我的答案,以便能够帮助任何有需要的人......

 CREATE procedure SP_GetEmployeeRequests
(
     @ApproverName varchar (50)
)
AS
BEGIN

SELECT PTS_Employee.Emp_Username, PTS_Requests.Request_RequestedAmount, PTS_Requests.Request_Description, PTS_BalanceTracker.Balance_BalanceAmount, PTS_BalanceTracker.Balance_LastApproval, PTS_BalanceTracker.Balance_LastUpdated
FROM PTS_Employee  JOIN PTS_Requests  ON PTS_Employee.Emp_ID = PTS_Requests.Emp_ID 
JOIN PTS_BalanceTracker  ON PTS_BalanceTracker.Emp_ID = PTS_Requests.Emp_ID
JOIN PTS_Approval  ON PTS_Approval.Approval_ApprovedID  = PTS_Requests.Approval_ApprovedID
WHERE PTS_Approval.Approval_ApprovedBY = @ApproverName 

END
GO

答案 1 :(得分:0)

试试这个:

SqlDataAdapter da = new SqlDataAdapter("select t1.EmpName,t2.EmpRqsts,t3.BalanceAmnt,t3.LastUpdated from EmpTable as t1,Requests as t2,Balance_tracker as t3 where t1.sno=t2.sno and t1.sno=t3.sno", conn);
            DataTable dt1 = new DataTable();
            da.Fill(dt1);
            GridView1.DataSource = dt1;
            GridView1.DataBind();