We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,489 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

using Table name as Variable string

hello :)

i'm developing an app in VS 2010 C#, linked with an MS Access Database.
i want to use a table name as variable. i have tried:

string tblname = students;
string query = "select * from  '" + tblname + "' where rollno=123";

but it's not working :(
kindly guide me.

thanks :)

2
Contributors
1
Reply
1 Day
Discussion Span
1 Year Ago
Last Updated
2
Views
Z33shan
Light Poster
26 posts since Sep 2011
Reputation Points: 11
Solved Threads: 1
Skill Endorsements: 0

Replace the single-quotes with square brackets like so:

string tblname = students;
string query = "select * from  [" + tblname + "] where rollno=123";

By putting single-quotes, you are basically telling your database engine to select everything from a string. Probably not what you want. MSAccess interprets anything inside square brackets as a database object (table, column, etc.). You could simply leave off the brackets altogether, but then if your variable tblname contains any spaces or special characters (which Access allows) then you'll get an error.

Hope this helps! Good luck!

BitBlt
Practically a Posting Shark
894 posts since Feb 2011
Reputation Points: 482
Solved Threads: 148
Skill Endorsements: 14

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.2855 seconds using 2.66MB