linq 聯合查詢(Join)


查詢人員信息

條件:當此人員信息在部門部門下面則查詢

 1  public partial class LinqJoin : System.Web.UI.Page
 2     {
 3        
 4         protected void Page_Load(object sender, EventArgs e)
 5         {
 6             DataLoad();
 7             DataInit();
 8         }
 9         /// <summary>
10         /// Sql查詢
11         /// </summary>
12         public void DataInit()
13         {
14             //計算時間差
15             Stopwatch watch = new Stopwatch();
16             //打開表
17             watch.Start();
18             using (SqlConnection conn = new SqlConnection(@"server=HAPPY-PC;User ID=sa;Password=happy;database=Exam4.1_OK;Connection Reset=FALSE;Max Pool Size = 512;"))
19             {
20                 conn.Open();
21 
22                 using (SqlCommand cmd = conn.CreateCommand())
23                 {
24                     //查詢UserId<276234的有部門Id的用戶信息
25                     cmd.CommandText = "select a.* from users a inner join Structure c on a.StructureID=c.StructureID where a.UserId<276234";
26 
27                     SqlDataAdapter adapter = new SqlDataAdapter();
28                     adapter.SelectCommand = cmd;
29                     DataSet ds = new DataSet();
30 
31                     adapter.Fill(ds);
32 
33                     this.GridView1.DataSource = ds;
34                     this.GridView1.DataBind();
35                     ds.Dispose();
36 
37                 }
38             }
39             //關閉表
40             watch.Stop();
41             //打印時間
42             Response.Write("我是第一個:"+watch.Elapsed.Milliseconds.ToString());
43         }
44         /// <summary>
45         /// Linq查詢
46         /// </summary>
47         private  void DataLoad()
48         {
49             //計算時間差
50             Stopwatch watch = new Stopwatch();
51             //打開表
52             watch.Start();
53             using (LinqInnerJoinDataContext ctx = new LinqInnerJoinDataContext("server=HAPPY-PC;User ID=sa;Password=happy;database=Exam4.1_OK;Connection Reset=FALSE;Max Pool Size = 512;"))
54             {
55                 //查詢UserId<276234的有部門Id的用戶信息
56                 var table = from c in ctx.Users
57                             join p in ctx.Structure
58                             on c.StructureID equals p.StructureID.ToString()
59                             where c.UserID < 276234
60                             select c;
61 
62                 this.GridView2.DataSource = table;
63                 this.GridView2.DataBind();
64             }
65             //關閉表
66             watch.Stop();
67             //顯示時間
68             Response.Write("我是第二個:" + watch.Elapsed.Milliseconds.ToString());
69         }
70     }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM