DataAdapter

  • Thread starter Thread starter Ranier Dunno
  • Start date Start date
R

Ranier Dunno

Hi,

Is there any way of telling a DataAdapter to retrieve
only the first column from a table, if the name of the
column is not known? I'd like to be able to write
something like:

string cmdTxt = string.Format("SELECT <Column_1> FROM
[MyTable]");
OleDbDataAdapter adapter = new OleDbDataAdapter(cmdTxt,
conn);
DataTable table = new DataTable();
adapter.Fill(table);

Any help would be greatly appreciated!

Regards,
Ranier.
 
You can get the name of the column using

COL_NAME ( table_id , column_id )

as in

USE Northwind
SET NOCOUNT OFF
SELECT COL_NAME(OBJECT_ID('Employees'), 1)

returns

EmployeeID

which you in turn can use in your other select string
 
-----Original Message-----
You can get the name of the column using

COL_NAME ( table_id , column_id )

as in

USE Northwind
SET NOCOUNT OFF
SELECT COL_NAME(OBJECT_ID('Employees'), 1)

returns

EmployeeID

which you in turn can use in your other select string

Thanks for your reply!

Sadly I'm not working against an SQL server database; I'm
connecting to an Excel-file, which has a single column of
real data, but lots of "empty" columns, and no data in
the first row (where a column name would be in my book).

If I try a "SELECT *" I get an OleDbException saying "too
many fields defined" because of the "empty" columns. And
I don't have any column name to specify that only the
first column should be read... If I manually enter a
column name in the first data row, it works like a charm.

Regards,
Ranier.
 
Hmmm... actually the empty columns seem to have nothing
to do with it, the problem is that empty first row... I
suspect it might be hard to work around that one (?)
 
have you tried to use a datareader instead of a data adapter? with the
lower overhead, you may not get the errors you are getting.

just a hunch...
 
Back
Top