zoukankan      html  css  js  c++  java
  • DataSet 多表关系

    protected void Page_Load(object sender, EventArgs e)
    {
        string connectionString = @"Data Source=.;Initial Catalog=Pubs;Integrated Security=SSPI";
        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");
            //多对多关系 
            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 += " " + rowAuthor["au_fname"]; lblList.Text += " " + rowAuthor["au_lname"] + " ";
                foreach (DataRow rowTitleAuthor in rowAuthor.GetChildRows(Authors_TitleAuthor))
                {
                    DataRow rowTitle = rowTitleAuthor.GetParentRows(Titles_TitleAuthor)[0];
                    lblList.Text += "  ";
                    lblList.Text += rowTitle["title"] + " ";
                }
            }
        }
        catch (Exception err)
        {
            lblList.Text = "Error reading list of names. ";
            lblList.Text = err.Message;
        }
        finally
        {
            con.Close();
        }
    }
  • 相关阅读:
    ethtool 命令输出的注意点--网卡参数
    centos7.2+zabbix3.2+sedmail邮件告警
    vscode monokai
    SQL SERVER 常用命令
    数据库问题6-將系統資料表對應至系統檢視
    数据库问题5-SYS.SYSPROCESSES使用和查找死锁
    select * from sys.sysprocesses
    【SQL Server学习笔记】事务、锁定、阻塞、死锁 sys.sysprocesses
    使用DMV调优性能 --Burgess_Liu
    sql server线程等待信息
  • 原文地址:https://www.cnblogs.com/wanghaibin/p/3872087.html
Copyright © 2011-2022 走看看