no primary key on union query

  • Thread starter Thread starter mharness
  • Start date Start date
M

mharness

Hello,

I need to find a string value in a dataset table which is the result of a
union query that does not have any primary keys.

Is there another method or approach other than find and contains that I
could use that does not require primary keys or is it possible to "supply" a
primary key on the query result?

Thanks,

Mike
 
You can add a primary key to the table. All you do is create an array of
datacolumns and set the PrimaryKey property of the DataTable to the array
( one point of confusion for some people is that you need to use an Array of
datacolumns for the PK, even if you have only one column).

So you'd do something like:

DataColumn KeyColumn = new DataColumn("MyKeyColumn", typeof(System.Int32));
MyDataSet.Tables[WhateverIndex].Columns.Add(KeyColumn);
KeyColumn.AutoIncrement = true; //I only do this so that we have values in
the column, you can use any other mechanism you find convenient to give the
key field values.//This will also give you the benefit of having guaranteed
unique values.

DataColumn[] Keys = new DataColum[]{KeyColumn};
MyDataSet.Tables[WhateverIndex].PrimaryKey = Keys;

Now you can use Find, Select etc. BTW, if you use the Select method, if
will use the PrimaryKey but if you don't have one, it will use the order of
addition so Select may be of benefit wihtout having to add a key value).

Another way to get there is to use a dataView and set its RowFilter
property. This link has a bunch of examples of using the RowFilter
http://www.google.com/search?q=RowFilter&domains=KnowDotNet.com&sitesearch=KnowDotNet.com

HTH,

Bill
 
Back
Top