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?

4
  • 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, 2011 at 14:16
  • @Niraj Doshi, i think he is meaning primary index or another from DB Apr 12, 2011 at 14:17
  • You may wish to clarify your question by adding some more detail or example.
    – Marshal
    Apr 12, 2011 at 14:19
  • FYI, DataView class 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.) Nov 23, 2018 at 11:27

6 Answers 6

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.

1

See this: Do ADO.Net DataTables have indexes?

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.

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"]);
        }
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.

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();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.