Results 1 to 6 of 6

Thread: Need help with from clause

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2013
    Posts
    200

    Need help with from clause

    Hello, I have an mdb database and I'm trying to check if some records exist, using a function. The code below is giving me syntax error in FROM clause.

    Code:
    Dim dbStr As String = "SELECT * FROM ? WHERE ? = ?"
    Command = New OleDbCommand(dbStr, Connection)
    
    Command.Parameters.AddWithValue("@table", t)
    Command.Parameters.AddWithValue("@column", c)
    Command.Parameters.AddWithValue("@value", v)
    I want to learn if something is wrong with this code.

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: Need help with from clause

    It doesn't appear that you can use a table name as a parameter like any other. Here's a lengthy discussion that you might find useful:

    http://www.sommarskog.se/dynamic_sql.html
    My usual boring signature: Nothing

  3. #3
    Fanatic Member Toph's Avatar
    Join Date
    Oct 2014
    Posts
    655

    Re: Need help with from clause

    You might want to learn SQL and ADO.NET before trying to use it

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Oct 2013
    Posts
    200

    Re: Need help with from clause

    Ok, I see. Thanks a lot.

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Need help with from clause

    this would work though:

    Code:
    Dim dbStr As String = string.format("SELECT * FROM {0} WHERE {1} = ?", t, c)
    Command = New OleDbCommand(dbStr, Connection)
    
    Command.Parameters.AddWithValue("@value", v)

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: Need help with from clause

    Not only would that work, it is probably the better answer.
    My usual boring signature: Nothing

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width