ms-accessoledbconnectionoledbcommandoledbdatareader

MS Access Sql query errors with or without brackets


I normally work in C# with a SQL Server Express database, but need to duplicate some queries using an MS Access database in another application.

In one of my methods, I am using an INNER JOIN to join 2-tables. When I run my original query, the first query shown below, it throws the following error.

IErrorInfo.GetDescription failed with E_FAIL(0x80004005).

In investigating this error, it suggested that I place brackets around the column names (perhaps even the table name).

However, when the code reaches my OleDbDataReader, it then throws a different error 'Invalid bracketing of column name'.

I have tried 3-times to change the structure of my query, as shown in versions #2 and #3. However, it continues to error.

Please suggest the correct way to form this parameterized query. Original query#1

using (OleDbConnection conn = new OleDbConnection(connectionString))
{
    conn.Open();
    using (OleDbCommand cmdGetPhone = new OleDbCommand(
        "SELECT s.GroupName, s.SpkrName, s.SpkrHomePhone, s.SpkrCellPhone, aIN.SpkrIN, aIN.GroupIN FROM Speakers AS s INNER JOIN AssignmentsIN AS aIN ON aIN.SpkrIN = s.SpkrName AND aIN.GroupIN = s.GroupName AND s.SpkrName = @SpkrName AND s.Local = @Local AND s.Visiting = @Visiting", conn))
    {
        cmdGetPhone.Parameters.Add("@SpkrName", OleDbType.VarWChar).Value = speaker;
        cmdGetPhone.Parameters.Add("@GroupIN", OleDbType.VarWChar).Value = group;
        cmdGetPhone.Parameters.Add("@Local", OleDbType.VarWChar).Value = local;
        cmdGetPhone.Parameters.Add("@Visiting", OleDbType.VarWChar).Value = visiting;
        using (OleDbDataReader reader = cmdGetPhone.ExecuteReader())
        {
            while (reader.Read())

Query#2

using (OleDbConnection conn = new OleDbConnection(connectionString))
{
    conn.Open();
    using (OleDbCommand cmdGetPhone = new OleDbCommand(
        "SELECT [Speakers.GroupName], [Speakers.SpkrName], [Speakers.SpkrHomePhone], [Speakers.SpkrCellPhone], [AssignmentsIN.SpkrIN], [AssignmentsIN.GroupIN] FROM Speakers INNER JOIN AssignmentsIN ON [AssignmentsIN.SpkrIN] = [Speakers.SpkrName] AND [AssignmentsIN.GroupIN] = [Speakers.GroupName] AND [Speakers.SpkrName] = @SpkrName AND [Speakers.Local] = @Local AND [Speakers.Visiting] = @Visiting", conn))
    {
        cmdGetPhone.Parameters.Add("@SpkrName", OleDbType.VarWChar).Value = speaker;
        cmdGetPhone.Parameters.Add("@GroupIN", OleDbType.VarWChar).Value = group;
        cmdGetPhone.Parameters.Add("@Local", OleDbType.VarWChar).Value = local;
        cmdGetPhone.Parameters.Add("@Visiting", OleDbType.VarWChar).Value = visiting;
        using (OleDbDataReader reader = cmdGetPhone.ExecuteReader())
        {
            while (reader.Read())

Query#3

using (OleDbConnection conn = new OleDbConnection(connectionString))
{
    conn.Open();
    using (OleDbCommand cmdGetPhone = new OleDbCommand(
        "SELECT s.GroupName, s.SpkrName, s.SpkrHomePhone, s.SpkrCellPhone, aIN.SpkrIN, aIN.GroupIN FROM (Speakers AS s INNER JOIN AssignmentsIN AS aIN ON aIN.SpkrIN = s.SpkrName AND aIN.GroupIN = s.GroupName AND s.SpkrName = @SpkrName AND s.Local = @Local AND s.Visiting = @Visiting)", conn))
    {
    cmdGetPhone.Parameters.Add("@SpkrName", OleDbType.VarWChar).Value = speaker;
    cmdGetPhone.Parameters.Add("@GroupIN", OleDbType.VarWChar).Value = group;
    cmdGetPhone.Parameters.Add("@Local", OleDbType.VarWChar).Value = local;
    cmdGetPhone.Parameters.Add("@Visiting", OleDbType.VarWChar).Value = visiting;
    using (OleDbDataReader reader = cmdGetPhone.ExecuteReader())
    {
        while (reader.Read())

Solution

  • Your first syntax looks fine. You have:

    SELECT s.GroupName, s.SpkrName, s.SpkrHomePhone, s.SpkrCellPhone, aIN.SpkrIN,
    aIN.GroupIN FROM Speakers AS s 
    INNER JOIN AssignmentsIN AS aIN ON 
    aIN.SpkrIN = s.SpkrName 
    AND aIN.GroupIN = s.GroupName 
    AND s.SpkrName = @SpkrName 
    AND s.Local = @Local 
    AND s.Visiting = @Visiting
    

    However, your params are

    @SpkrName   ok
    @GroupIN    hum, I don't see that in above
    @Local      ok
    @Visiting   ok
    

    So, your first SQL syntax looks fine (stick with it - it just fine). But it does seem a extra parameter is being added. Not sure, maybe sql server don't care with sql provider,

    With oleDB provider? Well, it might be different. And also as a FYI? Order MATTERS when using JET/ACE, so do put them in the sql, and add params as in the SAME order. Your order is ok (sans the missing one).

    So, a parms looks to be missing. We shall assume comments from spectators and the peanut gallery about a horrid and miss-leading error message to not speak out here!