|
-
Nov 20th, 2006, 10:05 AM
#1
Thread Starter
Frenzied Member
How to get table name of DataTable?
I am using the code below to fill a DataGridView with data, which is working fine, except the line where I want to get the tablename. The .TableName function always returns nothing.
VB Code:
Dim myAdapter As Odbc.OdbcDataAdapter = New Odbc.OdbcDataAdapter(QueryString, MyConnection)
Dim myDataTable As DataTable = New DataTable
Dim count As Integer = myAdapter.Fill(myDataTable)
DataGridView1.DataSource = myDataTable
MessageBox.Show(myDataTable.TableName) '<----- always returns nothing
Can someone kindly point out what I am doing wrong? If this wont work, then how else can I get the table name? Thanks...
-
Nov 20th, 2006, 10:07 AM
#2
Fanatic Member
Re: How to get table name of DataTable?
This is why:
you should do this:
VB Code:
myDataTable = New DataTable("tablename")
That will return the name of the table. Otherwise it doesn't exist, even if data was returned
Warren Ayen
Senior C# Developer
DLS Software Studios ( http://www.dlssoftwarestudios.com/)
I use Microsoft Visual Studio 2005, 2008, working with Visual Basic and Visual C#
Hey! If you like my post, or I solve your issue, please Rate Me!
-
Nov 20th, 2006, 10:14 AM
#3
Thread Starter
Frenzied Member
Re: How to get table name of DataTable?
But what if I don't know the name of the table? The only place it appears is in QueryString, ie. QueryString = "Select * from MyTable". Are you saying that I will have to parse the string to get the table name?
-
Nov 20th, 2006, 10:20 AM
#4
Fanatic Member
Re: How to get table name of DataTable?
Hmmm I've always known my tables before I open them. Otherwise how do I know what to open? I'm not sure what kind of circumstance you're trying for....how would you not know what table you're opening?
I just tried it all myself again. Unless you create a DataTable with an actual schema (with a tablename), there isn't one. Instead, your datatable is simply a bunch of named rows in a blank table. You have to tell the datatable what it's going to be. So yes, you will need to parse the querystring.
Warren Ayen
Senior C# Developer
DLS Software Studios ( http://www.dlssoftwarestudios.com/)
I use Microsoft Visual Studio 2005, 2008, working with Visual Basic and Visual C#
Hey! If you like my post, or I solve your issue, please Rate Me!
-
Nov 20th, 2006, 10:27 AM
#5
Thread Starter
Frenzied Member
Re: How to get table name of DataTable?
My program is a general SQL data retrieval program. A user can enter in any valid SQL command & the program will execute it & return the data in a DataGrid. It does that part great, but now I would like to display the table name in a label. I'm not sure how to parse the string for the name, since it is not always a Select command, it could also be a Delete, Insert or Update command. Any ideas?
-
Nov 20th, 2006, 10:30 AM
#6
Fanatic Member
Re: How to get table name of DataTable?
Well use a select case statement. Grab the first 20 characters or so and test to see if it starts with:
VB Code:
If Left(UCase(string), 6) = "SELECT" Then
'do this
ElseIf Left(UCase(string), 11) = "INSERT INTO" Then
'do this
You can do that, too. Depending on what it starts with tells you where the table is. Get the next space, and then the space after the table name, grab the tablename with a Mid statement and you're good to go.
Warren Ayen
Senior C# Developer
DLS Software Studios ( http://www.dlssoftwarestudios.com/)
I use Microsoft Visual Studio 2005, 2008, working with Visual Basic and Visual C#
Hey! If you like my post, or I solve your issue, please Rate Me!
-
Nov 20th, 2006, 10:39 AM
#7
Thread Starter
Frenzied Member
Re: How to get table name of DataTable?
Ok, thanks for the help. I was just hoping for an easier method...
-
Nov 20th, 2006, 10:50 AM
#8
Re: How to get table name of DataTable?
If you write "Select * from myTable" then you've already known that the tablename is "myTable", otherwiase you wouldn't write it down like that.... If you creating a dynamic select statement, then it should look like this: "Select * from " & strTableName & """" where strTableName is a variable that store the table name... In this case, you can get the table name from the variable. If you allow the user to type in the entire select statement (it's a bad idea because not all users are familiar with sql syntax, and I strongly suggest against it) then you have to parse the string to get the table name as Warrenayen said.
-
Nov 20th, 2006, 11:00 AM
#9
Thread Starter
Frenzied Member
Re: How to get table name of DataTable?
 Originally Posted by stanav
If you allow the user to type in the entire select statement (it's a bad idea because not all users are familiar with sql syntax, and I strongly suggest against it) then you have to parse the string to get the table name as Warrenayen said.
This is exactly what I am doing. The user types in the full select statement (or delete, insert or update). This program is not for general use, but for myself & maybe several co-workers who do program development as well, so I'm not worried about anyone not knowing sql. So I looks like I will be parsing the string for the table name. Thanks for the input guys...
-
Nov 20th, 2006, 11:13 AM
#10
Thread Starter
Frenzied Member
Re: How to get table name of DataTable?
 Originally Posted by warrenayen
This is why:
you should do this:
VB Code:
myDataTable = New DataTable("tablename")
That will return the name of the table. Otherwise it doesn't exist, even if data was returned
So if you have to specify a tablename when you declare your DataTable, then what is the point of having a .TableName method? It seems silly to have a way to get something that you already know...
-
Nov 20th, 2006, 12:56 PM
#11
Re: How to get table name of DataTable?
 Originally Posted by nbrege
So if you have to specify a tablename when you declare your DataTable, then what is the point of having a .TableName method? It seems silly to have a way to get something that you already know...
Supposed you have a dataset with with a bunch of tables in it...
1. It's very hard to keep track of which is which by using the table index, i.e. ds.Tables(0), ds.Tables(1)... So it's handy to name your datatable when you fill it for book keeping purpose.
2. Now that you want to add yet another table to your dataset only if it's not already in the dataset.... That's when you need the .TableName property to check for the existence of a specific table in the dataset.
There will be more situations that you'll need the tablename, but these 2 are the 1st to pops out of my head.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|