Follow in Twitter & Facebook

Like in Facebook

Powered By Blogger Widgets

Free Download

FREE Tools

Monday, September 3, 2012

Retrieving data from multiple tables using

In this article we will discuss how to retrieve data from multiple tables. You can also check my last article on DataReader in

Suppose there are multiple tables in SQL Server database. For this example we will retrive using disconnected architure.

Suppose we have 3 tables namely: Employee, Department, EmployeeCategory. In the very straight forward
approach, we can retrieve the data individually. We will use DataAdapter to retrive data.

Below is the full code:

 void GetDataFromMultipleTable()
        string connectionString = WebConfigurationManager.ConnectionStrings["ConnectionStringName"].ConnectionString;
        SqlConnection con = new SqlConnection(connectionString);
        string strSQL = "SELECT * FROM Employee";
        SqlCommand cmd = new SqlCommand(strSQL, con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
            da.Fill(ds, "Employee");

            cmd.CommandText = "SELECT * FROM Department";
            da.Fill(ds, "Department");

            cmd.CommandText = "SELECT * FROM EmployeeCategory";
            da.Fill(ds, "EmployeeCategory");
        catch (Exception ex)
            //Handle exception here.

Here if you will check the dataset ds, then you will get 3 datatables name as: Employee, Department and EmployeeCategory.

0 on: "Retrieving data from multiple tables using"