0

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

hawbsl
  • 15,313
  • 25
  • 73
  • 114
Harish
  • 169
  • 3
  • 5
  • 11

3 Answers3

2

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
  • 15,313
  • 25
  • 73
  • 114
2

Or

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

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

Buck Hicks
  • 1,534
  • 16
  • 27
1

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...

Adriaan Stander
  • 162,879
  • 31
  • 289
  • 284