Click here to Skip to main content
15,917,795 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,
i want to write a query which take some datas from table1 and some from table2 and shows them in dataGridview.i read the (INNER JOIN) statement but i thinck that it compare the records from two tables and then shows them ,but i want just take some records from two tables and show them (not to compare them).
plz help me
thanks a lot
Posted
Comments
Herman<T>.Instance 19-Jul-11 4:03am    
can you show the current query or link statement (you marked this thread as c#, not as sql)

There are two ways for doing the same thing !
1>> using classical and coding solution >> It will help up understanding ddata bounding more deeply !
2>> using Ado.Net easy solution by Genrating views from both the table >> More depndent on visual studio !

// I would surly go for More conceptual methad rather than fast and easy >>
// but its Totaly upto you I had posted both !!!




1>> clasical Method :

Unless you dont tranform your data schema in more deep normalization for you will not be able to do the same


For THis follow few steps >>

1. try normalizing you data schema /
//for example : if you have two different table assign a unique id in both the table as primary key and then create a new table with only those same primary keys . i.e; breaking your schema into more normal form .

2. try campareing for you firt table to joint table then from join table to last . this will help you in getting data from bth the tables .



Explanantion >>
SQL JOIN

The JOIN keyword is used in an SQL statement to query data from two or more tables, based on a relationship between certain columns in these tables.

Tables in a database are often related to each other with keys.

A primary key is a column (or a combination of columns) with a unique value for each row. Each primary key value must be unique within the table. The purpose is to bind data together, across tables, without repeating all of the data in every table.

Look at the "Persons" table:
P_Id LastName FirstName Address City
1 Hansen Ola Timoteivn 10 Sandnes
2 Svendson Tove Borgvn 23 Sandnes
3 Pettersen Kari Storgt 20 Stavanger

Note that the "P_Id" column is the primary key in the "Persons" table. This means that no two rows can have the same P_Id. The P_Id distinguishes two persons even if they have the same name.

Next, we have the "Orders" table:
O_Id OrderNo P_Id
1 77895 3
2 44678 3
3 22456 1
4 24562 1
5 34764 15

Note that the "O_Id" column is the primary key in the "Orders" table and that the "P_Id" column refers to the persons in the "Persons" table without using their names.

Notice that the relationship between the two tables above is the "P_Id" column.



this could help you a lot !


2>>easy solution in seconds

//*** Importantly if you are using Ado.net try Genre-ting views

>> in veiw table include the data from both the tables and then In data grid view bound the veiw as your source this will defently help you ....

For any more problems Contact anurag.smart4u@gmail.com
 
Share this answer
 
v2
Hi,

Try to use DISTINCT and WHERE clause
if it could help...
As example:
StringBuilder sbQry = new StringBuilder();
sbQry.Append("select DISTINCT A.CorpCode, B.PlanCode, A.EmpName ");
sbQry.Append("FROM DBO.Employee A, DBO.EmpPlan B ");
sbQry.Append("WHERE  A.EmployeeId=B.EmployeeId ");

string qry = sbQry.ToString();


Please remember to mark the replies as answers if they help and unmark them if they provide no help.

Regards,

Algem
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900