Getting a List of Field Names

Getting a List of Field Names

Postby ashlar64 » Fri 15 Aug 2008 18:51

Hello,

I am wondering if anyone knows how to do this...
I have several MySQL statements and I need to know what field names these statements will be returning. Is there a way to find this out without pulling down all the rows that these statements would pull down? (Perhaps I could just pull down 1 row?)

How would you do this?

:?:
ashlar64
 
Posts: 75
Joined: Thu 04 May 2006 18:56

Re: Getting a List of Field Names

Postby Daniel Dean » Mon 18 Aug 2008 09:31

ashlar64 wrote:Hello,

I am wondering if anyone knows how to do this...
I have several MySQL statements and I need to know what field names these statements will be returning. Is there a way to find this out without pulling down all the rows that these statements would pull down? (Perhaps I could just pull down 1 row?)

How would you do this?

:?:


as far as I understand, for question like these there are very good forums dedicated to Oracle, MySql, C++, Java or anything u can possible imagine! :) Imho Browsing the net and looking for the answer on such forums might provide u with the best result u could ever expected, :D (that's what I usually do ). :P
Of Course, u will get answer here too (I hope) but not that fast, u know... ^_^
Daniel Dean
Devart Team
 
Posts: 6
Joined: Mon 18 Aug 2008 07:21

Postby ashlar64 » Mon 18 Aug 2008 12:28

I am using the MyDirect.Net library for my database...

As for looking I did look quite extensively and I wasn't able to find what I needed.
ashlar64
 
Posts: 75
Joined: Thu 04 May 2006 18:56

Postby Daniel Dean » Mon 18 Aug 2008 13:52

I c. Maybe I can help.... Can u describe your question more specifically including examples (some SQL statements u r trying to execute maybe)?

Cause, tbh, I can't understand the problem. I mean, if I execute: "SELECT emp, empno, empstatus, FROM employees" I know that I will get field names: "emp", "empno", "empstatus". I specify the names in a SELECT statement.
So, I can't clearly understand your Question. Describe plzzz :)
Ty :)
Daniel Dean
Devart Team
 
Posts: 6
Joined: Mon 18 Aug 2008 07:21

Postby ashlar64 » Mon 18 Aug 2008 14:32

Well in my program I will be given quite a few SQL statements. Some will be very simple and some very complex. I need to make a string list of the Fieldnames for each statement.

Some of these statements will be using JOINS and will be using more than 1 table and a SELECT * statement. In any case its going to be a very wide range of SQL statements.

Also some of these statements could pull in hundrds of thousands of rows too.


So basically I need to find out what Fieldnames these statements will be using without pulling down hundreds of thousands of rows.

If I could alter this statements so that I brought down only 1 row of data I could use that and easily find the Fieldnames. But I am unsure as how to edit the SQL statements to do that.
ashlar64
 
Posts: 75
Joined: Thu 04 May 2006 18:56

Postby Daniel Dean » Mon 18 Aug 2008 14:53

Do u want it 2 blike this:

execute "SELECT statement" ;

Result: FieldName1
FieldName2
FieldName3.... n

Am I correct?
Daniel Dean
Devart Team
 
Posts: 6
Joined: Mon 18 Aug 2008 07:21

Postby ashlar64 » Mon 18 Aug 2008 15:14

Well anyway that I can get a string list of the Fieldnames from the query would be great.

And yes that is what I am looking for.


But remember I don't want to execute the whole query....because some of the queries would bring back hundreds of thousands of rows....
ashlar64
 
Posts: 75
Joined: Thu 04 May 2006 18:56

Postby ashlar64 » Mon 18 Aug 2008 20:44

Well the way I am doing it....
I take the SQL string and if there is a WHERE clause on it I remove it and everything after it. I then add to the new string at the end " LIMIT 1" which makes the statement only take the first row. I then can easily extract the fieldnames that way. If there is a better way I would love to know. (I am sure there is...my way feels very clunky).
ashlar64
 
Posts: 75
Joined: Thu 04 May 2006 18:56


Postby Shalex » Tue 19 Aug 2008 06:15

One of the possible solutions to get columns list:

mysqlCommand.CommandText = "select .....";
List columns = new List();
using(MySqlDataReader reader = mysqlCommand.ExecuteReader(CommandBehavior.SchemaOnly)) {
DataTable table = reader.GetSchemaTable();
foreach(DataRow row in table.Rows)
columns.Add((string)row["ColumnName"]);
}
Shalex
Devart Team
 
Posts: 7391
Joined: Thu 14 Aug 2008 12:44


Return to dotConnect for MySQL