Select from two DataTables for each record

Hello,

I am using Windows Forms.

I have a DataTable with the column where I have ID-s of my products. I need to create another column, where are displayed names of the products, which means: I have to traverse all ID-s and for each ID search on another table, which consists of ID-s and ID names and for each record find ID name. The only problem here is, that traversing through all records in the DataTable and write each particular record in new column is very, very slow.

How can I solve this faster?

My first tables column: ID
My seoond tables columns: ID, ID_Name

It is possible for me to query the database directly, but what is the query to find names for each particular ID in the first table?

My final result should like:

ID_name
ID_name
ID_name

(names for each particular ID) - so only one column should be in return.


Greetings, Frenky
AntonioRodrigoAsked:
Who is Participating?
 
VipulKadiaConnect With a Mentor Commented:
Select FirstTable.ID, SecondTable.ID_Name
FROM FirstTable LEFT JOIN SecondTable
ON FirstTable.ID = SecondTable.ID
0
 
sachinpatil10dCommented:
what VipulKadia done is right
only one change so only one column should be in return.

Select FirstTable.ID+ '_'  + SecondTable.ID_Name
FROM FirstTable LEFT JOIN SecondTable
ON FirstTable.ID = SecondTable.ID

Open in new window

0
 
gavsmithCommented:
Are both of the DataTables in the same DataSet? If so you should be able to create a DataRelation between the 2 tables the use GetChildRows to retrieve the names.
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
gavsmithCommented:
I saw DataTable and presumed this was a programming question not a sql question... I'm still not sure?!
0
 
VipulKadiaCommented:
And if you want only Names then write as follows :

Select SecondTable.ID_Name
FROM FirstTable LEFT JOIN SecondTable
ON FirstTable.ID = SecondTable.ID
0
 
AntonioRodrigoAuthor Commented:
Very very good job. Thanks.
0
 
AntonioRodrigoAuthor Commented:
Yes, I want only names, so I've made this little change of the query immediately. It solves the problem, 100%.

I am using WinForms, but data is coming from SQL server. I've decided to make a query command builder in my code and send it to sql server.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.