I’ve got a problem with this SELECT. When I execute it on MS Access, the result is returning right and fully. When I try to execute it via C#, the same query returns nothing. The previous variant of the SELECT was realized by adapter to show the result of select in dataGridView. But when I execute it, I get only an empty table with column names.
private void getByObjectAndPollutant(string Object, string Pollutant,
int firstYear, int lastYear)
{
List<string> result = new List<string>();
// line breaks added without concatenation, for readability.
cmd = new OleDbCommand("SELECT object_name, p_name, mpc, emission_concentration,
[year] FROM Pollutants INNER JOIN (Objects INNER JOIN Emissions
ON Objects.o_id = Emissions.e_o_id) ON Pollutants.p_id = Emissions.e_p_id
WHERE (object_name = '" + Object + "' AND p_name = '" + Pollutant + "' AND
[year] BETWEEN " + firstYear + " AND " + lastYear + ") ;", con);
con.Open();
reader = cmd.ExecuteReader();
while (reader.Read()) {
var myString = reader.GetString(0);
result.Add(myString);
richTextBox1.Text += myString;
}
con.Close();
}
This is how I call it in program:
private void button5_Click(object sender, EventArgs e) {
string localObject = comboBox5.Text.ToString();
string localPollutant = comboBox4.Text.ToString();
int localFirstYear = Convert.ToInt32(comboBox6.Text);
int localLastYear = Convert.ToInt32(comboBox7.Text);
getByObjectAndPollutant(localObject,localPollutant,localFirstYear,localLastYear);
}
This is SELECT query in MS Access:
SELECT object_name, p_name, mpc, emission_concentration, year
FROM Pollutants
INNER JOIN (Objects
INNER JOIN Emissions ON Objects.o_id=Emissions.e_o_id)
ON Pollutants.p_id=Emissions.e_p_id
WHERE object_name="some name" And p_name="some name" And year Between 2011 And 2015;
And this was the previous code of method:
private void getByObjectAndPollutant(string Object, string Pollutant, int firstYear,
int lastYear)
{
con.Open();
DataTable dt = new DataTable();
adapt = new OleDbDataAdapter(this long select);
adapt.Fill(dt);
dataGridView2.DataSource = dt;
con.Close();
}
Main reason I use reader or adapter is to get the result of SELECT query. Is this SELECT returning something? When this works I will then code method to draw chart based on SELECT.