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

数据库 SQL Server
本文我们主要介绍了SQL Server数据库DataRelation的一个示例,通过这个示例让我们来了解一下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的应用示例详解

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

【编辑推荐】

  1. SQL Server多表查询优化方案总结
  2. SQL Server数据库ISNULL函数的应用实例
  3. SQL Server数据库DATEPART的语法及使用实例
  4. SQL Server根据子节点查询所有父节点的代码示例
  5. SQL Server脏读方式数据提取之NOLOCK和READPAST
责任编辑:赵鹏 来源: 博客园
相关推荐

2011-08-09 17:24:21

SQL Server 数据库日志

2021-03-18 08:20:19

SQLServer数据库SQL

2010-07-06 14:12:58

SQL Server数

2010-06-30 11:31:55

SQL Server数

2010-07-06 15:07:37

SQL Server

2010-07-15 17:28:50

SQL Server

2011-08-30 11:04:30

链接查询内连接外连接

2011-08-22 11:39:53

SQL Server数PIVOT

2011-08-24 12:49:56

SQL Server托管代码

2011-04-02 11:02:54

SQL Server数文件恢复

2011-08-15 14:12:16

SQL ServerDATEDIFF

2011-08-25 13:41:50

SQL Server 变更跟踪

2011-08-15 11:24:46

SQL Server事务

2010-06-17 10:02:12

SQL Server数

2011-08-18 10:36:24

SQL ServerISNULL函数

2011-08-11 09:12:31

SQL Server nolock

2010-03-16 10:12:40

SQL Server

2010-07-08 11:05:14

SQL Server数

2010-07-13 09:12:56

SQL Server

2009-04-30 09:28:05

SynonymOpenquerySQL Server
点赞
收藏

51CTO技术栈公众号