Re: Remove DataTable Entries by Derek
Derek
Wed Jul 30 13:51:39 CDT 2003
This is a multi-part message in MIME format.
------=_NextPart_000_0096_01C356A1.B33DA1B0
Content-Type: text/plain;
charset="Windows-1252"
Content-Transfer-Encoding: quoted-printable
Hi Michael. Also, DataTable.Select( string) should work...
[But I'm not sure if it works on just the primary key field or all =
columns in the data row because it returns an array of datarows...]
Derek LaZard
"Michael Lang" <m@l.com> wrote in message =
news:eS3USWfVDHA.2368@TK2MSFTNGP09.phx.gbl...
> To delete a row from a DataTable, just call the "Delete" method on the
> DataRow you want to delete. Do NOT call =
"DataTable.Rows.Remove(datarow
> dr)". The Remove method of the DataRowCollection only removes it from =
the
> collection, which will not be queued for deletion from the database.
>=20
> As for how you determine what to delete? As far as I know, there is =
no
> automated way to determine which records have the same field values =
(PK or
> otherwise) in two tables?
>=20
> My intial idea would be to loop through one table, and for each item =
check
> for an item of the same PK value in the second table. If one exists, =
call
> the Delete method on it as I noted above.
>=20
> DataTable dtA =3D ...;
> DataTable dtB =3D ...;
> for (int i=3D0; i < dtA.Count; i++)
> {
> DataRow drA =3D dtA.Rows[i];
> object pkA =3D ... primary key value in drA ...
> DataRow drFound =3D dtB.Rows.Find(pkA);
> if (drFound !=3D null)
> {
> drFound.Delete();
> }
> }
>=20
> I haven't run this code, I just made it up from looking at the help. =
In
> theory, it should work. For details look at the help on DataTable,
> DataRowCollection, and DataRow.
>=20
> Michael Lang
>=20
> "frank" <frank@frank.com> wrote in message
> news:ey$jvPdVDHA.1692@TK2MSFTNGP11.phx.gbl...
> > I have two DataTables, A and B. Both have the primary key "CID". How =
do I
> > remove all entries in Table A from Table B (with the same PK)?
> >
> >
>=20
>
------=_NextPart_000_0096_01C356A1.B33DA1B0
Content-Type: text/html;
charset="Windows-1252"
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Dwindows-1252">
<META content=3D"MSHTML 6.00.2800.1106" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY>
<DIV><FONT size=3D2>Hi Michael. Also, =
DataTable.<STRONG>Select</STRONG>(=20
string) should work...</FONT></DIV>
<DIV><FONT size=3D2>[But I'm not sure if it works on just the primary =
key field or=20
all columns in the data row because it returns an array of=20
datarows...]</FONT></DIV>
<DIV><FONT size=3D2></FONT> </DIV>
<DIV><FONT size=3D2>Derek LaZard</FONT></DIV>
<DIV><FONT size=3D2></FONT> </DIV>
<DIV><FONT size=3D2>"Michael Lang" <</FONT><A =
href=3D"mailto:m@l.com"><FONT=20
size=3D2>m@l.com</FONT></A><FONT size=3D2>> wrote in message =
</FONT><A=20
href=3D"news:eS3USWfVDHA.2368@TK2MSFTNGP09.phx.gbl"><FONT=20
size=3D2>news:eS3USWfVDHA.2368@TK2MSFTNGP09.phx.gbl</FONT></A><FONT=20
size=3D2>...</FONT></DIV><FONT size=3D2>> To delete a row from a =
DataTable, just=20
call the "Delete" method on the<BR>> DataRow you want to =
delete. Do NOT=20
call "DataTable.Rows.Remove(datarow<BR>> dr)". The Remove =
method of the=20
DataRowCollection only removes it from the<BR>> collection, which =
will not be=20
queued for deletion from the database.<BR>> <BR>> As for how you =
determine=20
what to delete? As far as I know, there is no<BR>> automated =
way to=20
determine which records have the same field values (PK or<BR>> =
otherwise) in=20
two tables?<BR>> <BR>> My intial idea would be to loop through one =
table,=20
and for each item check<BR>> for an item of the same PK value in the =
second=20
table. If one exists, call<BR>> the Delete method on it as I =
noted=20
above.<BR>> <BR>> DataTable dtA =3D ...;<BR>> DataTable dtB =3D =
...;<BR>> for (int i=3D0; i < dtA.Count; i++)<BR>> {<BR>>=20
DataRow drA =3D dtA.Rows[i];<BR>> =
object=20
pkA =3D ... primary key value in drA ...<BR>> =
DataRow=20
drFound =3D dtB.Rows.Find(pkA);<BR>> if (drFound =
!=3D=20
null)<BR>> {<BR>>=20
drFound.Delete();<BR>>=20
}<BR>> }<BR>> <BR>> I haven't run this code, =
I just=20
made it up from looking at the help. In<BR>> theory, it should=20
work. For details look at the help on DataTable,<BR>>=20
DataRowCollection, and DataRow.<BR>> <BR>> Michael Lang<BR>> =
<BR>>=20
"frank" <</FONT><A href=3D"mailto:frank@frank.com"><FONT=20
size=3D2>frank@frank.com</FONT></A><FONT size=3D2>> wrote in =
message<BR>>=20
</FONT><A href=3D"news:ey$jvPdVDHA.1692@TK2MSFTNGP11.phx.gbl"><FONT=20
size=3D2>news:ey$jvPdVDHA.1692@TK2MSFTNGP11.phx.gbl</FONT></A><FONT=20
size=3D2>...<BR>> > I have two DataTables, A and B. Both have the =
primary=20
key "CID". How do I<BR>> > remove all entries in Table A from =
Table B=20
(with the same PK)?<BR>> ><BR>> ><BR>> <BR>>=20
</FONT></BODY></HTML>
------=_NextPart_000_0096_01C356A1.B33DA1B0--