日期: 2023.11.02 星期二
-
建立資料庫
- 伺服器總管視窗若沒出現時: 執行檢視 > 伺服器總管

-
選擇左下視窗上的伺服器總管後然後選擇項目: 伺服器下的資料連接後,
滑鼠右鍵選擇建立新的SQL Server資料庫

- 伺服器名稱:
(localdb)mssqllocaldb - 登入伺服器:
使用 Windows 驗證 - 新資料庫名稱:
im


- 伺服器名稱:
- 伺服器總管視窗若沒出現時: 執行檢視 > 伺服器總管
-
建立資料表
-
資料表上,按下滑鼠右鍵,選擇加入新的資料表。

名稱 資料型別 允許 Null 預設值 PID varchar(50) Not Null PNAME varchar(50) Not Null PRICE int Not Null - 切換到頁籤:
T-SQL,將[Table]命名成[PRODUCT],然後執行更新

- 按下
更新資料庫

- 回到伺服器總管按下
更新,就可以看到資料表PRODUCT

-
-
新增資料
選擇資料表PRODUCT後,點擊滑鼠右側,選擇顯示資料表資料,然後在出現視窗上開始編輯與新增資料

PID PNAME PRICE P01 Monitor 1200 P02 Case 3900 P03 CPU 12000 P04 Power 3000 
打完一筆資料庫管理系統自動將這筆資料寫入資料庫,可以按下左上角確認內容,刪除可以顯取該筆後,點擊滑鼠右鍵後選擇刪除。
- 開始建立新專案: 檔案 > 新增 > 專案




- 於專案項目上,滑鼠右鍵選擇加入
Web 表單

指定項目的名稱:WebForm1即表單項目名稱

-
WebForm1上,滑鼠右鍵,選擇設計工具檢視,進入設計界面

