Skip to main content

Joining in SQL

This article explains how to join tables in SQL Server.

Joining in SQL

There are various type of SQL joinning. A few of them that are commonly used are described below.
1: Inner join
2: Right Outer Join
3: Left Outer join

First Step

First create a SQL table named bio.


The following is the second table named fee.
 
2nd table

Now insert values into the bio table.

 insert values in bio table

Now insert values into the fee table.
 
 insert values in fee table

Second Step
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class SQLJoins : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    SqlConnection con = new SqlConnection(@"Data Source=IS9;Initial Catalog=practice;User ID=sa;Password=isource");
    protected void innerJoin_Click(object sender, EventArgs e)
    {
        con.Open();
        SqlCommand cmd=new SqlCommand ();
        cmd.Connection = con;
        cmd.CommandText = "Select * From bio Inner Join fee on bio.id=fee.id";
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
        con.Close();
    }

Inner Join
    protected void RightOuterJoin_Click(object sender, EventArgs e)
    {
        con.Open();
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandText = "Select * From bio Right  Outer Join fee on bio.id=fee.id";
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
        con.Close();   
    }
 
Outer Join 
 
    protected void LeftOuterJoin_Click(object sender, EventArgs e)
    {
        con.Open();
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandText = "Select * From bio Left Outer Join fee on bio.id=fee.id";
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
        con.Close();   
    }   
}
 
Left Outer Join 

Comments

Popular posts from this blog

Login Form Using LINQ in Windows Forms Application

Step 1:  First we create a database and then Table in SQL Server. Here we create a table named “logintb”. Now insert a name and Password into the logintb table. Step 2:  Open Visual Studio then select "File" -> "New" -> "Project..." then select "Windows Forms". Drag and drop a Label for ID and a Label for password. Drag and drop a TextBox for txtuser and a TextBox for txtPassword. Drag and drop a Button for Login. Step 3:  Go to the project then right-click and seelct "Add" -> "New Item..." then select "Windows Form" -> "LINQ to SQL Classes" then click on "Add". Now go to the Server Explorer, drag and drop the logintb table as in the following: Open Server Explorer and select the database and then the table. Step 4 :  Now double-click on the button Login in the form and write the following code: CS Code:   using  System; using  System.Collections.Generic; using  System.Comp

Working With a Binding Navigator Control in Windows Forms

Definition : BindingNavigator control Provide user interface with a simple data navigation and manipulation on the form . it is used for Binding the Database Tables values to the control (Datagridview,Text Box, label etc.). BindingNavigator with the BindingSource enable users to move through data records on a form and interact with the records. There are few buttons in binding Navigator listed below: 1.       Move First--------------- à Go first record of the table 2.       Move Next--------------- à Go Next record of the table with respect to current record. 3.       Move Previous---------- à Go previous record of the table. 4.       Move Last---------------- à Go Last record of the table. 5.       Delete---------------------- à Delete selected row (record). 6.       Add New-------------------- à Add New Row. 7.       Tool Strip------------------- à You can add button, Label, Image and Textbox etc. 8.       Position Item--------------- à current row count. 9.

How to Pass Data from one Form to other Form in windows Form Application C#

Introduction: Hi Guys! In this tutorial we learn how to pass data from one form to other form in windows form application using c#. Let’s Follow the Step and create windows form application. Step1: Visual studio- à File- à New- à Project à windows Form Application- à Ok Step2: Click on ToolBox- à Drag and drop Label and TextBox. I creat a Form with 3 Labels and TextBox as shown in fig. Step 3: I have Name,Fname,Address Label on Form .so I take a three global variable .Write this code on the Form1.cs         public static string SetValueForText1 = "" ;         public static string SetValueForText2 = "" ;         public static string SetValueForText3 = "" ; Step4: Project- à Add Windows Form- à Click on Add Step 5: After Creating Form Double Click on Submit button on windows Form1 and Write the Code:   private void button1_Click( object sender, EventArgs e)