Click here to Skip to main content
14,271,339 members
Rate this:
Please Sign up or sign in to vote.
See more:
Good day everyone please i am working on c# windowsform applicaton for loan i want to join two database base on a particular column. i have  two database(employement,loanform) please i want to join the two of them to a new table so there will not be duplicate colums)
database employementform(1st database)
 id          employementnumber      fullname      age      
1            ts/02/001               adedoyin     40
2            ts/02/003                chisom      25
3            ts/02/012                 femi       44
4            ts/02/022                 James      33
database loanform(2nd database)
id          employementnumber      amountofloan         percent         totalamount
1           ts/02/003                10000               10                100000
2           ts/02/003               2500                 10                 2750
3            ts/02/01               1200                 10               1320
i want to join them on a new database to show on a datagridview and i want it to be like this:
database join
 id     employementnumber fullname     age    amountofloan   percent  totalamount
 1      ts/02/003          adedoyin     40       10000           10     100000
2        ts/02/003        adedoyin      40        2500            10     2750
3        ts/02/01         chisom       25        1200            10     1320

What I have tried:

SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\odt\Documents\Loanapp.mdf;Integrated Security=True;Connect Timeout=30");
          DataTable dt = new DataTable();
          SqlCommand cmd = con.CreateCommand();
          cmd.CommandType = CommandType.Text;
          cmd.CommandText = "select * from loanform,Registrationform";

          SqlDataAdapter da = new SqlDataAdapter(cmd);
Updated 30-May-19 9:19am

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

What you need is a JOIN: SQL Joins[^]
Something like:
SELECT, a.Employmentnumber, b.fullname, b.age, a.amountofloan, a.percent, a.totalamount FROM loanform a
JOIN employmentform b ON a.employmentnumber = b.employmentnumber

But ... that's a poor bit of design you have there.
Instead of storing EmploymentNumber in each row of the loanform, store the ID from the employmentform as a foreign key, and instead of storing the age store the DOB - you can work out the age from that, but age changes, DOB doesn't.

And those aren't databases, they are tables within a database - it's very important to use the right terminology, or answers can be very, very wrong.

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

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100