Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8996527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:45:59+00:00 2026-06-15T23:45:59+00:00

I have an ODBC connection to a database and I would like the user

  • 0

I have an ODBC connection to a database and I would like the user to be able to view data within any table. As this is an ASP.net application I cannot trust that the table name sent doesn’t also contain nasties. I have tried using a parameterised query but I always get an error saying that I “Must declare the table variable” – this appears to be an issue because it is the table name

 string sql = "SELECT TOP 10 * FROM ? ";
 OdbcCommand command = new OdbcCommand(sql, dbConnection);
 command.Parameters.Add(new OdbcParameter("@table", tableName));
 OdbcDataAdapter adapter = new OdbcDataAdapter();
 adapter.SelectCommand = command;
 adapter.Fill(tableData);

What is the best method to achieve this in a secure way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T23:46:00+00:00Added an answer on June 15, 2026 at 11:46 pm

    Use a stored procedure, it’s the safest way.

    Some hints:

    1. You probably may also use the System.Data.SqlClient namespace objects
    2. Enclose your connection, command and adapter objects initializations in using statements

    Here’s a simple example:

    string sqlStoredProcedure = "SelectFromTable";
    using (OdbcConnection dbConnection = new OdbcConnection(dbConnectionString))
    {
        dbConnection.Open();
        using (OdbcCommand command = new OdbcCommand(sqlStoredProcedure, dbConnection))
        {
            command.CommandType = System.Data.CommandType.StoredProcedure;
            command.Parameters.Add(new OdbcParameter("@table", tableName));
            using (OdbcDataAdapter adapter = new OdbcDataAdapter(command))
            {
                adapter.SelectCommand = command;
                adapter.Fill(tableData);
            }
        }
    }
    

    Another way to go would be to retrieve all table names and validate the tableName string variable as an entry in the list, maybe using:

    DataTable tables = dbConnection.GetSchema(OdbcMetaDataCollectionNames.Tables);
    

    Here’s a simple implementation based on your scenario:

    string sql = "SELECT TOP 10 * FROM {0}";
    using (OdbcConnection dbConnection = new OdbcConnection(dbConnectionString))
    {
        dbConnection.Open();
    
        DataTable tables = dbConnection.GetSchema(OdbcMetaDataCollectionNames.Tables);
        var matches = tables.Select(String.Format("TABLE_NAME = '{0}'", tableName));
    
        //check if table exists
        if (matches.Count() > 0)
        {
            using (OdbcCommand command = new OdbcCommand(String.Format(sql, tableName), dbConnection))
            {
                using (OdbcDataAdapter adapter = new OdbcDataAdapter(command))
                {
                    adapter.SelectCommand = command;
                    adapter.Fill(tableData);
                }
            }
        }
        else
        {
            //handle invalid value
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

On the class level, I have created reference: System::Data::Odbc::OdbcConnection Connection; in some method I
Is it possible to have an ODBC connection to a MS Access 2007 database
I have a method which is connecting to a database via Odbc. The stored
I have created an ODBC connection for oracle using Microsoft ODBC for Oracle driver
I have an Excel 2007 ODBC connection defined and I need to allow users
I have a pdo connection with ODBC (v2000.86.359.00) connecting to an SQL Server (v8.00.2039
I have a linked Firebird database on my SQL Server 2008 via ODBC. I
I have a database in a format which can be accessed via ODBC. I'm
My user have a password field such as 0!ZWQ2 saved in the database. I
I am creating a C++ application the uses a MySQL database. I would like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.