# 2019/0314/Asp.net鏈結資料庫-查詢

## asp.net鏈結資料庫-查詢

### 前情提要

sqldatasource 連接 資料庫

連接過的話會顯示在 web.cofig

gridview 顯示資料 相關屬性

###

### 既有資料做查詢

最簡單的方式gridview元件&#x20;

selectcommand 屬性&#x20;

寫入查詢sql語法做查詢&#x20;

### sqldatsource 控制項的 屬性

sql dml

4種 select insert update delete

![](/files/-L_uhk6pLnVaaMHuirMG)

#### controlparameter 控制項&#x20;

#### 用於鏈結 textbox控制項與 selectcommand屬性

```
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:MySystemConnectionString1 %>" 
SelectCommand="SELECT * FROM [Members] where Name=@name">
<SelectParameters>

<asp:ControlParameter Name="name" ControlID="txtName" Type="String" />
</SelectParameters>

</asp:SqlDataSource>
請輸入姓名:<asp:TextBox ID="txtName" runat="server"></asp:TextBox><asp:Button ID="Button1" runat="server" Text="確定" />
```

tsql 純量變數 @

declare @name Voichar(8)

php 變數$

### emptydatatext屬性:顯示查無此資料時之文本

```
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Account" DataSourceID="SqlDataSource1" 
EmptyDataText="查無資料!!">
```

### 模糊查詢語法置入

```
 <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:MySystemConnectionString1 %>" 
 SelectCommand="SELECT * FROM [Members] where Name like '%'+ @name + '%'">
```

#### 一次複製 visual studio 會自動增加不同 id

### fiterparameters 屬性 不用 where 也可查詢 ( 使用過濾器 )

![](/files/-L_upB1IvVlGiLtKWiQo)

```
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:MySystemConnectionString1 %>" 
SelectCommand="SELECT * FROM [Members]" FilterExpression="Name like '%{0}%'">
            
<FilterParameters>
       <asp:ControlParameter Name="name" ControlID="txtName2" Type="String" />
</FilterParameters>
 
</asp:SqlDataSource>

請輸入姓名:<asp:TextBox ID="txtName2" runat="server"></asp:TextBox><asp:Button ID="Button2" runat="server" Text="確定" />

<hr />

<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" DataKeyNames="Account" 
DataSourceID="SqlDataSource2" EmptyDataText="查無資料!!">
```

filterexpression 屬性 c# 自己的模糊查詢表示法

```
SelectCommand="SELECT * FROM [Members]" FilterExpression="Name like '%{0}%'"
```

#### sqldatasource  顯示方式 products 資料型態是 bit 只有0跟1 可以拿來代表上架下架

p 定價 p2 售價

intro 商品內容

### imagefield 控制項

url相關 都是在指定路徑

dataimageurlfield 圖檔資料夾

dataimageurlformatstring 圖檔路徑

```
<asp:ImageField 
DataImageUrlField="Product_Img" 
DataImageUrlFormatString="~\ProductsImg\s{0}">
</asp:ImageField>
```

![](/files/-L_v5zogep6kjwjuctoW)

{% embed url="<https://docs.microsoft.com/zh-tw/dotnet/api/system.web.ui.webcontrols.imagefield?redirectedfrom=MSDN&view=netframework-4.7.2>" %}

```
從上述網址找的範例
dataimageurlfield="LastName"
dataimageurlformatstring="~\Images\{0}.jpg"
```

## 不知道控制項屬性相關功能時 msdn 查詢關鍵字

class 類別 -> 命名空間&#x20;

建構子 co

屬性p

### gridview 控制項 data formatstring 型態自訂格式顯示

{% embed url="<https://docs.microsoft.com/zh-tw/dotnet/api/system.web.ui.webcontrols.imagefield.dataimageurlformatstring?view=netframework-4.7.2#System_Web_UI_WebControls_ImageField_DataImageUrlFormatString>" %}

\r \n c# 換行 html \<br>

### 設計模式 templatefield 自訂

![](/files/-L_v93LKOb7lrKmt-SuV)

```
<%--<asp:BoundField DataField="Product_Intro" HeaderText="Product_Intro" SortExpression="Product_Intro" />--%>
 
<asp:TemplateField HeaderText="Product_Intro" SortExpression="Product_Intro">
     <EditItemTemplate>
          <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Product_Intro") %>'></asp:TextBox>
     </EditItemTemplate>
     <ItemTemplate>
          <asp:Label ID="Label1" runat="server" Text='<%# Eval("Product_Intro").ToString().Replace("\n","<br>") %>'></asp:Label>
     </ItemTemplate>
</asp:TemplateField>
```

資料繫節表示式

```
Text='<%# Bind("Product_Intro") 
```

屬性 : EditItemTemplate 編輯欄位

屬性 : ItemTemplate 顯示欄位

方法 : Eval 可以讀資料&#x20;

方法: bind 可以讀也可以寫回去

Eval ("繫節欄位的名稱","可以設定顯示格式")

replace ("被取代的字串" , "取代成字串" )

## 如何結合前端??

拿 Bootstrap 寫好的 css 鏈結過來用&#x20;

gridview gridlines none 將表格拿掉

bootstrap css樣式表 都寫在class 套用就好

#### 題外話

公保退被砍 6成恩給制

繳低拿高  5年 勞退gg


---

# 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-0314-sqs.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.
