Help Needed to build a dynamic SQL query string in VB.NET

Please support our VB.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2008
Posts: 22
Reputation: isaackhazi is an unknown quantity at this point 
Solved Threads: 0
isaackhazi isaackhazi is offline Offline
Newbie Poster

Help Needed to build a dynamic SQL query string in VB.NET

 
0
  #1
Sep 24th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 710
Reputation: Teme64 will become famous soon enough Teme64 will become famous soon enough 
Solved Threads: 114
Teme64's Avatar
Teme64 Teme64 is offline Offline
Master Poster

Re: Help Needed to build a dynamic SQL query string in VB.NET

 
0
  #2
Sep 24th, 2008
Use Replace and placeholder for table name:

  1. Dim cmdText2 As String
  2. cmdText2 = "SELECT * FROM XTABLEX "

and later replace
  1. cmdText2 = cmdText2.Replace("XTABLEX", strTableName)
where strTableName is obtained from the user (or dropdown box)
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 22
Reputation: isaackhazi is an unknown quantity at this point 
Solved Threads: 0
isaackhazi isaackhazi is offline Offline
Newbie Poster

Re: Help Needed to build a dynamic SQL query string in VB.NET

 
0
  #3
Sep 24th, 2008
Thanx....Ill try that out and let you know..
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 22
Reputation: isaackhazi is an unknown quantity at this point 
Solved Threads: 0
isaackhazi isaackhazi is offline Offline
Newbie Poster

Re: Help Needed to build a dynamic SQL query string in VB.NET

 
0
  #4
Sep 25th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 710
Reputation: Teme64 will become famous soon enough Teme64 will become famous soon enough 
Solved Threads: 114
Teme64's Avatar
Teme64 Teme64 is offline Offline
Master Poster

Re: Help Needed to build a dynamic SQL query string in VB.NET

 
0
  #5
Sep 26th, 2008
Ok. You had 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:
  1. Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
  2. '
  3. ' Get table name from dropdown box, build SQL and...
  4. Dim strSQL As String
  5. strSQL = cmdText2.Replace("XTABLEX", strTableName)
  6. ' Use strSQL
  7.  
  8. End Sub
Can you use it in this way?
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 22
Reputation: isaackhazi is an unknown quantity at this point 
Solved Threads: 0
isaackhazi isaackhazi is offline Offline
Newbie Poster

Re: Help Needed to build a dynamic SQL query string in VB.NET

 
0
  #6
Sep 26th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 27
Reputation: Sheryl99 is an unknown quantity at this point 
Solved Threads: 0
Sheryl99 Sheryl99 is offline Offline
Light Poster

Re: Help Needed to build a dynamic SQL query string in VB.NET

 
0
  #7
Sep 28th, 2008
Not sure if this is what you need, but maybe it will help.

Dim strTable as String = "MyTable"
Dim strSQL as String = "SELECT * FROM " & strTable & ";"

result:

SELECT * FROM MyTable;
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 710
Reputation: Teme64 will become famous soon enough Teme64 will become famous soon enough 
Solved Threads: 114
Teme64's Avatar
Teme64 Teme64 is offline Offline
Master Poster

Re: Help Needed to build a dynamic SQL query string in VB.NET

 
0
  #8
Sep 28th, 2008
Originally Posted by isaackhazi View Post
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.
Ok, see http://www.daniweb.com/forums/post70...tml#post700670
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the VB.NET Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC