0

After I get DataTable from sqlAdapter I'm closing connection. If I preform SELECT from this DataTable, does the select use the index I created in the DB?

what code do I need to write for using the index?

Mark T
  • 773
  • 4
  • 10
  • 22
  • Why do u need index. You can select using primary key. Set primary key for that table and access it. `DataTable.PrimaryKey =DataTable.Columns["ColumnName"];` – Marshal Apr 12 '11 at 14:16
  • @Niraj Doshi, i think he is meaning primary index or another from DB – Achilleterzo Apr 12 '11 at 14:17
  • You may wish to clarify your question by adding some more detail or example. – Marshal Apr 12 '11 at 14:19
  • FYI, [DataView class](https://learn.microsoft.com/en-us/dotnet/api/system.data.dataview) is in a sense a secondary index on a DataTable. (I write this comment here because this stackoverflow Q&A was high in google when I searched for how to do such a secondary index.) – ToolmakerSteve Nov 23 '18 at 11:27

6 Answers6

2

If I understand your question, you want to know if the index that's defined in the DB will be present in the DataTable? Unfortunately the answer is no, the DataTable is separate from the database.

DavidGouge
  • 4,583
  • 6
  • 34
  • 46
1

A DataTable is an in-memory dataset. If you (sub)select after it has been fetched from the DB you're query your data in memory and you're not using an index.

LeftyX
  • 35,328
  • 21
  • 132
  • 193
1

See this: Do ADO.Net DataTables have indexes?

Community
  • 1
  • 1
mservidio
  • 12,817
  • 9
  • 58
  • 84
1

If you've already selected your data from the database and placed it into an in-memory table, you won't have access to the db's indicies. However, because the table is now in-memory, you won't need those indicies, as the lookup performance will be improved due to the fact that it is an in-memory search.

cwharris
  • 17,835
  • 4
  • 44
  • 64
1

All data in the DataTable is respecting database structure/data, you have exactly what you have in the database.

        DataRow[] dra = datatable.Select("uid > 2");
        foreach (DataRow dr in dra)
        {
            Console.WriteLine(dr["uid"]);
        }
Achilleterzo
  • 742
  • 6
  • 16
0

Select all of the rows in the DataTable

int rowNumber DataRow[] foundrow; String showString;

foundrow = yourDataSet.Tables["yourTableName"].Select();

Select the specific row and column(s)

showString = foundrow[rowNumber]["yourColumnName"].ToString();