Click here to Skip to main content
15,894,825 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So I am wanting to pass a string value wit the name of the table in it. There are multiple tables, so each will be called at a specific time.

so instead of
profiledt.Rows


is there a way to do something like this ["profiledt"].rows

What I have tried:

I have googled, but could not find any answers that helped with this issue.
Posted
Updated 22-Dec-19 20:26pm
Comments
RickZeeland 23-Dec-19 1:27am    
Are you using SQL Server ?

Yes, you can!

VB
Dim ds As DataSet = New DataSet()
'add 5 datatables with example data
For i= 1 To 5
	Dim dt As DataTable = New DataTable(String.Format("Table{0}", i))
	dt.Columns.AddRange(New DataColumn()  _
		{ _
			New DataColumn("ID", Type.GetType("System.Int32")), _
			New DataColumn("Description", Type.GetType("System.String")) _
		})
	dt.Rows.Add(New Object(){1, "SomeData"})
	ds.Tables.Add(dt)
Next

'get datatable by its name:
Dim d = ds.Tables("Table1")
'd variable is ready to use!


For further details, please see:
DataSet Class (System.Data) | Microsoft Docs[^]
DataSets, DataTables, and DataViews | Microsoft Docs[^]
DataSet.Tables Property (System.Data) | Microsoft Docs[^]
 
Share this answer
 
Comments
Member 11856456 23-Dec-19 8:30am    
I changed you code up a little bit because I have multiple tables already built.

Dim d As New DataSet
Profileadaptor.Fill(profiledt)
profiledt.TableName = "Profile"
d.Tables.Add(profiledt)

works the way I need it too. thanyou Maciej Los for the recomendation of using datasets to incorporate using a table/tables by string name.
Maciej Los 23-Dec-19 9:00am    
You're very welcome.
Cheers!
Maciej
You could use a function that takes the table name as a parameter, something like this example: Visual Basic .NET Language - Simple Function to read from Database and return as DataTable | vb.net Tutorial[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900