-2

I am migrating code from VS2010 to VS2013 and my syntax in VS2010 shows compile errors in 13?!? Can someone assist me with why these two compile errors are being thrown?

Syntax error, ',' expected
{ expected

And this is the syntax that works w/o issue in 10.

    public static string DCAVACA(string badTable)
    {
        olecon = new OleDbConnection(OLECONN)
        {
            olecon.Open();
            deleteTable = new string[1] { "badTable" };
            for (int q = deleteTable.GetLowerBound(0); q <= deleteTable.GetUpperBound(0); q++)
            {
                System.Data.OleDb.OleDbCommand cmd = new OleDbCommand("DROP TABLE " + deleteTable[q], olecon);
                cmd.ExecuteNonQuery();
            }
        }
        return null;
    }
  • 1
    Your code is invalid syntax in both versions. You're missing semicolongs, and your braces don't make sense. – SLaks Apr 30 '15 at 19:14
  • @SLaks that is a direct copy/paste from my VS10 project that compiles and runs error free – MasterOfStupidQuestions Apr 30 '15 at 19:18
  • Then you're utilizing some bug in the C# compiler (which seems extremely unlikely). Every statement must end in a semicolon, including variable declarations. – SLaks Apr 30 '15 at 19:24
  • Why does the method even return anything at all? (It's always going to be `null` unless an Exception occurs.) Why have a (overly complicated) for loop over a `string[1]` (was `string[2]` initialized with 1 element before the edit)? What about SQL injection (yes, there can be "worse" things than dropping specified tables, ie. getting access to secret data)? – hangy Apr 30 '15 at 19:31

2 Answers2

1

I don't think that syntax should work in any version of Visual Studio tbh. FWIW, I think you want to use the using statement:

    public static string DCAVACA(string badTable)
    {
        using (var olecon = new OleDbConnection(OLECONN)) // only change required for *syntactic* reasons
        {
            olecon.Open();
            deleteTable = new string[1] { "badTable" };
            for (int q = deleteTable.GetLowerBound(0); q <= deleteTable.GetUpperBound(0); q++)
            {
                System.Data.OleDb.OleDbCommand cmd = new OleDbCommand("DROP TABLE " + deleteTable[q], olecon);
                cmd.ExecuteNonQuery();
            }
        }
        return null;
    }

There are some other issues with the code in general (badTable variable is not used at all, deleteTable is useless as a static string[2], naming, etc.), but that's out of scope.

hangy
  • 10,765
  • 6
  • 43
  • 63
1

You're missing a semicolon after olecon = new OleDbConnection(OLECONN).

Braces after a "new" are used to set attributes/properties of the newly-created object, not to run other arbitrary code. If this truly does run in VS2010, I have no idea why.

Eric Dand
  • 1,106
  • 13
  • 37