ASP.NET 網頁製作教本 -- 從基本語法學起 第10章 ADO.NET 與

Download Report

Transcript ASP.NET 網頁製作教本 -- 從基本語法學起 第10章 ADO.NET 與

ASP.NET 網頁製作教本 –
從基本語法學起
第10章 ADO.NET 與資料庫存取(二)
10-1
Command 物件與資料
的增刪修改
Action Query 指令
刪除資料列:Delete 指令。
 更新資料列:Update 指令
 新增資料列:Insert Into 指令。
 建立新資料表:Select Into 指令。

Delete 指令:刪除資料列
格式:
Delete From 資料表 Where 條件式
例子:
Delete From 成績單 Where 數學 = 0
Delete From 成績單
Update 指令:更新資料列
Update 成績單 Set 數學 = 數學 ^ 0.5 * 10
資料表
指定運算式
Update 成績單 Set 數學 = 數學 ^ 0.5 * 10
Where 姓名 Like “王%”
Update 成績單 Set 數學 = 數學 ^ 0.5 * 10, 國文
= 國文 ^ 0.5 * 10, 英文 = 英文 ^ 0.5 * 10
Insert Into 指令:新增資料列

Insert Into 指令有兩種格式:
Insert Into 資料表 (欄位串列) Values (欄位值串
列)
Insert Into 資料表 (欄位一, 欄位二, …) Select …
Insert Into 指令:新增資料列
格式一:Insert Into …
Values …(1)
Insert Into 成績單 ( 學號, 姓名, 國文 )
Values ( 850330, "張無忌", 70 )
學號
姓名
國文
英文
數學
850301
陳桶一
90
76
98
850302
黃光權
58
77
75
850303
胡生妙
41
14
33
850304
王為全
95
97
87
850305
李日正
59
66
57
…
…
…
…
…
850329
鍾百達
0
0
20
850330
張無忌
70
新增此一資料列
Insert Into 指令:新增資料列
格式一:Insert Into …
Values …(2)
Insert Into 成績單 Values ( 850331, "胡青牛", 90, 80, 88 )
學號
姓名
國文 英文 數學
850301 陳桶一
90
76
98
850302 黃光權
58
77
75
850303 胡生妙
41
14
33
850304 王為全
95
97
87
850305 李日正
59
66
57
…
…
…
850329 鍾百達
0
0
20
850330 張無忌
70
850331 胡青牛
90
80
88
…
…
新增此一資料列
Insert Into 指令:新增資料列
格式二:Insert Into … Select … (1)

此一格式把 Select 指令所選取的資料列
新增到另一個資料表中,例如:
選取的結果新增到另一個資料表
Insert Into 成績單複製本 Select * From 成績單
Insert Into 指令:新增資料列
格式二:Insert Into … Select …
(2)
「成績單」資料表 => 「成績單複製本」資料表
學號
姓名
國文 英文
數學
學號
姓名
國文 英文 數學
850301 陳桶一
90
76
98
850301
陳桶一
90
76
98
850302 黃光權
58
77
75
850302
黃光權
58
77
75
850303 胡生妙
41
14
33
850303
胡生妙
41
14
33
850304 王為全
95
97
87
850304
王為全
95
97
87
850305 李日正
59
66
57
850305
李日正
59
66
57
…
…
…
…
…
…
…
…
0
0
20
850329
鍾百達
0
0
20
…
…
850329 鍾百達
Insert Into 指令:新增資料列
格式二:Insert Into … Select …
(3)

如果兩者的欄位名稱或順序不一致,則
應一一指定欄位名稱才可以,例如:
Insert Into 成績單複製本 (姓名, 學號, 數學, 英文, 國文)
Select 姓名, 學號, 數學 ^ 0.5 * 10, 英文 ^ 0.5 * 10,
國文 ^ 0.5 * 10 From 成績單
Select Into 指令:建立新資料表

Select Into 比較常見的用法是選取既有資
料表的資料列,然後建立成另一個新的
資料表,例如:
Select * Into 數學高手 From 成績單 Where 數學 >= 90
1.先寫好 Select 指令,並且加以測試
Select * From 成績單 Where 數學 >= 90
2.插入「Into 數學高手」
Select * Into 數學高手 From 成績單 Where 數學 >= 90
利用 Command 物件執行
Action Query

