日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢(xún)
選擇下列產(chǎn)品馬上在線(xiàn)溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷(xiāo)解決方案
SQLServer數(shù)據(jù)庫(kù)DataRelation的應(yīng)用示例詳解

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

目前創(chuàng)新互聯(lián)已為數(shù)千家的企業(yè)提供了網(wǎng)站建設(shè)、域名、雅安服務(wù)器托管成都網(wǎng)站托管、企業(yè)網(wǎng)站設(shè)計(jì)、嵐縣網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶(hù)導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶(hù)和合作伙伴齊心協(xié)力一起成長(zhǎng),共同發(fā)展。

此外,在數(shù)據(jù)量不大的情況下,也可以用DataRelation進(jìn)行主子表或父子表的關(guān)聯(lián)。我們假定:有兩張表請(qǐng)假類(lèi)型LeaveType和請(qǐng)假表Leave,這里是一個(gè)表結(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. create table Leave (
  9.    PKID                 int                  identity(1,1),
  10.    Title                nvarchar(50)         null,
  11.    Reason               nvarchar(254)        null,
  12.    LoginID              nvarchar(50)         null,
  13.    LeaveTypeID            int ,
  14.    DepartID             int                  null,
  15.    EmployeeID           int                  null,
  16.    AddTime              datetime             null,
  17.    BeginTime            datetime             null,
  18.    EndTime              datetime             null,
  19.    TBeginDate           datetime             null,
  20.    TEndDate             datetime             null,
  21.    Remark               nvarchar(1000)       null,
  22.    ModUser              nvarchar(50)         null,
  23.    ModTime              datetime             null,
  24.    CurState             smallint             not null default 0,
  25.    constraint PK_LEAVE primary key (PKID)
  26. )
  27. go

再插入一些測(cè)試數(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. Insert into Leave
  11. select '請(qǐng)假'+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',
  12. '這回鐵了心了','孫嘉柔',getdate(),1
  13. union all
  14. 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',
  15. '這回鐵了心了','孫嘉柔',getdate(),1
  16. union all 
  17. select  
  18. '回娘家'+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',
  19. '這回鐵了心了','孫嘉柔',getdate(),1
  20. union all
  21. 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',
  22. '這回鐵了心了','孫嘉柔',getdate(),1
  23. union all
  24. select '回娘家'+Convert( Nvarchar(11),getdate(),120),'準(zhǔn)備與方鴻漸離婚','孫嘉柔',2,1,1909,getdate(),'2010-1-1','2012-1-1','2010-1-1','2012-1-1',
  25. '這回鐵了心了','孫嘉柔',getdate(),1
  26. update Leave set Title='第'+cast(PKID as nvarchar(10))+'次'+Title 

查詢(xún)主要代碼如下:

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

    " + drParent["TypeName"] + "

      ";
    •                 foreach (DataRow drChild in drParent.GetChildRows("Type_Leave"))
    •                 {
    •                     lblDisplay.Text += "
    • " + drChild["loginID"] + drChild["Title"] + drChild["Reason"] + "
    • ";
    •                 }
    •                 lblDisplay.Text += "
    ";
  32.             }
  33.         }

最終效果:

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


當(dāng)前標(biāo)題:SQLServer數(shù)據(jù)庫(kù)DataRelation的應(yīng)用示例詳解
網(wǎng)頁(yè)網(wǎng)址:http://www.5511xx.com/article/djipiss.html