2

从子行检索数据时出现错误。基本上我需要lecturerNamefrom tbl_lecturerprojectTitlefrom tbl_lecturer_project

这是我的代码。

//get data from 3 tables
DataSet ds = new DataSet(); // .xsd file name
DataTable dt = new DataTable();

//Connection string replace 'databaseservername' with your db server name
SqlConnection con = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand();
SqlDataAdapter adapter;

con.Open();

//Stored procedure calling. It is already in sample db.
string selectSQL = "SELECT * FROM tbl_allocated_project where allocatedGroupId='" + team + "'";
cmd = new SqlCommand(selectSQL, con);
ds = new DataSet();
adapter = new SqlDataAdapter(cmd);
adapter.Fill(ds, "tbl_allocated_project");

cmd.CommandText = "SELECT * FROM tbl_lecturer_project";
adapter.Fill(ds, "tbl_lecturer_project");

cmd.CommandText = "SELECT * FROM tbl_lecturer";
adapter.Fill(ds, "tbl_lecturer");

DataRelation dr1 = new DataRelation("dr1", ds.Tables["tbl_lecturer"].Columns["lecturerId"],ds.Tables["tbl_lecturer_project"].Columns["lecturerId"]);
DataRelation dr2 = new DataRelation("dr2", ds.Tables["tbl_lecturer_project"].Columns["lecturerProjectId"], ds.Tables["tbl_allocated_project"].Columns["allocatedProjectId"]);
ds.Relations.Add(dr1);
ds.Relations.Add(dr2);

foreach (DataRow row in ds.Tables["tbl_allocated_project"].Rows)
{
    lblDisplay.Text = "";
    //lblDisplay.Text += row["allocatedGroupId"];
    //lblDisplay.Text += " " + row["intro"];

    foreach (DataRow col in row.GetChildRows(dr2))
    {
        DataRow rowdata = col.GetParentRows(dr1)[0];
        //lblDisplay.Text += "  ";
        lblDisplay.Text += rowdata["projectTitle"];
    }
}

我收到此错误:

GetChildRows 需要其表为 的行tbl_lecturer_project,但指定行的表为tbl_allocated_project

请帮忙。

4

1 回答 1

0

应该这样:

DataRelation dr2 = new DataRelation("dr2", ds.Tables["tbl_lecturer_project"].Columns["lecturerProjectId"], ds.Tables["tbl_allocated_project"].Columns["allocatedProjectId"]);

是这样的:

DataRelation dr2 = new DataRelation("dr2", ds.Tables["tbl_allocated_project"].Columns["lecturerProjectId"], ds.Tables["tbl_lecturer_project"].Columns["allocatedProjectId"]);

这将使, 这应该允许您调用它tbl_allocated_project的父级。tbl_lecturer_projectGetChildRows

于 2013-05-09T00:06:00.750 回答