先利用 New OleDbCommand() 建立
Command 物件,然後再呼叫
ExecuteNonQuery 方法即可。
' Conn為已連結到資料庫的Connection物件
Cmd = New OleDbCommand( SQL指令, Conn )
Cmd.ExecuteNonQuery()
利用 Command 物件執行
Action Query – 測試程式
TestCmd.aspx Part I
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<HTML>
<BODY bgcolor="#FFFFFF">
<H2>TestCmd.aspx -- Action Query 測試程式<HR></H2>
<Form runat="server">
資料庫:Sample.mdb<BR>
指 令:<asp:TextBox runat="server" id="SQL" Size=50 />
<p>
<asp:Button runat="server" Text="執行" OnClick="RunSQL" />
TestCmd.aspx Part II
<HR>指令執行情況:
<asp:Label runat="server" id="Msg" Text="尚未輸入 SQL 指令"
ForeColor="Red" />
</Form>
</BODY>
</HTML>
<script Language="VB" runat="server">
Sub RunSQL(sender As Object, e As EventArgs)
Dim Conn As OleDbConnection
Dim Cmd
As OleDbCommand
TestCmd.aspx Part III
Dim Provider = "Provider=Microsoft.Jet.OLEDB.4.0"
Dim Database = "Data Source=" & Server.MapPath( "Sample.mdb" )
Conn = New OleDbConnection( Provider & ";" & DataBase )
Conn.Open()
On Error Resume Next
Cmd = New OleDbCommand( SQL.Text, Conn )
Cmd.ExecuteNonQuery()
If Err.Number <> 0 Then
Msg.Text = Err.Description
Else
Msg.Text = "成功!"
End If
Conn.Close()
End Sub
</script>
含有參數的 SQL 指令
Insert.aspx Part I
%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<Html>
<Body BgColor="White">
<H3>Insert.aspx -- 新增資料到「成績單」資料表<HR></H3>
<Form runat="server">
<Blockquote>
學號: <asp:TextBox runat="server" id="學號" /><br>
姓名: <asp:TextBox runat="server" id="姓名" /><br>
國文: <asp:TextBox runat="server" id="國文" /><br>
英文: <asp:TextBox runat="server" id="英文" /><br>
數學: <asp:TextBox runat="server" id="數學" /><p>
Insert.aspx Part II
<asp:Button runat="server" Text="新增" OnClick="InsertData" />
</Blockquote>
<HR><asp:Label runat="server" id="Msg" ForeColor="Red" />
</Form>
</Body>
</Html>
<script Language="VB" runat="server">
Sub InsertData(sender As Object, e As EventArgs)
Dim Conn As OleDbConnection
Dim Cmd As OleDbCommand
Dim Provider = "Provider=Microsoft.Jet.OLEDB.4.0"
Dim Database = "Data Source=" & Server.MapPath( "Sample.mdb" )
Conn = New OleDbConnection( Provider & ";" & DataBase )
Conn.Open()
Insert.aspx Part III
Dim SQL As String
SQL = "Insert Into 成績單 (學號, 姓名, 國文, 英文, 數學 )
Values( ?, ?, ?, ?, ?)"
Cmd = New OleDbCommand( SQL, Conn )
Cmd.Parameters.Add( New OleDbParameter("學號", OleDbType.Integer))
Cmd.Parameters.Add( New OleDbParameter("姓名", OleDbType.Char, 10))
Cmd.Parameters.Add( New OleDbParameter("國文", OleDbType.SmallInt))
Cmd.Parameters.Add( New OleDbParameter("英文", OleDbType.SmallInt))
Cmd.Parameters.Add( New OleDbParameter(""數學", OleDbType.SmallInt))
Insert.aspx Part IV
Cmd.Parameters("學號").Value
Cmd.Parameters("姓名").Value
Cmd.Parameters("國文").Value
Cmd.Parameters("英文").Value
Cmd.Parameters("數學").Value
Cmd.ExecuteNonQuery()
If Err.Number <> 0 Then
Msg.Text = Err.Description
Else
Msg.Text = "資料新增成功!"
End If
Conn.Close()
End Sub
</script>
=
=
=
=
=
Val(學號.Text)
姓名.Text
Val(國文.Text)
Val(英文.Text)
Val(數學.Text)
設定參數型別的
Cmd.Parameters.Add() 敘述 (1)

「成績單」資料表的「學號、姓名、國
文、英文、數學」欄位,而這幾個欄位
的型別如下:
欄位
mdb資料庫的欄位型別
學號
姓名
國文
英文
數學
長整數
字元(Char)
整數
整數
整數
對應之OleDb型別
Integer
Char
SmallInt
SmallInt
SmallInt
長度
-
10
-
-
-
設定參數型別的
Cmd.Parameters.Add() 敘述 (2)

為了建立 5 個參數並且將其資料型別對
應到上表的型別,所撰寫的敘述如下:
Cmd.Parameters.Add( New OleDbParameter("學號",
OleDbType.Integer))
Cmd.Parameters.Add( New OleDbParameter("姓名",
OleDbType.Char, 10))
Cmd.Parameters.Add( New OleDbParameter("國文",
OleDbType.SmallInt))
Cmd.Parameters.Add( New OleDbParameter("英文",
OleDbType.SmallInt))
Cmd.Parameters.Add( New OleDbParameter("數學",
OleDbType.SmallInt))
設定參數型別的
Cmd.Parameters.Add() 敘述 (3)

參數二(OleDbType) 型別的設定值
mdb資料庫的欄位型別 OleDbType的設定值
VarChar
備忘
TinyInt
短整數
DBDate
日期時間
Currency
金額
Single
單精準度
Double
倍精準度
Boolean
布林資料
VarBinary
二進位資料
設定參數值的
Cmd.Parameters().Value 敘述
Cmd.Parameters("學號").Value = Val(學號.Text)
Cmd.Parameters("姓名").Value = 姓名.Text
Cmd.Parameters("國文").Value = Val(國文.Text)
Cmd.Parameters("英文").Value = Val(英文.Text)
Cmd.Parameters("數學").Value = Val(數學.Text)

TextBox 所讀取的內容為字串資料,要呼
叫 Val 函數將其轉換成數值資料。
含有參數的 SQL 指令 -- 具名的參數
Cmd.Parameters.Add( New OleDbParameter("@學號", OleDbType.Integer))
Cmd.Parameters.Add( New OleDbParameter("@姓名", OleDbType.Char, 10))
Cmd.Parameters.Add( New OleDbParameter("@國文", OleDbType.SmallInt))
Cmd.Parameters.Add( New OleDbParameter("@英文", OleDbType.SmallInt))
Cmd.Parameters.Add( New OleDbParameter("@數學", OleDbType.SmallInt))
Cmd.Parameters("@學號").Value = Val(學號.Text)
Cmd.Parameters("@姓名").Value = 姓名.Text
Cmd.Parameters("@國文").Value = Val(國文.Text)
Cmd.Parameters("@英文").Value = Val(英文.Text)
Cmd.Parameters("@數學").Value = Val(數學.Text)
10-2 DataGrid 與資料的修改
DataGrid 與資料的修改
(a)
(b)
Update01.aspx -- 基本架構
將修改過的資料
顯示出來,暫時
不寫到資料庫。
DataGrid 的安插
<asp:DataGrid runat="server" id="MyGrid"
HeaderStyle-BackColor="#AAAADD"
AlternatingItemStyle-BackColor="#FFFFC0"
BorderColor="Black"
CellPadding="2"
CellSpacing="0"
OnEditCommand="EditData"
OnUpdateCommand="UpdateData"
OnCancelCommand="CancelEdit"
DataKeyField="學號">
<Columns>
<asp:EditCommandColumn
HeaderText="修改" ItemStyle-Wrap="False"
EditText="編輯" UpdateText="更新" CancelText="取消" />
</Columns>
</asp:DataGrid>
幾個屬性的意義
屬性
HeaderText
ItemStyle-Wrap
EditText
UpdateText
CancelText
意義
欄位抬頭
欄位是否自動換行
「編輯」連結所顯示的文字
「更新」連結所顯示的文字
「取消」連結所顯示的文字
EditData 事件程序的撰寫
Sub EditData(sender As Object, e As DataGridCommandEventArgs)
MyGrid.EditItemIndex = e.Item.ItemIndex
OpenDataBase_And_BindToDataGrid()
End Sub
CancelEdit 事件程序的撰寫
Sub CancelEdit(sender As Object, e As DataGridCommandEventArgs)
MyGrid.EditItemIndex = -1
OpenDataBase_And_BindToDataGrid()
End Sub
UpdateData 事件程序的撰寫
Part I
Sub UpdateData(sender As Object, e As DataGridCommandEventArgs)
Dim TB(5) As TextBox
TB(1)
TB(2)
TB(3)
TB(4)
TB(5)
=
=
=
=
=
e.Item.Cells(1).Controls(0)
e.Item.Cells(2).Controls(0)
e.Item.Cells(3).Controls(0)
e.Item.Cells(4).Controls(0)
e.Item.Cells(5).Controls(0)
'
'
'
'
'
學號
姓名
國文
英文
數學
TextBox
TextBox
TextBox
TextBox
TextBox
UpdateData 事件程序的撰寫
Part II
Dim Msg As String = "欲更新的資料:<blockquote>"
Msg &= "DataKey = " & MyGrid.DataKeys(e.Item.ItemIndex)
Msg &= "<br>Cell(1) = " & TB(1).Text
Msg &= "<br>Cell(2) = " & TB(2).Text
Msg &= "<br>Cell(3) = " & TB(3).Text
Msg &= "<br>Cell(4) = " & TB(4).Text
Msg &= "<br>Cell(5) = " & TB(5).Text & "</blockquote>"
Response.Write( Msg )
MyGrid.EditItemIndex = -1
OpenDataBase_And_BindToDataGrid()
End Sub
程式最重要的關鍵(1)
e.Item.Cells(2).Controls(0)
e.Item.Cells(1).Controls(0)
e.Item.Cells(4).Controls(0)
e.Item.Cells(3).Controls(0)
e.Item.Cells(5).Controls(0)
程式最重要的關鍵(2)
Dim TB(5) As TextBox ' 宣告TextBox
' 將DataGrid中的TextBox指定給我們宣告的TextBox
TB(1) = e.Item.Cells(1).Controls(0) ' 學號 TextBox
TB(2) = e.Item.Cells(2).Controls(0) ' 姓名 TextBox
TB(3) = e.Item.Cells(3).Controls(0) ' 國文 TextBox
TB(4) = e.Item.Cells(4).Controls(0) ' 英文 TextBox
TB(5) = e.Item.Cells(5).Controls(0) ' 數學 TextBox
' 接下來分別利用TB(1).Text、TB(2).Text、TB(3).Text、TB(4).Text
' 及TB(5).Text讀取DataGrid等5個TextBox的內容。
Update02.aspx –
將修改的資料寫入資料庫 Part I
#01
Sub UpdateData(sender As Object, e As DataGridCommandEventArgs)
#02
Dim Conn As OleDbConnection
#03
Dim Cmd
As OleDbCommand
#04
Dim SQL
As String
#05
#06
Dim Provider = "Provider=Microsoft.Jet.OLEDB.4.0"
#07
Dim Database = "Data Source=" & Server.MapPath( "Sample.mdb" )
#08
Conn = New OleDbConnection( Provider & ";" & DataBase )
#09
Conn.Open()
#10
#11
SQL = "Update 成績單 Set 學號=@學號, 姓名=@姓名, 國文=@國文, 英文=@英
文, 數學=@數學 Where 學號=@Key"
#12
#13
Cmd = New OleDbCommand( SQL, Conn )
Update02.aspx –
將修改的資料寫入資料庫 Part II
#14
Cmd.Parameters.Add( New OleDbParameter("@學號", OleDbType.Integer))
#15
Cmd.Parameters.Add( New OleDbParameter("@姓名", OleDbType.Char, 10))
#16
Cmd.Parameters.Add( New OleDbParameter("@國文", OleDbType.SmallInt))
#17
Cmd.Parameters.Add( New OleDbParameter("@英文", OleDbType.SmallInt))
#18
Cmd.Parameters.Add( New OleDbParameter("@數學", OleDbType.SmallInt))
#19
Cmd.Parameters.Add( New OleDbParameter("@Key",
OleDbType.Integer))
#20
#21
Dim TB(5) As TextBox
#22
TB(1) = e.Item.Cells(1).Controls(0) ' 學號 TextBox
#23
TB(2) = e.Item.Cells(2).Controls(0) ' 姓名 TextBox
#24
TB(3) = e.Item.Cells(3).Controls(0) ' 國文 TextBox
#25
TB(4) = e.Item.Cells(4).Controls(0) ' 英文 TextBox
#26
TB(5) = e.Item.Cells(5).Controls(0) ' 數學 TextBox
#27
Update02.aspx –
將修改的資料寫入資料庫 Part III
#28
Cmd.Parameters("@Key").Value = MyGrid.DataKeys(e.Item.ItemIndex)
#29
Cmd.Parameters("@學號").Value = Val(TB(1).Text)
#30
Cmd.Parameters("@姓名").Value = TB(2).Text
#31
Cmd.Parameters("@國文").Value = Val(TB(3).Text)
#32
Cmd.Parameters("@英文").Value = Val(TB(4).Text)
#33
Cmd.Parameters("@數學").Value = Val(TB(5).Text)
#34
Cmd.ExecuteNonQuery()
#35
#36
Conn.Close()
#37
#38
MyGrid.EditItemIndex = -1
#39
OpenDataBase_And_BindToDataGrid()
#40
End Sub
Update03.aspx –
設定不可修改的欄位
Update02.aspx
學號欄位不可修改
學號欄位可以修改
Update03.aspx
DataGrid 所做的修改 Part I
<asp:DataGrid runat="server" id="MyGrid"
HeaderStyle-BackColor="#AAAADD"
AlternatingItemStyle-BackColor="#FFFFC0"
BorderColor="Black"
CellPadding="2"
CellSpacing="0"
OnEditCommand="EditData"
OnUpdateCommand="UpdateData"
OnCancelCommand="CancelEdit"
DataKeyField="學號"
AutoGenerateColumns="False" >
DataGrid 所做的修改 Part II
<Columns>
<asp:EditCommandColumn
HeaderText="修改" ItemStyle-Wrap="False"
EditText="編輯" UpdateText="更新" CancelText="取消" />
<asp:BoundColumn DataField="學號" HeaderText="學號"
ReadOnly="True" />
<asp:BoundColumn DataField="姓名" HeaderText="姓名"/>
<asp:BoundColumn DataField="國文" HeaderText="國文"/>
<asp:BoundColumn DataField="英文" HeaderText="英文"/>
<asp:BoundColumn DataField="數學" HeaderText="數學"/>
</Columns>
</asp:DataGrid>
程式所做的修改

更新資料的SQL指令:
SQL = "Update 成績單 Set 姓名=@姓名, 國文=@國文,
英文=@英文,
數學=@數學 Where 學號=@Key"

「@學號」參數的相關敘述均需刪除:
Cmd.Parameters.Add( New OleDbParameter("@學號",
OleDbType.Integer))
TB(1) = e.Item.Cells(1).Controls(0) ' 學號 TextBox
Cmd.Parameters("@學號").Value = Val(TB(1).Text)
Update04.aspx –
設定 ListBox 及 CheckBox 欄位
CheckBox
ListBox
DataGrid 的安插
EditCommandColumn 欄位。
 BoundColumn 欄位 。
 HyperLinkColumn 欄位。
 TemplateColumn 欄位。

TemplateColumn 欄位
安插之語法及結構 (1)
<asp:TemplateColumn HeaderText="抬頭">
<ItemTemplate>
在此安插顯示資料的Server控制元件
</ItemTemplate>
<EditItemTemplate>
在此安插編輯資料的Server控制元件
</asp:TemplateColumn>
</asp:TemplateColumn>
TemplateColumn 欄位
安插之語法及結構 (2)

例如:
<asp:TemplateColumn HeaderText="姓名">
<ItemTemplate>
<asp:Label runat="server"
Text='<%# Container.DataItem("姓名") %>'/>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" id="姓名" Size=10
Text='<%# Container.DataItem("姓名") %>'/>
</EditItemTemplate>
</asp:TemplateColumn>
TemplateColumn 欄位
安插之語法及結構 (3)
<ItemTemplate>
<asp:Label runat="server"
Text='<%# Container.DataItem("姓名") %>'/>
<EditItemTemplate>
<asp:TextBox runat="server" id="姓名" Size=10
Text='<%# Container.DataItem("姓名") %>'/>
Update04.aspx 網頁的欄位(1)

顯示及編輯所使用 Server 控制元件分別
如下:
欄位
姓名
性別
血型
已婚
顯示用的控制元件
Label
Label
Label
Label
編輯用的控制元件
TextBox
ListBox
ListBox
CheckBox
Update04.aspx 網頁的欄位(2)
已婚欄位
<asp:TemplateColumn HeaderText="已婚">
<ItemTemplate>
<asp:Label runat="server"
Text='<%# Container.DataItem("已婚") %>'/>
</ItemTemplate>
<EditItemTemplate>
<asp:CheckBox runat="server" id="已婚"
Checked='<%# Container.DataItem("已婚") %>'/>
</EditItemTemplate>
</asp:TemplateColumn>
Update04.aspx 網頁的欄位(3)
性別欄位
<EditItemTemplate>
<asp:ListBox runat="server" id="性別" Rows=1
SelectedIndex='<%# 性別編號(Container.DataItem("性別")) %>'>
<asp:ListItem>男</asp:ListItem>
<asp:ListItem>女</asp:ListItem>
</asp:ListBox>
</EditItemTemplate>
Update04.aspx 網頁的欄位(4)
血型欄位
<EditItemTemplate>
<asp:ListBox runat="server" id="血型" Rows=1
SelectedIndex='<%# 血型編號(Container.DataItem("血型")) %>'>
<asp:ListItem>A</asp:ListItem>
<asp:ListItem>B</asp:ListItem>
<asp:ListItem>O</asp:ListItem>
<asp:ListItem>AB</asp:ListItem>
</asp:ListBox>
</EditItemTemplate>
Update04.aspx 網頁的欄位(5)
Function 血型編號( 血型 As String ) As Integer
If 血型 = "A" Then Return 0
If 血型 = "B" Then Return 1
If 血型 = "O" Then Return 2
If 血型 = "AB" Then Return 3
End Function
Update04.aspx 網頁的欄位(6)
Function 性別編號( 性別 As String ) As Integer
If 性別 = "男" Then Return 0
If 性別 = "女" Then Return 1
End Function
Update04.aspx 資料的更新(1)
Dim Text姓名 As TextBox
Text姓名 = e.Item.FindControl("姓名")
Cmd.Parameters("@姓名").Value = Text姓名.Text
Dim List性別 As ListBox
List性別 = e.Item.FindControl("性別")
Cmd.Parameters("@性別").Value = List性別.SelectedItem.Text
Update04.aspx 資料的更新 (2)
Dim List血型 As ListBox
List血型 = e.Item.FindControl("血型")
Cmd.Parameters("@血型").Value = List血型.SelectedItem.Text
Dim Check已婚 As CheckBox
Check已婚 = e.Item.FindControl("已婚")
Cmd.Parameters("@已婚").Value = Check已婚.Checked
10-3 DataSet 物件與 XML 的讀寫
XML 格式的資料庫檔案 (1)

Score.xml Part I
<?xml version="1.0" standalone="yes"?>
<NewDataSet>
<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:Locale="zh-TW">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element name="成績單">
<xs:complexType>
<xs:sequence>
XML 格式的資料庫檔案 (2)

Score.xml Part II
<xs:element name="學號" type="xs:int" minOccurs="0" />
<xs:element name="姓名" type="xs:string" minOccurs="0" />
<xs:element name="國文" type="xs:short" minOccurs="0" />
<xs:element name="英文" type="xs:short" minOccurs="0" />
<xs:element name="數學" type="xs:short" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
XML 格式的資料庫檔案 (3)

Score.xml Part III
<成績單>
<學號>850301</學號>
<姓名>陳捅一</姓名>
<國文>90</國文>
<英文>76</英文>
<數學>98</數學>
</成績單>
<成績單>
<學號>850302</學號>
<姓名>黃光權</姓名>
<國文>58</國文>
<英文>77</英文>
<數學>75</數學>
</成績單>
...中間資料省略...
</NewDataSet>
XML 檔案的讀取與產生
XmlRead.aspx Part I
<%@ Import Namespace="System.Data" %>
<script Language="VB" runat="server">
Sub Page_Load(sender As Object, e As EventArgs)
Dim Ds As New DataSet
Ds.ReadXml( Server.MapPath("Score.xml") )
MyGrid.DataSource = Ds.Tables( "成績單" ).DefaultView
MyGrid.DataBind()
End Sub
</script>
XmlRead.aspx Part II
<Html>
<Body BgColor="White">
<H3>XmlRead.aspx -- 讀取 Score.xml 成為「成績單」資料表
<HR></H3>
<Center>
<Form runat="server">
<asp:DataGrid runat="server" id="MyGrid"
HeaderStyle-BackColor="#AAAADD"
AlternatingItemStyle-BackColor="#FFFFC0"
BorderColor="Black"
CellPadding="2"
CellSpacing="0" />
</Form>
<p></Center>
<HR></Body>
</Html>
XmlWrite.aspx Part I
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<script Language="VB" runat="server">
Sub Page_Load(sender As Object, e As EventArgs)
Dim Conn As OleDbConnection
Dim Adpt As OleDbDataAdapter
Dim Ds As DataSet
Dim Provider = "Provider=Microsoft.Jet.OLEDB.4.0"
Dim Database = "Data Source=" & Server.MapPath( "Sample.mdb" )
Conn = New OleDbConnection( Provider & ";" & DataBase )
Conn.Open()
PXmlWrite.aspx Part II
Dim SQL = "Select * From 成績單"
Adpt = New OleDbDataAdapter( SQL, Conn )
Ds = New Dataset()
Adpt.Fill(Ds, "成績單")
' 將 DataSet 物件的內容寫到 Score2.xml 檔案
Ds.WriteXml( Server.MapPath("Score2.xml"),
XmlWriteMode.WriteSchema )
Conn.Close()
Response.Write( "已經將「成績單」資料表寫到 Score2.xml!" )
End Sub
</script>
10-4 存取 SQL Server 資料庫
轉換提供的資料表 -- 情況一

有安裝附屬於 .NET Framework SDK 的
SQL Server Desktop Engine。
按下
按下
按下
轉換提供的資料表--情況二 (1)

安裝有完整的 SQL Server:
開啟 ch10/Sql 目錄,然後逐一修改每
一個 .aspx 檔案的以下敘述:
Conn = New SqlConnection("server=(local)\NetSDK;" & _
"database=pubs;" & _
"Trusted_Connection=Yes")
轉換提供的資料表--情況二 (2)

Server 參數
原內容為 (local)\NetSDK,其中 (local) 表
示本機、而 NetSDK 為 SQL Server
Desktop Engine 安裝時所設定的執行個體
名稱。如果您安裝 SQL Server 之後,沒
有額外建立新的執行個體,此一參數應
設定成 (local),如果有建立新的執行個
體,而想採用新的執行個體為資料庫,
則將此一參數設定成 (local)\執行個體名
稱。
轉換提供的資料表--情況二 (3)

Trusted_Connection=Yes
將此一參數刪除,然後設定成 user id 及
password 參數,其中 user id 參數建議設定成 sa,
而 password 參數則設定成 sa 這個帳號的密碼,
假設您安裝 SQL Server 時,並沒有額外建立新
的執行個體,而 sa 帳號的密碼設定為
“xo123”,則敘述要修改成:
Conn = New SqlConnection("server=(local);" & _
"database=pubs;" & _
"user id=sa;password=xo123")
從 OleDb 到 SqlClient (1)

將 <%@ Import Namespace=“System.Data.OleDb” %>
標示改成:
<%@ Import Namespace=“System.Data.SqlClient” %>。
也就是說,我們原本使用 OleDb 命名空間的物件,
現在要改成用 SqlClient 命名空間的物件。
從 OleDb 到 SqlClient (2)

將所有以 OleDb 開頭的物件,包含:
OleDbConnection
OleDbCommand
OleDbDataReader
OleDbDataAdapter
…

改成以 Sql 開頭的物件,如下:
SqlConnection
SqlCommand
SqlDataReader
SqlDataAdapter
…
從 OleDb 到 SqlClient (3)

將建立 Connection 物件的敘述:
Dim Provider = "Provider=Microsoft.Jet.OLEDB.4.0"
Dim Database = "Data Source=" &
Server.MapPath( "Sample.mdb" )
Conn = New
OleDbConnection( Provider & ";" & DataBase )
Conn.Open()

改成:
Conn = New SqlConnection("server=(local)\NetSDK;" & _
"database=pubs;" & _
"Trusted_Connection=Yes")
Conn.Open
Route01.aspx Part I
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<script Language="VB" runat="server">
Sub Page_Load(sender As Object, e As EventArgs)
' 相關物件的宣告
Dim Conn As SqlConnection
' 宣告一個 Connection 物件
Dim Adpt As SqlDataAdapter
' 宣告一個 DataAdapter 物件
Dim Ds As DataSet
' 宣告一個 DataSet 物件
' Connection 物件開啟 Sql Server, pub 資料庫
Conn = New SqlConnection("server=(local)\NetSDK;" & _
"database=pubs;" & _
"Trusted_Connection=Yes")
Conn.Open()
Route01.aspx Part II
' DataAdapter 物件開啟「成績單」資料表
Dim SQL = "Select * From 成績單"
Adpt = New SqlDataAdapter( SQL, Conn )
' 將 DataAdapter 物件所開啟的「成績單」資料表存放於 DataSet 物件中
Ds = New Dataset()
Adpt.Fill(Ds, "成績單")
' DataGrid控制元件顯示「成績單」資料表
MyGrid.DataSource = Ds.Tables( "成績單" ).DefaultView
MyGrid.DataBind()
' 關閉Sample.mdb資料庫
Conn.Close()
End Sub
</script>
Route01.aspx Part III
<Html>
<Body BgColor="White">
<H3>資料庫存取路徑一: Connection - DataAdapter - DataSet - DataGrid
<HR></H3>
<Center>
<Form runat="server">
<asp:DataGrid runat="server" id="MyGrid"
HeaderStyle-BackColor="#AAAADD"
AlternatingItemStyle-BackColor="#FFFFC0"
BorderColor="Black"
CellPadding="2"
CellSpacing="0" />
</Form>
<p></Center>
<HR></Body>
</Html>
含有參數的 SQL 指令
Cmd.Parameters.Add( New OleDbParameter("@學號", OleDbType.Integer))
Cmd.Parameters.Add( New OleDbParameter("@姓名", OleDbType.Char, 10))
Cmd.Parameters.Add( New OleDbParameter("@國文", OleDbType.SmallInt))
Cmd.Parameters.Add( New OleDbParameter("@英文", OleDbType.SmallInt))
Cmd.Parameters.Add( New OleDbParameter("@數學", OleDbType.SmallInt))

改成
Cmd.Parameters.Add( New SqlParameter("@學號", SqlDbType.Integer))
Cmd.Parameters.Add( New SqlParameter("@姓名", SqlDbType.Char, 10))
Cmd.Parameters.Add( New SqlParameter("@國文", SqlDbType.SmallInt))
Cmd.Parameters.Add( New SqlParameter("@英文", SqlDbType.SmallInt))
Cmd.Parameters.Add( New SqlParameter("@數學", SqlDbType.SmallInt))
SQL Server 不接受沒有具名的 SQL 指令

SQL 指令如下:
Insert Into 成績單 (學號, 姓名, 國文, 英文, 數學 )
Values( ?, ?, ?, ?, ?)

而 SQL Server 並不接受沒有具名的 SQL
指令,所以一定要先把沒有具名的 SQL
指令改成具名的 SQL 指令,該網頁才能
再轉換成為存取 SQL Server 的網頁。
欄位填入空白的問題
Function 血型編號( 血型 As String ) As Integer
If Trim(血型) = "A" Then Return 0
If Trim(血型) = "B" Then Return 1
If Trim(血型) = "O" Then Return 2
If Trim(血型) = "AB" Then Return 3
End Function