Handle table relationship (C#) : SqlServer « ADO.net Database « ASP.NET Tutorial

ASP.NET Tutorial
1. ASP.Net Instroduction
2. Language Basics
3. ASP.net Controls
4. HTML Controls
5. Page Lifecycle
6. Response
7. Collections
8. Validation
9. Development
10. File Directory
11. Sessions
12. Cookie
13. Cache
14. Custom Controls
15. Profile
16. Configuration
17. LINQ
18. ADO.net Database
19. Data Binding
20. Ajax
21. Authentication Authorization
22. I18N
23. Mobile
24. WebPart
25. XML
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
ASP.NET Tutorial » ADO.net Database » SqlServer 
18. 42. 7. Handle table relationship (C#)
File: Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="TableRelationships" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="lblList" runat="server" EnableViewState="False"></asp:Label>
    </div>
    </form>
</body>
</html>

File: Default.aspx.cs

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Web.Configuration;

public partial class TableRelationships : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            CreateList();
        }
    }

    private string connectionString = WebConfigurationManager.ConnectionStrings["Pubs"].ConnectionString;


    private void CreateList()
    {
        string selectSQL = "SELECT au_lname, au_fname, au_id FROM Authors";
        SqlConnection con = new SqlConnection(connectionString);
        SqlCommand cmd = new SqlCommand(selectSQL, con);
        SqlDataAdapter adapter = new SqlDataAdapter(cmd);
        DataSet dsPubs = new DataSet();

        try
        {
            con.Open();
            adapter.Fill(dsPubs, "Authors");

            cmd.CommandText = "SELECT au_id, title_id FROM TitleAuthor";
            adapter.Fill(dsPubs, "TitleAuthor");

            cmd.CommandText = "SELECT title_id, title FROM Titles";
            adapter.Fill(dsPubs, "Titles");
        }
        catch (Exception err)
        {
            lblList.Text = "Error reading list of names. ";
            lblList.Text += err.Message;
        }
        finally
        {
            con.Close();
        }

        DataRelation Titles_TitleAuthor = new DataRelation("Titles_TitleAuthor",dsPubs.Tables["Titles"].Columns["title_id"],dsPubs.Tables["TitleAuthor"].Columns["title_id"]);
        DataRelation Authors_TitleAuthor = new DataRelation("Authors_TitleAuthor",dsPubs.Tables["Authors"].Columns["au_id"],dsPubs.Tables["TitleAuthor"].Columns["au_id"]);
        dsPubs.Relations.Add(Titles_TitleAuthor);
        dsPubs.Relations.Add(Authors_TitleAuthor);

        foreach (DataRow rowAuthor in dsPubs.Tables["Authors"].Rows)
        {
            lblList.Text += "<br /><b>" + rowAuthor["au_fname"];
            lblList.Text += " " + rowAuthor["au_lname""</b><br />";

            foreach (DataRow rowTitleAuthor in
                rowAuthor.GetChildRows(Authors_TitleAuthor))
            {
                foreach (DataRow rowTitle in
                    rowTitleAuthor.GetParentRows(Titles_TitleAuthor))
                {
                    lblList.Text += "&nbsp;&nbsp;";
                    lblList.Text += rowTitle["title""<br />";
                }
            }
        }
    }
}

File: Web.config

<?xml version="1.0"?>
<configuration>
  <connectionStrings>
    <add name="Pubs" connectionString="Data Source=localhost\SQLEXPRESS;Initial Catalog=Pubs;Integrated Security=SSPI"/>
  </connectionStrings>
</configuration>
18. 42. SqlServer
18. 42. 1. Connect to SQL server with integrated Authentication or SQL Authentication
18. 42. 2. Execute delete command with SqlCommand against SqlServer
18. 42. 3. You can connect to a Local database named MyLocalData.mdf by using the following connection string:
18. 42. 4. For example, the following connection string enables you to connect to a Server database named MyData:
18. 42. 5. You use SQLCMD by opening a command prompt and connecting to your database with the following command:
18. 42. 6. Read data from SQL server and fill asp:dropdownlist (C#)
18. 42. 7. Handle table relationship (C#)
18. 42. 8. Insert, update and delete (C#)
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.