views:

48

answers:

3

Am storing a table name in a String

ugad = "INSERT INTO tb(Ugname,Ugdob,Uggender)"

this is the ordinary query which functions well.

But i need to store a Tablename in a string called "dept"

and this string will have diff table name at diff times. How should i run it, Wat query should I Give.

ugad = "INSERT INTO dept(Ugname,Ugdob,Uggender)" I know this query is not vaild. May i know the correct query

+2  A: 

Use:

ugad = "INSERT INTO " & dept & "(Ugname,Ugdob,Uggender)" 

N.B. There are arguably safer, better ways to compose SQL (if you are worried about malicious or accidental interference with your underlying data through SQL injection) than the above but hopefully that gets you started.

hawbsl
+1  A: 

If I understand you correctly you neet to try something like

ugad = "INSERT INTO " + dept + "(Ugname,Ugdob,Uggender)"

Have a llok at Operators in VB.NET

Just remember that string concatenation can be very slow once you start concatenating in loops, so always have in the back of your mind that the StringBuilder Class exists, and is a lot faster than normal concatenation...

astander
+2  A: 

Or

ugad = String.Format("INSERT INTO {0}(Ugname,Ugdob,Uggender)", dept)

Which I think is easier to read and easier to maintain.

BukHix