I have a SQl Server database which I have linked to a C# application. The data is bound to a datagridview in the application. This works fine for a simple select from one table command, as shown in the code below. However, I would like the datagridview to show data from more than one table based on a more complex query, but when I try to replace the command below with the complicated query, I get lots of errors:
SELECT DISTINCT f_name, l_name, grade, mentor_name
FROM [Person] P, [Exam] E, [Mentor] M
WHERE P.Person_id = E.Person_id
AND P.Person_id = M.Person_id
I'm open to suggestions as to other ways of representing my data, if anyone has any.
public partial class Form1 : Form {
private BindingSource bindingSource1 = new BindingSource();
public Form1() {
InitializeComponent();
dataGridView1.Dock = DockStyle.Fill;
}
private void Form1_Load(object sender, EventArgs e) {
dataGridView1.DataSource = bindingSource1;
GetData("SELECT * from Person");
}
private void GetData(string command) {
String connectionString = "Data Source=MY-PC\\USER;Initial Catalog=MyDatabase;Integrated Security=True";
SqlDataAdapter dataAdapter = new SqlDataAdapter(command, connectionString);
SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);
DataTable table = new DataTable();
table.Locale = System.Globalization.CultureInfo.InvariantCulture;
dataAdapter.Fill(table);
bindingSource1.DataSource = table;
dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);
}
}
Thanks.