Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Rob Lang http://www.maison-de-stuff.net/rob/

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

Using Rows.Find in ADO.NET

When using a DataTable in ADO.NET, you may want to search your table given a key. The Find method requires the index of the primary key to find the row that you're looking for. However, before you do this you must set the primary key of the Datatable. Setting the primary key in your database is not enough. To set the primary key, create a new column and then set the primary key field on the Datatable like so:

   1  
   2  
   3  // Create the DataColumn list for holding the columns of the primary keys. In this case, we are using only one primary key
   4  DataColumn[] primaryKeyColumns = new DataColumn[1];
   5  
   6  // Set the first primary key to the column 'id' in the datatable
   7  primaryKeyColumns[0] = myDataTable.Columns["id"];
   8  
   9  // Now apply the primary key to the datatable
  10  myDataTable.PrimaryKey = primaryKeyColumns;
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS