偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

SQL Server數(shù)據(jù)庫DataRelation的應(yīng)用示例詳解

數(shù)據(jù)庫 SQL Server
本文我們主要介紹了SQL Server數(shù)據(jù)庫DataRelation的一個示例,通過這個示例讓我們來了解一下DataRelation的應(yīng)用吧,希望能夠?qū)δ兴鶐椭?/div>

SQL Server數(shù)據(jù)庫DataRelation的應(yīng)用是本文我們主要要介紹的內(nèi)容,我們知道,System.Data.DataRelation 類,表示兩個DataTable 對象之間的父/子關(guān)系。在常見的查詢中,可以利用SQL Server 2005/2008的CTE應(yīng)用來進(jìn)行遞歸查詢,這里有一個典型示例:http://www.cnblogs.com/downmoon/archive/2009/10/23/1588405.html。

此外,在數(shù)據(jù)量不大的情況下,也可以用DataRelation進(jìn)行主子表或父子表的關(guān)聯(lián)。我們假定:有兩張表請假類型LeaveType和請假表Leave,這里是一個表結(jié)構(gòu)的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 

再插入一些測試數(shù)據(jù):

代碼如下:

  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 '產(chǎn)假',1 union all  
  8. select '特休假',1   
  9. go  
  10.  
  11. Insert into Leave  
  12. select '請假'+Convert( Nvarchar(11),dateadd(dd,-500,getdate()),120),'準(zhǔn)備與方鴻漸結(jié)婚','孫嘉柔',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),'準(zhǔn)備為方鴻漸生孩子','孫嘉柔',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),'準(zhǔn)備與方鴻漸離婚','孫嘉柔',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),'準(zhǔn)備與方鴻漸離婚','孫嘉柔',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),'準(zhǔn)備與方鴻漸離婚','孫嘉柔',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數(shù)據(jù)庫DataRelation的應(yīng)用示例詳解

關(guān)于SQL Server數(shù)據(jù)庫用DataRelation進(jìn)行主子表或父子表的關(guān)聯(lián)的知識就介紹到這里了,希望本次的介紹能夠?qū)δ兴斋@!

【編輯推薦】

  1. SQL Server多表查詢優(yōu)化方案總結(jié)
  2. SQL Server數(shù)據(jù)庫ISNULL函數(shù)的應(yīng)用實(shí)例
  3. SQL Server數(shù)據(jù)庫DATEPART的語法及使用實(shí)例
  4. SQL Server根據(jù)子節(jié)點(diǎn)查詢所有父節(jié)點(diǎn)的代碼示例
  5. SQL Server臟讀方式數(shù)據(jù)提取之NOLOCK和READPAST
責(zé)任編輯:趙鵬 來源: 博客園
相關(guān)推薦

2021-03-18 08:20:19

SQLServer數(shù)據(jù)庫SQL

2011-08-09 17:24:21

SQL Server 數(shù)據(jù)庫日志

2010-07-06 14:12:58

SQL Server數(shù)

2010-06-30 11:31:55

SQL Server數(shù)

2010-07-06 15:07:37

SQL Server

2011-04-02 11:02:54

SQL Server數(shù)文件恢復(fù)

2011-08-24 12:49:56

SQL Server托管代碼

2011-08-30 11:04:30

鏈接查詢內(nèi)連接外連接

2011-08-22 11:39:53

SQL Server數(shù)PIVOT

2010-07-15 17:28:50

SQL Server

2010-03-16 10:12:40

SQL Server

2011-08-11 09:12:31

SQL Server nolock

2011-08-25 13:41:50

SQL Server 變更跟蹤

2011-08-15 11:24:46

SQL Server事務(wù)

2010-06-17 10:02:12

SQL Server數(shù)

2011-08-18 10:36:24

SQL ServerISNULL函數(shù)

2011-08-15 14:12:16

SQL ServerDATEDIFF

2010-07-13 09:12:56

SQL Server

2009-04-30 09:28:05

SynonymOpenquerySQL Server

2011-08-22 13:28:56

FOR XMLSQL Server
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號