SQL Server数据库DataRelation的应用示例详解

SQL Server数据库DataRelation的应用是本文我们主要要介绍的内容,我们知道,System.Data.DataRelation 类,表示两个DataTable 对象之间的父/子关系。在常见的查询中,可以利用SQL Server 2005/2008的CTE应用来进行递归查询,这里有一个典型示例:http://www.cnblogs.com/downmoon/archive/2009/10/23/1588405.html。

此外,在数据量不大的情况下,也可以用DataRelation进行主子表或父子表的关联。我们假定:有两张表请假类型LeaveType和请假表Leave,这里是一个表结构的SQL,代码如下:

  1. create table LeaveType (  
  2.    PKID                 int                  identity(1,1),  
  3.    TypeName             nvarchar(50)         null,  
  4.    CurState             smallint             not null default 0,  
  5.    constraint PK_LEAVETYPE primary key (PKID)  
  6. )  
  7. go  
  8.  
  9. create table Leave (  
  10.    PKID                 int                  identity(1,1),  
  11.    Title                nvarchar(50)         null,  
  12.    Reason               nvarchar(254)        null,  
  13.    LoginID              nvarchar(50)         null,  
  14.    LeaveTypeID            int ,  
  15.    DepartID             int                  null,  
  16.    EmployeeID           int                  null,  
  17.    AddTime              datetime             null,  
  18.    BeginTime            datetime             null,  
  19.    EndTime              datetime             null,  
  20.    TBeginDate           datetime             null,  
  21.    TEndDate             datetime             null,  
  22.    Remark               nvarchar(1000)       null,  
  23.    ModUser              nvarchar(50)         null,  
  24.    ModTime              datetime             null,  
  25.    CurState             smallint             not null default 0,  
  26.    constraint PK_LEAVE primary key (PKID)  
  27. )  
  28. go 

再插入一些测试数据:

代码如下:

  1. truncate table LeaveType  
  2. insert into   
  3. LeaveType   
  4. select '事假',1 union all  
  5. Select '病假',1 union all  
  6. select '婚假',1 union all  
  7. select '产假',1 union all  
  8. select '特休假',1   
  9. go  
  10.  
  11. Insert into Leave  
  12. select '请假'+Convert( Nvarchar(11),dateadd(dd,-500,getdate()),120),'准备与方鸿渐结婚','孙嘉柔',3,1,1909,getdate(),'2010-1-1','2012-1-1','2010-1-1','2012-1-1',  
  13. '这回铁了心了','孙嘉柔',getdate(),1  
  14. union all  
  15. select '回娘家'+Convert( Nvarchar(11),dateadd(dd,-200,getdate()),120),'准备为方鸿渐生孩子','孙嘉柔',4,1,1909,getdate(),'2010-1-1','2012-1-1','2010-1-1','2012-1-1',  
  16. '这回铁了心了','孙嘉柔',getdate(),1  
  17. union all   
  18. select    
  19. '回娘家'+Convert( Nvarchar(11),dateadd(dd,-10,getdate()),120),'准备与方鸿渐离婚','孙嘉柔',1,1,1909,getdate(),'2010-1-1','2012-1-1','2010-1-1','2012-1-1',  
  20. '这回铁了心了','孙嘉柔',getdate(),1  
  21. union all  
  22. select '回娘家'+Convert( Nvarchar(11),dateadd(dd,-2,getdate()),120),'准备与方鸿渐离婚','孙嘉柔',2,1,1909,getdate(),'2010-1-1','2012-1-1','2010-1-1','2012-1-1',  
  23. '这回铁了心了','孙嘉柔',getdate(),1  
  24.  
  25. union all  
  26. select '回娘家'+Convert( Nvarchar(11),getdate(),120),'准备与方鸿渐离婚','孙嘉柔',2,1,1909,getdate(),'2010-1-1','2012-1-1','2010-1-1','2012-1-1',  
  27. '这回铁了心了','孙嘉柔',getdate(),1  
  28.  
  29. update Leave set Title='第'+cast(PKID as nvarchar(10))+'次'+Title  

查询主要代码如下:

  1. protected void Page_Load(object sender, EventArgs e)  
  2.         {  
  3.  
  4.             SqlConnection objConn = default(SqlConnection);  
  5.             SqlDataAdapter da = default(SqlDataAdapter);  
  6.             DataSet ds = default(DataSet);  
  7.            //DataRow dtrParent = default(DataRow);  
  8.             //DataRow dtrChild = default(DataRow);  
  9.  
  10.             objConn = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["Testdb"]);  
  11.             da = new SqlDataAdapter("SELECT * FROM LeaveType", objConn);  
  12.             ds = new DataSet();  
  13.             try  
  14.             {  
  15.                 objConn.Open();  
  16.                 da.Fill(ds, "LeaveTypes");  
  17.                 da.SelectCommand = new SqlCommand("SELECT * FROM Leave", objConn);  
  18.                 da.Fill(ds, "Leaves");  
  19.             }  
  20.             catch (SqlException exc)  
  21.             {  
  22.                 Response.Write(exc.ToString());  
  23.             }  
  24.             finally  
  25.             {  
  26.                 objConn.Dispose();  
  27.             }  
  28.  
  29.             ////Create the Data Relationship  
  30.             ds.Relations.Add("Type_Leave", ds.Tables["LeaveTypes"].Columns["PKID"], ds.Tables["Leaves"].Columns["LeaveTypeID"]);  
  31.  
  32.             ////Display the Category and Child Products Within  
  33.             foreach (DataRow drParent in ds.Tables["LeaveTypes"].Rows)  
  34.             {  
  35.                 lblDisplay.Text += "<h3>" + drParent["TypeName"] + "</h3><ul>";  
  36.                 foreach (DataRow drChild in drParent.GetChildRows("Type_Leave"))  
  37.                 {  
  38.                     lblDisplay.Text += "<li>" + drChild["loginID"] + drChild["Title"] + drChild["Reason"] + "</li>";  
  39.                 }  
  40.                 lblDisplay.Text += "</ul>";  
  41.  
  42.             }  
  43.  
  44.         } 

最终效果:

关于SQL Server数据库用DataRelation进行主子表或父子表的关联的知识就介绍到这里了,希望本次的介绍能够对您有所收获!

【编辑推荐】

  1. SQL Server多表查询优化方案总结
  2. SQL Server数据库ISNULL函数的应用实例
  3. SQL Server数据库DATEPART的语法及使用实例
  4. SQL Server根据子节点查询所有父节点的代码示例
  5. SQL Server脏读方式数据提取之NOLOCK和READPAST

文章来源网络,作者:运维,如若转载,请注明出处:https://shuyeidc.com/wp/231661.html<

(0)
运维的头像运维
上一篇2025-04-19 19:35
下一篇 2025-04-19 19:36

相关推荐

发表回复

您的邮箱地址不会被公开。必填项已用 * 标注