| | |
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 2005 2008 access account arithmetic array arrays basic bing button buttons c# center check checkbox code convert crystalreport data database datagrid datagridview date dissertation dissertations dropdownlist excel fade file-dialog filter ftp generatetags google gridview hardcopy images inline input insert intel internet listview mobile monitor ms net networking objects output panel passingparameters picturebox picturebox1 port position print printing problem project read remove save searchbox searchvb.net select serial server shutdown soap sorting survey table tcp temperature text textbox timer timespan toolbox trim update user validation vb vb.net vb.netformclosing()eventpictureboxmessagebox vb2008 vbnet view visual visualbasic visualbasic.net visualstudio visualstudio2008 web winforms wpf year





