| | |
Help Needed to build a dynamic SQL query string in VB.NET
Please support our VB.NET advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Sep 2008
Posts: 22
Reputation:
Solved Threads: 0
Im currently working on a project to build an application for Windows Mobile 6. Im just stuck with this stupid issue:
I have a SQL query string:
Dim connectionString2 As String = "Data source = " + path + "\HC.sdf"
Dim cmdText2 = "SELECT * FROM BigC_Rangsit_0_"
I want to substitute the table name ie. BigC_Rangsit_0_ with a variable which contains the table name.
say, during runtime the user selects a table from the dropdown list , that variable should be substituted with the selected table name dynamically in the SELECT statement.
Dim cmdText2 = "SELECT * FROM "selected table name VARiable""
well, I need the right syntax for this.
I have a SQL query string:
Dim connectionString2 As String = "Data source = " + path + "\HC.sdf"
Dim cmdText2 = "SELECT * FROM BigC_Rangsit_0_"
I want to substitute the table name ie. BigC_Rangsit_0_ with a variable which contains the table name.
say, during runtime the user selects a table from the dropdown list , that variable should be substituted with the selected table name dynamically in the SELECT statement.
Dim cmdText2 = "SELECT * FROM "selected table name VARiable""
well, I need the right syntax for this.
Use Replace and placeholder for table name:
and later replace
where strTableName is obtained from the user (or dropdown box)
VB.NET Syntax (Toggle Plain Text)
Dim cmdText2 As String cmdText2 = "SELECT * FROM XTABLEX "
and later replace
VB.NET Syntax (Toggle Plain Text)
cmdText2 = cmdText2.Replace("XTABLEX", strTableName)
Teme64 @ Windows Developer Blog
•
•
Join Date: Sep 2008
Posts: 22
Reputation:
Solved Threads: 0
The problem is that im declaring the variable cmdText2 in a calss declaration and not in a function. So when i try to use it ie.
Dim cmdText2 as String
cmdText2 = "SELECT * FROM XTABLEX"
in the second line it says "Declaration Expected"
DO you have any other solution. Your help will be greatly appreciated.
Dim cmdText2 as String
cmdText2 = "SELECT * FROM XTABLEX"
in the second line it says "Declaration Expected"
DO you have any other solution. Your help will be greatly appreciated.
Ok. You had
To get it to work in application you have to have some function/sub where to do replacing. Is this possible? You mentioned dropdown box:
Can you use it in this way?
cmdText2 = "SELECT * FROM XTABLEX" in class declaration and it gave you that error. Dim cmdText2 as String = "SELECT * FROM XTABLEX" works in class declaration?To get it to work in application you have to have some function/sub where to do replacing. Is this possible? You mentioned dropdown box:
VB.NET Syntax (Toggle Plain Text)
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged ' ' Get table name from dropdown box, build SQL and... Dim strSQL As String strSQL = cmdText2.Replace("XTABLEX", strTableName) ' Use strSQL End Sub
Teme64 @ Windows Developer Blog
•
•
Join Date: Sep 2008
Posts: 22
Reputation:
Solved Threads: 0
Yeah Yeah... Thanks a lot . I did exactly that before you posted your reply. Thanks anyways , your first post gave me a head start. Thanx a lot . appreciate it.
Now i have a different problem regarding SQL scripts. may be you could help me out with that one. I have posted the question on Wed Development -> Databases -> MS SQL. Would be of great help if you could help me out.
Thanx again for every thing. Appreciate it.
Now i have a different problem regarding SQL scripts. may be you could help me out with that one. I have posted the question on Wed Development -> Databases -> MS SQL. Would be of great help if you could help me out.
Thanx again for every thing. Appreciate it.
•
•
•
•
Yeah Yeah... Thanks a lot . I did exactly that before you posted your reply. Thanks anyways , your first post gave me a head start. Thanx a lot . appreciate it.
Now i have a different problem regarding SQL scripts. may be you could help me out with that one. I have posted the question on Wed Development -> Databases -> MS SQL. Would be of great help if you could help me out.
Thanx again for every thing. Appreciate it.
Teme64 @ Windows Developer Blog
![]() |
Other Threads in the VB.NET Forum
- Previous Thread: Syntax error in UPDATE statement.
- Next Thread: help with code.
| Thread Tools | Search this Thread |
.net .net2008 2008 access advanced application array basic beginner browser button buttons center click client code combo convert cuesent data database datagrid datagridview date datetimepicker designer dissertation dissertations dissertationtopic eclipse excel exists fade filter forms function generatetags html images input intel internet listview map mobile module monitor msaccess net number objects open panel pdf picturebox picturebox2 port position print printing problem read regex remove right-to-left save search searchvb.net select serial settings shutdown socket sqldatbase sqlserver survey temperature textbox timer timespan transparency txttoxmlconverter user usercontol vb vb.net vba vbnet visual visualbasic visualbasic.net visualstudio.net visualstudio2008 web winforms winsock wpf wrapingcode xml year





