473,408 Members | 2,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Question on DataTable join

Hi, I have two datatables and I want to efficiently query the
intersection of the two using the studentID. In my example code below,
I want to print out Mary and Jane and their exam grades and I don't
want to use loop because the real datatables are much larger. Can this
be done using one of the inbuilt methods in the class library and if
so how.
Thank you in advance
Peter

DataTable examgrade;
DataTable students;

private void addExamgrade(int studentID, string grade)
{
DataRow dr=examgrade.NewRow();
dr["STUDENTID"]=studentID;
dr["GRADE"]=grade;
}

private void addstudent(int studentID, string studentname,int
yearenrolled)
{
DataRow dr=students.NewRow();
dr["STUDENTID"]=studentID;
dr["STUDENTNAME"]=studentname;
dr["YEARENROLLED"]=yearenrolled;
}

private void button1_Click(object sender, System.EventArgs e)
{
examgrade=new DataTable();
examgrade.Columns.Add("STUDENTID",typeof(int));
examgrade.Columns.Add("GRADE",typeof(string));
addExamgrade(101,"A");
addExamgrade(102,"B-");
addExamgrade(500,"C");
addExamgrade(150,"FAIL");

students=new DataTable();
students.Columns.Add("STUDENTID",typeof(int));
students.Columns.Add("STUDENTNAME",typeof(string)) ;
students.Columns.Add("YEARENROLLED",typeof(int));
addstudent(400,"Fred",1982);
addstudent(300,"John",1975);
addstudent(100,"Mark",2003);
addstudent(500,"Mary",1960);
addstudent(150,"Jane",2002);

//hardcoded answer
Console.WriteLine("The students and their grades are");
Console.WriteLine("Mary C");
Console.WriteLine("Jane FAIL");
}
Nov 16 '05 #1
3 9242
hi
you can use the Find and Select method of the databale object.
these are two built in function of the datatable object . the select
method is rather simple .it takes as input a string "SQL query" and return
an array of dataraws
DataRow[] result = tableName.Select("name = 'whatever' ");
the find method returns only one datarow , this is because it search with
the primary key of the table . therefore, to use this method you need to
define a primary key to the table you are searching into . to define a
primary key set the PrimaryKey property of the datatable to an ARRAY of
datacoumns
mytable.PrimaryKey = new DataColumn[]{mytable.Columns["index or
name of the primary key column"].
then you can use find to get the row that has this specific value " the one
you are looking for " on its primary key feild
DataRow result = mytable.Find("some value for the primary key");

hope that was clear , if not you can read more about these two methods on
the msdn online or you can post your inquries .
Mohamed Mahfouz
MEA Developer Support Center
ITworx on behalf of Microsoft EMEA GTSC

Nov 16 '05 #2
Hi there - yes this is clear.

But my friends at school are talking about creating a "DataRelation"
and just doing one query. I don't understand how I can do ONE query on
a "DataSet" containing two tables.

Here is my modified code (another whole day writing this small code).
Thank you again
Peter

private void button1_Click(object sender, System.EventArgs e)
{
examgrade=new DataTable();
examgrade.Columns.Add("STUDENTID",typeof(int));
examgrade.Columns.Add("GRADE",typeof(string));
addExamgrade(101,"A");
addExamgrade(102,"B-");
addExamgrade(500,"C");
addExamgrade(150,"FAIL");

students=new DataTable();
students.Columns.Add("STUDENTID",typeof(int));
students.Columns.Add("STUDENTNAME",typeof(string)) ;
students.Columns.Add("YEARENROLLED",typeof(int));
addstudent(400,"Fred",1982);
addstudent(300,"John",1975);
addstudent(100,"Mark",2003);
addstudent(500,"Mary",1960);
addstudent(150,"Jane",2002);

DataSet ds=new DataSet();
ds.Tables.Add(examgrade);
ds.Tables.Add(students);

DataColumn dcParentStudentID=ds.Tables[0].Columns["STUDENTID"];
DataColumn dcParentGrade=ds.Tables[0].Columns["GRADE"];

DataColumn dcChildStudentID=ds.Tables[1].Columns["STUDENTID"];
DataColumn dcChildStudentName=ds.Tables[1].Columns["STUDENTNAME"];
DataColumn dcChildYearEnrolled=ds.Tables[1].Columns["YEARENROLLED"];

DataRelation dr=new DataRelation("join",new
DataColumn[]{dcParentStudentID},new
DataColumn[]{dcChildStudentID},false);
ds.Relations.Add(dr);

//Now what?
//how to I query the "ds" to get only
//Mary & Jane and C and FAIL in a textbox or dropdownlists
}
mo************@egdsc.microsoft.com (Mohamoss) wrote in message
news:<xH*************@cpmsftngxa10.phx.gbl>...
hi
you can use the Find and Select method of the databale object.
these are two built in function of the datatable object . the select
method is rather simple .it takes as input a string "SQL query" and return
an array of dataraws
DataRow[] result = tableName.Select("name = 'whatever' ");
the find method returns only one datarow , this is because it search with
the primary key of the table . therefore, to use this method you need to
define a primary key to the table you are searching into . to define a
primary key set the PrimaryKey property of the datatable to an ARRAY of
datacoumns
mytable.PrimaryKey = new DataColumn[]{mytable.Columns["index or
name of the primary key column"].
then you can use find to get the row that has this specific value " the one
you are looking for " on its primary key feild
DataRow result = mytable.Find("some value for the primary key");

hope that was clear , if not you can read more about these two methods on
the msdn online or you can post your inquries .
Mohamed Mahfouz
MEA Developer Support Center
ITworx on behalf of Microsoft EMEA GTSC

Nov 16 '05 #3
hi
using datarelation would be useful when you have a parent and child
realtion . like You can have a data relation between two tables that has a
primary key and foreign key relation. When you have that you can create a
date relation between the tables. And use the get child rows function
(offered when you use dataRelation object ) then you pass this function .
a value of the primary key of the first table . and it return all the rows
that has this value as a foreign key in the second table . So this can be
more compact in many cases ( if you have a situation like that ) .. hope
that helps
Mohamed Mahfouz
MEA Developer Support Center
ITworx on behalf of Microsoft EMEA GTSC

Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: gro.asumsa | last post by:
I have two DataTables in a DataSet. I would like to create a third DataTable in the same DataSet that is a Join of the two original DataTables. The two tables are extracted from two different...
2
by: Ev | last post by:
I have a database table in SQL Server that has a self join. In C# I have a DataTable with a self-join. I have defined a foreign key constraint on the DataTable for the self join. The...
7
by: Chris | last post by:
I'm using ASP.NET and SQL Server and this might be an obviuos question for most, but if I have a table that contains several fields that I need to relate to just one field in another table, how do...
1
by: cindy | last post by:
Get data into datatable, add to dataset dsSearch " Get data into datatable, add to dataset dsSearch Using In-Memory SQL Engine join the tables and select the filenames from the join, add to...
1
by: RJN | last post by:
Hi Need a help on data set. I have a .csv file as an input which I read using the oledb provider and upload into a dataset using a dataadapter. So the dataset has one datatable now. Using this...
2
by: Marcel Hug | last post by:
Hi NG! With a Inner-Join SQL I get my datas in a DataSet. In the table are the column Entry and Version. Like this: Entry Version 1 1 1 2 1 ...
9
by: SMALLp | last post by:
Hy! I have error something like this TypeError: unbound method insert() must be called with insertData instance as first argument (got str instance instead) CODE: File1.py...
1
by: szwejk | last post by:
Hi! How to get result od dataTable from Linq query? I have typied DataSet and I want to join couple of tables. And I have a problem with change this result to DataTable type. (I don't want to...
0
by: =?Utf-8?B?cm9uZSBtYXRpYXM=?= | last post by:
I have the same task to do but everytime I tried to parse my code I get a null value returned after executing "dtMaterials.WriteXml(swMaterials);". I am using the following code: Hope you can hep...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.