如何从另一个表中显示列的值

时间:2013-11-16 02:13:39

标签: c# asp.net sql

如何在网络表格表格中显示DepartmenttblDepartmenttblEmployee列的值?我有这段代码,但它只显示DeptID

这是Page_load代码

        sConn = new SqlConnection(sStr);
        daEmp = new SqlDataAdapter("SELECT * FROM tblEmployee", sConn);
        daDep = new SqlDataAdapter("SELECT * FROM tblDepartment", sConn);
        dsEmp = new DataSet();
        dsDep = new DataSet();

        daEmp.Fill(dsEmp, "tblEmployee");
        daDep.Fill(dsDep, "tblDepartment");

        dsEmp.Tables["tblEmployee"].PrimaryKey = new DataColumn[] { dsEmp.Tables["tblEmployee"].Columns["EmployeeID"] };

        DataTable dt = new DataTable();

        dgvEmployee.DataSource = dsEmp.Tables["tblEmployee"];
        dgvEmployee.DataBind();

这些是表格

enter image description here

1 个答案:

答案 0 :(得分:1)

当您需要显示来自两个或多个不同tables的信息时,您可以join根据关系(primary keyforeign key)列显示这些信息:

您需要替换此查询:

SELECT * FROM tblEmployee

有了这个:

SELECT employee.*,department.Department 
FROM tblEmployee employee 
    INNER JOIN tblDepartment department ON employee.DeptID=department.DeptID