# 2019/0304/SS集合運算/子查詢/exists\&asp.net GridView 事件 OnRowDataBound

## SS

### 集合運算

聯集  union差集 except交集 intersect

欄位數量必須相等 ， 資料型態必須一樣

兩次查詢 一個結果

```
--集合運算set
--聯集union
select 姓名 from 員工
union
select 姓名 from 學生


--交集intersect
select 姓名 from 員工
intersect
select 姓名 from 學生

--差集except
select 姓名 from 員工
except
select 姓名 from 學生

select 姓名 from 學生
except
select 姓名 from 員工
-------------------------------

select 姓名 from 員工
union all
select 姓名 from 學生
--------------------------------
--語法注意:欄位數目一樣,欄位資料型態一樣
select 身份證字號,姓名 from 員工
union
select 姓名,性別 from 學生

```

### 子查詢&#x20;

(同一個查詢) ex.班級表裡面張無忌選了哪幾門課但只有他的學號(在另外一張表內)

#### 資料筆數大時合併查詢效能比子查詢好

1.合併查詢(join別張資料表進來且關聯後下條件)

2.子查詢(先用以知資料查詢到查詢資料，再用查詢資料查詢)

```
select * from 班級 where 學號=(select 學號 from 學生 where 學生.姓名='張無忌')

select *
from 員工
where 薪水>=50000
--找哪些員工的薪水>=平均薪資
select *
from 員工
where 薪水>=(select avg(薪水) from 員工)
```

竭盡可能的去想出一段sql去看能否拉出資料，再來才重複驗證是否正確

### 合併查詢 效能 > 子查詢 >= 集合運算

取別名就像取id，可用於完全表達法

自身取別名合併 (卡式基扣掉自己)

原因自身表欄位無法跟自身運算去比較

on 看成 where 的條件

```
--利用合併查詢寫法
select a.身份證字號,a.姓名,a.薪水
from 員工 as a inner join 員工 as b
on a.身份證字號!=b.身份證字號
group by a.身份證字號,a.姓名,a.薪水
having a.薪水>=avg(b.薪水)

select a.身份證字號,a.姓名,a.薪水
from 員工 as a cross join 員工 as b
where a.身份證字號!=b.身份證字號
group by a.身份證字號,a.姓名,a.薪水
having a.薪水>=avg(b.薪水)


select a.身份證字號,a.姓名,a.薪水
from 員工 as a , 員工 as b
where a.身份證字號!=b.身份證字號
group by a.身份證字號,a.姓名,a.薪水
having a.薪水>=avg(b.薪水)
```

子查詢 查出來的 通常不止一筆資料 很容易同名同姓錯誤結果

只有子查詢資料只有一筆資料時才會用等於

而必須使用 in 避免未來出現錯誤

