Click here to Skip to main content
15,888,321 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi friends

I want to use select statement like this sql = "select * from ['" + table_name + "']";

I am using access 2003 mdb database and provider "Microsoft.Jet.OLEDB.4.0" it is not execute
Dynamic Sql

Suggest Me what should I Do.

Here is my code

C#
string table_name = "", sql="";
            table_name = comboBox1.Text;
            sql = "select * from  ['" + table_name + "']";
            //sql = "Select * from shriya.sysobjects where name=['" + table_name + "']";
            con2.Open();
            Daaccount = new OleDbDataAdapter(sql, con2);
            Daaccount.Fill(Dsaccount);
            dataGridView1.DataSource = Dsaccount.Tables[0];
            con2.Close();
Posted

1 solution

You can do it, but you really, really shouldn't: it is extremely dangerous. Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead - except you can't for a table name, because SQL won't let it be a variable.

I wouldn't do it myself (I value my data, and don't want my users destroying it) but if you absolutely must, all you have to do is remove the single quotes:

C#
sql = "select * from  [" + table_name + "]";
 
Share this answer
 
Comments
Asif 7969814 28-Feb-15 10:07am    
Thanks i will use Parametrized queries in my Real code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900