如何在网络表格表格中显示Department
到tblDepartment
列tblEmployee
列的值?我有这段代码,但它只显示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();
这些是表格
答案 0 :(得分:1)
当您需要显示来自两个或多个不同tables
的信息时,您可以join
根据关系(primary key
和foreign key
)列显示这些信息:
您需要替换此查询:
SELECT * FROM tblEmployee
有了这个:
SELECT employee.*,department.Department
FROM tblEmployee employee
INNER JOIN tblDepartment department ON employee.DeptID=department.DeptID