exists 主查詢與子查詢是否有相同資料 ((合併寫法就沒有主子問題

不需要 欄位對欄位

用在只知道主鍵與關聯時&#x20;

其實是透過合併查詢的方法但是子查詢的語句表現

A與B之間無關聯則無法使用EXISTS

##

## OnRowDataBound 事件

###

```
 <asp:GridView ID="GridView2" runat="server" DataSourceID="SqlDataSource1" AutoGenerateColumns="False" 
                OnRowDataBound="GridView2_RowDataBound" >
```

#### 藉由繫結事件去寫c\#

#### 控制項細節還是要靠自己改

如 男女 or true false

可以從前端與後端改，也可去sql改

控制項屬性不熟悉就去查字典

### BoundField 控制項

asp 繫結 sql 都是一筆一筆繫結上去 一百萬筆就是一百萬次

![](/files/-L_6CiKPTgREH5W88Zpd)

先做了欄位 才是值 所以必須排除欄位

欄位索引值為 -1

控制項細節可以寫c#一筆一筆去改

![](/files/-L_6J_ELUbEettV3WuNF)

```
        if (e.Row.RowIndex > -1)
            {
                //if (e.Row.Cells[3].Text == "False")
                //    e.Row.Cells[3].Text = "女";
                //else
                //    e.Row.Cells[3].Text = "男";

                e.Row.Cells[3].Text = e.Row.Cells[3].Text == "False" ? "女" : "男";

                string stEeduLevel = "";
                switch (e.Row.Cells[4].Text)
                {
                    case "1":
                        stEeduLevel = "國小";
                        break;
                    case "2":
                        stEeduLevel = "國中";
                        break;
                    case "3":
                        stEeduLevel = "高中";
                        break;
                    case "4":
                        stEeduLevel = "大學";
                        break;
                    case "5":
                        stEeduLevel = "研究所以上";
                        break;

                }
                e.Row.Cells[4].Text = stEeduLevel;
```

### SqlDataSource 控制項

同樣方式也可藉由修改 sql 裡面的屬性再去拉鏈結做修改

```

SelectCommand="SELECT * FROM [Members]"


SelectCommand="SELECT account,pswd, [name], birthday,email, gender, edu.EduLevel,notes
FROM [Members] inner join Edu on Members.EduLevel=edu.EduLevel_Code
```

### allowsorting 排序布林屬性&#x20;

需要指定 sortexpression

預設元屬性是依照SelectCommand抓取的sql原本的主key去排

### allowpaging 分頁屬性

設定 pagesize 屬性

### GridView 控制項

內  < 自動編碼裡面都是有關設計的屬性

#### pagersettings

mode

pagebuttoncount

```
 <asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" AllowPaging="true" PageSize="1">

                <PagerSettings Mode="NextPreviousFirstLast" PageButtonCount="3" NextPageText="8" PreviousPageText="7"
                     FirstPageText="9" LastPageText=":"
                    />
                <PagerStyle Font-Names="Webdings" />
            </asp:GridView>
```

### aps.net  前端的方法去操作後端的物件

控制項的css都是編譯inline產生

asp.net要寫css or js要去看編譯完後的原始碼再去在style cript增加屬性

![](/files/-L_6hOcdgEhj24ow17YA)

```
     #GridView1 tr:last-child table a{
            text-decoration:none;
        }
        
        
         <div>
	<table cellspacing="0" rules="all" border="1" id="GridView1" style="border-collapse:collapse;">
		<tr>
			<th scope="col">Account</th><th scope="col">Pswd</th><th scope="col">Name</th><th scope="col">Birthday</th><th scope="col">Email</th><th scope="col">Gender</th><th scope="col">EduLevel</th><th scope="col">Notes</th>
		</tr><tr>
			<td>bochun</td><td>\_+*&gt;夭_?/棎?焮砒&lt;許	鵨=&quot;</td><td>岳不群</td><td>1989/5/21 上午 12:00:00</td><td>bochun@wda.gov.tw</td><td><span class="aspNetDisabled" title="Gender"><input id="GridView1_ctl00_0" type="checkbox" name="GridView1$ctl02$ctl00" disabled="disabled" /></span></td><td>4</td><td>&nbsp;</td>
		</tr><tr style="font-family:Webdings;">
			<td colspan="8"><table>
				<tr>
					<td><a href="javascript:__doPostBack(&#39;GridView1&#39;,&#39;Page$Next&#39;)">8</a></td><td><a href="javascript:__doPostBack(&#39;GridView1&#39;,&#39;Page$Last&#39;)">:</a></td>
				</tr>
			</table></td>
		</tr>
	</table>
</div>
        </div>
```

#### css 難是難在怎在對的時間狀態選到要操控的物件

```
     <style>
        #GridView1 tr:last-child table a{
            text-decoration:none;

        }
        #GridView1 tr:last-child table a:hover{
            font-size:24pt;

        }
          #GridView1 tr:last-child table span{
            font-size:larger;
            color:red;
        }
      #GridView1 tr:last-child table a[href*="First"]{
            font-family:Webdings;
        }
       #GridView1 tr:last-child table a[href*="Last"]{
            font-family:Webdings;
        }
    </style>
```

a \[ href ] 標籤a裡面 href屬性

\*= 包含

"First"


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://johch3n611u.gitbook.io/c50108/ju-li-cheng-bei/201903/2019-0304-ss.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
