peter piper

peter piper

  • NA
  • 22
  • 3.9k

How to create master detail Select query

Feb 12 2017 5:40 PM
I have a form and 2 sql tables. Table 1 contains header info and table 2 details info
A button that starts the process of writing data from the tables to an application.

private void button1_Click(object sender, EventArgs e)
{
using (SqlCommand cmd = new SqlCommand(@SELECT col1,col2,col3,col4 FROM tbl1,conn))
using (var rdr = cmd.ExecuteReader())
{
while (rdr.Read())
{
writeHeadData(rdr[“col1”], rdr[“col2”], rdr[“col3”], rdr[“col3”],rdr[“col4”]);
}
}


private void writeHeadData(string col1,string col2,string col3,string col4)
{
//do something here (writeHeadData)
writeDetailData(col1, col2, col3, col4)
}

private void writeDetailData(string col1,string col2,string col3,string col4)
{
//do something here with data from the details table
}

Where do I put the query from the detail table (see below) and ensure that the detail info relates to the header info using the WHERE clause

using (SqlCommand cmd = new SqlCommand(@SELECT * FROM tbl2,conn))
using (var rdr = cmd.ExecuteReader())
while (rdr.Read())
{
writeHeadData(rdr[“col1”], rdr[“col2”], rdr[“col3”], rdr[“col3”],rdr[“col4”]);
}

There are 3 details rows for every header row

I hope whoever reads this understands what I’am trying to achieve and if not clear please tell me and I shall try and elucidate further

Answers (11)