I have this table in my DB (record ID is the same for 1 Student, it increments by 1 automatically for different students):
id | firstName | lastName | subject | grade | recordID |
----+-----------+----------+---------+-------+----------+
1 | John | Doe | 1 | A | 1 |
1 | John | Doe | 2 | B | 1 |
3 | Max | Smith | 1 | C | 2 |
using C# I want to save data for id = 1 into a string in this format:
Name: John Doe
Details: 1A; 2B
Name: Max Smith
Details: 1C
what I've done so far is:
SqlCommand cmd = _connection.CreateCommand();
string res = null;
cmd.CommandText = "SELECT COUNT(DISTINCT recordID) FROM table1";
int numb = Convert.ToInt32(cmd.ExecuteScalar().ToString());
int currentRecord = 1;
for (int i = 0; i < numb; i++)
{
cmd.CommandText = "SELECT firstname, lastname FROM table1 WHERE recordID="+currentRecord+";";
res += "Name: " + cmd.ExecuteScalar().ToString() + "\n Details: ";
cmd.CommandText = "SELECT subject, grade FROM table1 WHERE recordID="+currentRecord+";";
res += "Details: " + cmd.ExecuteScalar().ToString() + "\n";
currentRecord++
}
This always saves the first record in a string, like this
Name: John
Details: 1
Name: Max
Details: 1
though I need to save multiple rows and columns. please help!