-
案例一:
WebForm1網頁打開執行時就將能到資料庫把資料表PRODUCT撈出來放於ListBox控制項上
在控制項上顯示產品名稱,當按下該名稱項就會顯示該產品價格。- 設計
UI介面- 加入控制項
ListBox1。
- 加入控制項
-
產生
PageLoad事件方法protected void Page_Load(object sender, EventArgs e) { //還沒 PostBack if (!this.IsPostBack) { //建立連線字串 //@ 避免遇到特殊字元,例如 string strcon = @"Data Source=(localdb)mssqllocaldb;Initial Catalog=im;Integrated Security=True;Pooling=False"; //要與 MSSQL 連線的話,要使用對應 ADO.NET 的套件,所以需要 SqlClient 套件 //所以需先引入 System.Data.SqlClient; SqlConnection sqlcon = new SqlConnection(strcon); //連接資料庫 sqlcon.Open(); //產生 SQL 命令物件 SqlCommand sqlcmd = new SqlCommand(); //連線時要透過哪一個連線字串來連接 sqlcmd.Connection = sqlcon; //建立 SQL 語句,要告訴資料庫做什麼: 查詢產品名稱與價錢 sqlcmd.CommandText = "select PNAME, PRICE from product "; //要求資料庫系統執行並且將結果回傳封裝成 SQLDataReader 物件的型式 SqlDataReader sqldr = sqlcmd.ExecuteReader(); //執行一次 SqlDataReader.Reader() 方法會讀出一筆資料,有成功會回傳 True //取出 n 筆資料 while( sqldr.Read()){ //準備 ListItem ListItem aaa = new ListItem(); //這一行不能寫在外面變成全域事件,這樣取出的值都相同,因為指向同一位址 aaa.Text = sqldr.GetString(0); //欄位 PName aaa.Value = sqldr.GetInt32(1).ToString(); //欄位 Price //放到 ListBox ListBox1.Items.Add(aaa); } } } -
測試結果

連線字串


引入類別
System.Data.SqlClient
- 設計
-
案例二: 新增一個功能,即是按下
ListBox裡面的項目內容(產品名稱),就會帶出產品價格。UI部分:
Label1.Text = ""

- 因為要按下去列出產品價格,需在伺服器端作,既然要在伺服端執行(才會知道選了哪一項目),就必須
Postback,而且須將價格放在Label1內。 - 在控制項
ListBox上設定屬性:AutoPostBack=True或

-
雙擊控制項
ListBox來建立事件ListBox1_SelectedIndexChang方法(這事件被觸發時機在於項目改變時)protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e) { //Label1.Text = ListBox1.SelectedItem; //找到選擇到的 ListItem // 要顯示價格 Label1.Text = ListBox1.SelectedItem.Value; //或 Label1.Text = ListBox1.SelectedValue; } - 測試結果

-
List的相關控制項,主要要放許多控制項,這些項目通常可以從資料庫取得,取得後就可以放入List內,這些可以不用自己用while loop來完成,這功能所有相關List控制項都有一很重要功能,要把資料丟到List控制項,可改寫下列:: //取出 n 筆資料 while (sqldr.Read()) { //準備 ListItem ListItem aaa = new ListItem(); aaa.Text = sqldr.GetString(0); //欄位 PName aaa.Value = sqldr.GetInt32(1).ToString(); //欄位 Price //放到 ListBox ListBox1.Items.Add(aaa); }變成為:
//資料來源來自 SqlReader ListBox1.DataSource = sqldr; //要顯示的資料欄位 ListBox1.DataTextField = "PNAME"; ListBox1.DataValueField = "PRICE"; //這個就是作前面 while 的部分,把資料一筆一筆讀到 ListBox 裡面 ListBox1.DataBind(); //關閉資料庫連線 sqlcon.Close();
不使用自己寫 Ado.Net 程式的方式
-
前言
前面介紹的是自己寫程式,如下的方法。string strcon = @"Data Source=(localdb)mssqllocaldb;Initial Catalog=im;Integrated Security=True;Pooling=False"; SqlConnection sqlcon = new SqlConnection(strcon); sqlcon.Open(); SqlCommand sqlcmd = new SqlCommand(); sqlcmd.Connection = sqlcon; sqlcmd.CommandText = "select PNAME, PRICE from product "; SqlDataReader sqldr = sqlcmd.ExecuteReader(); ListBox1.DataSource = sqldr; ListBox1.DataTextField = "PNAME"; ListBox1.DataValueField = "PRICE"; ListBox1.DataBind(); sqlcon.Close(); -
專案上加入
WebForm2.aspx,作出與WebForm1.aspx相同的功能。

-
UI部分:ListBox1Label1.Text = ""

- 啟用
Auto PostBack

-
不使用任何程式碼
- 到控制項選取
資料>SqlDataSource拖曳到WebForm2上任一位置,這個SqlDataSource控制項沒有介面不同於其他控制項。
這控制項主要用來連接資料庫用
-
按一下設定
資料來源

-
選擇資料連接: 目的主要產生連線字串
- 當已經建立好資料庫時:

- 無資料庫時:
新增連接

選擇MS SQL SERVER

- 當已經建立好資料庫時:
-
將連接字串儲存到應用程式組態檔(方案中的
Web.config)
不建議寫到一般檔案中,容易讓別人看到,可以寫到 Web.config 會加密過

勾選:
是,將這個連接儲存為imConnectionString,存成Web.config,部分內容是:<connectionStrings> <add name="imConnectionString" connectionString="Data Source=(localdb)mssqllocaldb;Initial Catalog=im;Integrated Security=True;Pooling=False" providerName="System.Data.SqlClient" /> </connectionStrings>用法是:
WebForm1.aspx中程式碼//引入 using System.Web.Configuration; protected void Page_Load(object sender, EventArgs e) { //還沒 PostBack if (!this.IsPostBack) { //建立連線字串 //@ 避免遇到特殊字元,例如 //string strcon = @"Data Source=(localdb)mssqllocaldb;Initial Catalog=im;Integrated Security=True;Pooling=False"; //回傳物件 WebConfigurationManager.ConnectionStrings["imConnection"] 中的 ConnectionString string strcon = WebConfigurationManager.ConnectionStrings["imConnectionString"].ConnectionString; } }可以把各式各樣連接字串寫(新增)到
<connectionStrings>裡面</connectionStrings> -
設定 Select 陳述式: 準備
SqlCommand物件

- 測試查詢

完成後,就會產生SqlDataReader物件
-
-
如何使用
點擊ListBox控制項上的選擇資料來源


-
雙擊控制項
ListBox來建立事件ListBox1_SelectedIndexChang方法(這事件被觸發時機在於項目改變時)protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e) { //Label1.Text = ListBox1.SelectedItem; //找到選擇到的 ListItem // 要顯示價格 Label1.Text = ListBox1.SelectedItem.Value; //或 Label1.Text = ListBox1.SelectedValue; } - 測試結果

- 到控制項選取
-
留言