My Access query is returning fields out of order. It was my understanding that for each row from the query result the fields would be in the same order that were specified in the query’s SELECT statement. What am I doing wrong?
String query =
"SELECT " +
"bas.[BAS BACnet Object Type/Instance], " +
"bas.[BAS BACnet Object Name], " +
"bas.[BAS Point List Description], " +
"ore.[ORE Data Direction], " +
"ref.[ENUM_H], " +
"yk.[CCC Max Value (eng units)], " +
"yk.[CCC Min Value (eng units)], " +
"yk.[CCC Enum/Data Set], " +
"ore.[ORE COV Increment], " +
"ore.[ORE Display Precision] " +
"FROM (([OV2 BAS] AS bas " +
"INNER JOIN [OV2 ORE] AS ore ON bas.[Ref ID] = ore.[Ref ID]) " +
"INNER JOIN [OV2 RefID] AS ref ON bas.[Ref ID] = ref.[Ref ID]) " +
"INNER JOIN [YK CAPP] AS yk ON bas.[Ref ID] = yk.[Ref ID] " +
"WHERE bas.[BAS BACnet Object Type/Instance] <> '';";
try
{
cmd = new OleDbCommand(query, this._conn);
reader = cmd.ExecuteReader();
if (reader.HasRows == false)
{
Exception e = new Exception("Read of mapping table returned no results.");
throw e;
}
while (reader.Read() != false)
{
Int32 columns;
Object[] fields = new Object[10];
columns = reader.GetValues(fields);
avClass = new AVClass();
for (int i = 0; i < AVClass.AV_CLASS_PROPERTIES; i++)
{
avClass.Properties[i] = new AVProperty((AVProperty.PROPERTY_ID)i, fields[i]);
results.Add(avClass);
}
}
}
catch (Exception e)
{
Console.WriteLine("ERROR: " + e.Message);
Console.WriteLine(e.ToString());
}
Well it’s not clear whether it’s the only problem, but this looks wrong to me:
You’re adding the same reference (
avClass) to your results list for every field. I suspect you want to put thatAddcall after the loop.Also, you haven’t shown what
AVProperty.PROPERTY_IDis, but you’d need make sure that the SELECT part of the query matches that. Presumably it’s an enum… so you’d need the enum value corresponding to “bas.[BAS BACnet Object Type/Instance]” to have value 0, etc.(If you can give more of the relevant code, we may be able to help you more. I’d also strongly recommend that you go through your code and make your names more self-explanatory and idiomatic. .NET doesn’t use SHOUTY_CAPS for names.)
Additionally:
usingstatements for any of the objects which should be disposedGetValues.