-1

How would I go about fetching the row number (or index) of a particular row that matches a condition?

Example:

If I wanted to select:

SELECT * FROM tblAccount WHERE [Account ID] = 2343 LIMIT 0, 1

How would I get the row number of that selected row?

Thanks.

Nick
  • 1,229
  • 3
  • 30
  • 45

2 Answers2

0

Row number isn't a property of a SQL-query result, other than the key - but I guess that's not what you are after. If you need it in the access-table, then you have to create and maintain it as a column in the table.

After you fetch the result into a DataTable, then you can find the DataTable-specific row number using the Select and IndexOf methods.

Can you provide more information about what you want to use it for?

JonC
  • 927
  • 2
  • 7
  • 26
0

Look at the code below

OleDbConnection cn = new OleDbConnection();
OleDbCommand cmd = new OleDbCommand();
DataTable schemaTable; 
OleDbDataReader myReader; 

//Open a connection to the SQL Server Northwind database.
cn.ConnectionString = "Provider=SQLOLEDB;Data Source=server;User ID=login;
                       Password=password;Initial Catalog=Northwind";
cn.Open();

//Retrieve records from the Employees table into a DataReader.
cmd.Connection = cn;
cmd.CommandText = "SELECT * FROM tblAccount WHERE [Account ID] = 2343 LIMIT 0, 1";
myReader = cmd.ExecuteReader(CommandBehavior.KeyInfo); 

//Retrieve column schema into a DataTable.
schemaTable = myReader.GetSchemaTable();
...

schemaTable will tell you everything like schemaTable.Column.Count to tell column number

bitoshi.n
  • 2,230
  • 1
  • 14
  • 16