Getting a List of Field Names
Getting a List of Field Names
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?
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?
-
- Devart Team
- Posts: 6
- Joined: Mon 18 Aug 2008 07:21
Re: Getting a List of Field Names
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, (that's what I usually do ).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?
Of Course, u will get answer here too (I hope) but not that fast, u know... ^_^
-
- Devart Team
- Posts: 6
- Joined: Mon 18 Aug 2008 07:21
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
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
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.
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.
-
- Devart Team
- Posts: 6
- Joined: Mon 18 Aug 2008 07:21
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).
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).
-
- Devart Team
- Posts: 6
- Joined: Mon 18 Aug 2008 07:21
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"]);
}
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"]);
}