Insert Into Insert Into Values 1 Insert Into

  • Slides: 81
Download presentation

Insert Into 指令:新增資料列 格式一:Insert Into … Values …(1)

Insert Into 指令:新增資料列 格式一:Insert Into … Values …(1)

Insert Into 指令:新增資料列 格式一:Insert Into … Values …(2) Insert Into 成績單 Values ( 850331,

Insert Into 指令:新增資料列 格式一:Insert Into … Values …(2) Insert Into 成績單 Values ( 850331, "胡青牛", 90, 88 )

Insert Into 指令:新增資料列 格式二:Insert Into … Select … (2)

Insert Into 指令:新增資料列 格式二:Insert Into … Select … (2)

利用 Command 物件執行Action Query n 先利用 New Ole. Db. Command() 建立 Command 物件,然後再呼叫 Execute.

利用 Command 物件執行Action Query n 先利用 New Ole. Db. Command() 建立 Command 物件,然後再呼叫 Execute. Non. Query 方法即可。 ' Conn為已連結到資料庫的Connection物件 Cmd = New Ole. Db. Command( SQL指令, Conn ) Cmd. Execute. Non. Query()

利用 Command 物件執行Action Query – 測試程式

利用 Command 物件執行Action Query – 測試程式

Test. Cmd. aspx Part I <%@ Import Namespace="System. Data" %> <%@ Import Namespace="System. Data.

Test. Cmd. aspx Part I <%@ Import Namespace="System. Data" %> <%@ Import Namespace="System. Data. Ole. Db" %> <HTML> <BODY bgcolor="#FFFFFF"> <H 2>Test. Cmd. aspx -- Action Query 測試程式<HR></H 2> <Form runat="server"> 資料庫:Sample. mdb<BR> 指 令:<asp: Text. Box runat="server" id="SQL" Size=50 /> <p> <asp: Button runat="server" Text="執行" On. Click="Run. SQL" />

Test. Cmd. aspx Part II <HR>指令執行情況: <asp: Label runat="server" id="Msg" Text="尚未輸入 SQL 指令" Fore.

Test. Cmd. aspx Part II <HR>指令執行情況: <asp: Label runat="server" id="Msg" Text="尚未輸入 SQL 指令" Fore. Color="Red" /> </Form> </BODY> </HTML> <script Language="VB" runat="server"> Sub Run. SQL(sender As Object, e As Event. Args) Dim Conn As Ole. Db. Connection Dim Cmd As Ole. Db. Command

Test. Cmd. aspx Part III Dim Provider = "Provider=Microsoft. Jet. OLEDB. 4. 0" Dim

Test. Cmd. aspx Part III Dim Provider = "Provider=Microsoft. Jet. OLEDB. 4. 0" Dim Database = "Data Source=" & Server. Map. Path( "Sample. mdb" ) Conn = New Ole. Db. Connection( Provider & "; " & Data. Base ) Conn. Open() On Error Resume Next Cmd = New Ole. Db. Command( SQL. Text, Conn ) Cmd. Execute. Non. Query() If Err. Number <> 0 Then Msg. Text = Err. Description Else Msg. Text = "成功!" End If Conn. Close() End Sub </script>

Insert. aspx Part I %@ Import Namespace="System. Data" %> <%@ Import Namespace="System. Data. Ole.

Insert. aspx Part I %@ Import Namespace="System. Data" %> <%@ Import Namespace="System. Data. Ole. Db" %> <Html> <Body Bg. Color="White"> <H 3>Insert. aspx -- 新增資料到「成績單」資料表<HR></H 3> <Form runat="server"> <Blockquote> 學號: <asp: Text. Box runat="server" id="學號" /> 姓名: <asp: Text. Box runat="server" id="姓名" /> 國文: <asp: Text. Box runat="server" id="國文" /> 英文: <asp: Text. Box runat="server" id="英文" /> 數學: <asp: Text. Box runat="server" id="數學" /><p>

Insert. aspx Part II <asp: Button runat="server" Text="新增" On. Click="Insert. Data" /> </Blockquote> <HR><asp:

Insert. aspx Part II <asp: Button runat="server" Text="新增" On. Click="Insert. Data" /> </Blockquote> <HR><asp: Label runat="server" id="Msg" Fore. Color="Red" /> </Form> </Body> </Html> <script Language="VB" runat="server"> Sub Insert. Data(sender As Object, e As Event. Args) Dim Conn As Ole. Db. Connection Dim Cmd As Ole. Db. Command Dim Provider = "Provider=Microsoft. Jet. OLEDB. 4. 0" Dim Database = "Data Source=" & Server. Map. Path( "Sample. mdb" ) Conn = New Ole. Db. Connection( Provider & "; " & Data. Base )

Insert. aspx Part III Dim SQL As String SQL = "Insert Into 成績單 (學號,

Insert. aspx Part III Dim SQL As String SQL = "Insert Into 成績單 (學號, 姓名, 國文, 英文, 數學 ) Values( ? , ? , ? )" Cmd = New Ole. Db. Command( SQL, Conn ) Cmd. Parameters. Add( New Ole. Db. Parameter("學號", Ole. Db. Type. Integer)) Cmd. Parameters. Add( New Ole. Db. Parameter("姓名", Ole. Db. Type. Char, 10)) Cmd. Parameters. Add( New Ole. Db. Parameter("國文", Ole. Db. Type. Small. Int)) Cmd. Parameters. Add( New Ole. Db. Parameter("英文", Ole. Db. Type. Small. Int)) Cmd. Parameters. Add( New Ole. Db. Parameter(""數學", Ole. Db. Type. Small. Int))

Insert. aspx Part IV Cmd. Parameters("學號"). Value Cmd. Parameters("姓名"). Value Cmd. Parameters("國文"). Value Cmd.

Insert. aspx Part IV Cmd. Parameters("學號"). Value Cmd. Parameters("姓名"). Value Cmd. Parameters("國文"). Value Cmd. Parameters("英文"). Value Cmd. Parameters("數學"). Value Cmd. Execute. Non. Query() 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() 敘述 (2) n 為了建立 5 個參數並且將其資料型別對 應到上表的型別,所撰寫的敘述如下: Cmd. Parameters. Add(

設定參數型別的 Cmd. Parameters. Add() 敘述 (2) n 為了建立 5 個參數並且將其資料型別對 應到上表的型別,所撰寫的敘述如下: Cmd. Parameters. Add( New Ole. Db. Parameter("學號", Ole. Db. Type. Integer)) Cmd. Parameters. Add( New Ole. Db. Parameter("姓名", Ole. Db. Type. Char, 10)) Cmd. Parameters. Add( New Ole. Db. Parameter("國文", Ole. Db. Type. Small. Int)) Cmd. Parameters. Add( New Ole. Db. Parameter("英文", Ole. Db. Type. Small. Int)) Cmd. Parameters. Add( New Ole. Db. Parameter("數學", Ole. Db. Type. Small. Int))

設定參數值的 Cmd. Parameters(). Value 敘述 Cmd. Parameters("學號"). Value = Val(學號. Text) Cmd. Parameters("姓名"). Value

設定參數值的 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) n Text. Box 所讀取的內容為字串資料,要呼 叫 Val 函數將其轉換成數值資料。

含有參數的 SQL 指令 -- 具名的參數 Cmd. Parameters. Add( New Ole. Db. Parameter("@學號", Ole. Db.

含有參數的 SQL 指令 -- 具名的參數 Cmd. Parameters. Add( New Ole. Db. Parameter("@學號", Ole. Db. Type. Integer)) Cmd. Parameters. Add( New Ole. Db. Parameter("@姓名", Ole. Db. Type. Char, 10)) Cmd. Parameters. Add( New Ole. Db. Parameter("@國文", Ole. Db. Type. Small. Int)) Cmd. Parameters. Add( New Ole. Db. Parameter("@英文", Ole. Db. Type. Small. Int)) Cmd. Parameters. Add( New Ole. Db. Parameter("@數學", Ole. Db. Type. Small. Int)) Cmd. Parameters("@學號"). Value = Val(學號. Text) Cmd. Parameters("@姓名"). Value = 姓名. Text Cmd. Parameters("@國文"). Value = Val(國文. Text) Cmd. Parameters("@英文"). Value = Val(英文. Text) Cmd. Parameters("@數學"). Value = Val(數學. Text)

Update 01. aspx -- 基本架構

Update 01. aspx -- 基本架構

Data. Grid 的安插 <asp: Data. Grid runat="server" id="My. Grid" Header. Style-Back. Color="#AAAADD" Alternating. Item.

Data. Grid 的安插 <asp: Data. Grid runat="server" id="My. Grid" Header. Style-Back. Color="#AAAADD" Alternating. Item. Style-Back. Color="#FFFFC 0" Border. Color="Black" Cell. Padding="2" Cell. Spacing="0" On. Edit. Command="Edit. Data" On. Update. Command="Update. Data" On. Cancel. Command="Cancel. Edit" Data. Key. Field="學號"> <Columns> <asp: Edit. Command. Column Header. Text="修改" Item. Style-Wrap="False" Edit. Text="編輯" Update. Text="更新" Cancel. Text="取消" /> </Columns> </asp: Data. Grid>

Edit. Data 事件程序的撰寫 Sub Edit. Data(sender As Object, e As Data. Grid. Command. Event.

Edit. Data 事件程序的撰寫 Sub Edit. Data(sender As Object, e As Data. Grid. Command. Event. Args) My. Grid. Edit. Item. Index = e. Item. Index Open. Data. Base_And_Bind. To. Data. Grid() End Sub

Cancel. Edit 事件程序的撰寫 Sub Cancel. Edit(sender As Object, e As Data. Grid. Command. Event.

Cancel. Edit 事件程序的撰寫 Sub Cancel. Edit(sender As Object, e As Data. Grid. Command. Event. Args) My. Grid. Edit. Item. Index = -1 Open. Data. Base_And_Bind. To. Data. Grid() End Sub

Update. Data 事件程序的撰寫 Part I Sub Update. Data(sender As Object, e As Data. Grid.

Update. Data 事件程序的撰寫 Part I Sub Update. Data(sender As Object, e As Data. Grid. Command. Event. Args) Dim TB(5) As Text. Box TB(1) = e. Item. Cells(1). Controls(0) TB(2) = e. Item. Cells(2). Controls(0) TB(3) = e. Item. Cells(3). Controls(0) TB(4) = e. Item. Cells(4). Controls(0) TB(5) = e. Item. Cells(5). Controls(0) ' ' ' 學號 姓名 國文 英文 數學 Text. Box

Update. Data 事件程序的撰寫 Part II Dim Msg As String = "欲更新的資料: <blockquote>" Msg &=

Update. Data 事件程序的撰寫 Part II Dim Msg As String = "欲更新的資料: <blockquote>" Msg &= "Data. Key = " & My. Grid. Data. Keys(e. Item. Index) Msg &= " Cell(1) = " & TB(1). Text Msg &= " Cell(2) = " & TB(2). Text Msg &= " Cell(3) = " & TB(3). Text Msg &= " Cell(4) = " & TB(4). Text Msg &= " Cell(5) = " & TB(5). Text & "</blockquote>" Response. Write( Msg ) My. Grid. Edit. Item. Index = -1 Open. Data. Base_And_Bind. To. Data. Grid() End Sub

程式最重要的關鍵(2) Dim TB(5) As Text. Box ' 宣告Text. Box ' 將Data. Grid中的Text. Box指定給我們宣告的Text. Box

程式最重要的關鍵(2) Dim TB(5) As Text. Box ' 宣告Text. Box ' 將Data. Grid中的Text. Box指定給我們宣告的Text. Box TB(1) = e. Item. Cells(1). Controls(0) ' 學號 Text. Box TB(2) = e. Item. Cells(2). Controls(0) ' 姓名 Text. Box TB(3) = e. Item. Cells(3). Controls(0) ' 國文 Text. Box TB(4) = e. Item. Cells(4). Controls(0) ' 英文 Text. Box TB(5) = e. Item. Cells(5). Controls(0) ' 數學 Text. Box ' 接下來分別利用TB(1). Text、TB(2). Text、TB(3). Text、TB(4). Text ' 及TB(5). Text讀取Data. Grid等5個Text. Box的內容。

Update 02. aspx – 將修改的資料寫入資料庫 Part I #01 Sub Update. Data(sender As Object, e

Update 02. aspx – 將修改的資料寫入資料庫 Part I #01 Sub Update. Data(sender As Object, e As Data. Grid. Command. Event. Args) #02 Dim Conn As Ole. Db. Connection #03 Dim Cmd As Ole. Db. Command #04 Dim SQL As String #05 #06 Dim Provider = "Provider=Microsoft. Jet. OLEDB. 4. 0" #07 Dim Database = "Data Source=" & Server. Map. Path( "Sample. mdb" ) #08 Conn = New Ole. Db. Connection( Provider & "; " & Data. Base ) #09 Conn. Open() #10 #11 SQL = "Update 成績單 Set 學號=@學號, 姓名=@姓名, 國文=@國文, 英文=@英 文, 數學=@數學 Where 學號=@Key" #12 #13 Cmd = New Ole. Db. Command( SQL, Conn )

Update 02. aspx – 將修改的資料寫入資料庫 Part II #14 Cmd. Parameters. Add( New Ole. Db.

Update 02. aspx – 將修改的資料寫入資料庫 Part II #14 Cmd. Parameters. Add( New Ole. Db. Parameter("@學號", Ole. Db. Type. Integer)) #15 Cmd. Parameters. Add( New Ole. Db. Parameter("@姓名", Ole. Db. Type. Char, 10)) #16 Cmd. Parameters. Add( New Ole. Db. Parameter("@國文", Ole. Db. Type. Small. Int)) #17 Cmd. Parameters. Add( New Ole. Db. Parameter("@英文", Ole. Db. Type. Small. Int)) #18 Cmd. Parameters. Add( New Ole. Db. Parameter("@數學", Ole. Db. Type. Small. Int)) #19 Cmd. Parameters. Add( New Ole. Db. Parameter("@Key", Ole. Db. Type. Integer)) #20 #21 Dim TB(5) As Text. Box #22 TB(1) = e. Item. Cells(1). Controls(0) ' 學號 Text. Box #23 TB(2) = e. Item. Cells(2). Controls(0) ' 姓名 Text. Box #24 TB(3) = e. Item. Cells(3). Controls(0) ' 國文 Text. Box #25 TB(4) = e. Item. Cells(4). Controls(0) ' 英文 Text. Box #26 TB(5) = e. Item. Cells(5). Controls(0) ' 數學 Text. Box #27

Update 02. aspx – 將修改的資料寫入資料庫 Part III #28 Cmd. Parameters("@Key"). Value = My. Grid.

Update 02. aspx – 將修改的資料寫入資料庫 Part III #28 Cmd. Parameters("@Key"). Value = My. Grid. Data. Keys(e. Item. Index) #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. Execute. Non. Query() #35 #36 Conn. Close() #37 #38 My. Grid. Edit. Item. Index = -1 #39 Open. Data. Base_And_Bind. To. Data. Grid() #40 End Sub

Data. Grid 所做的修改 Part I <asp: Data. Grid runat="server" id="My. Grid" Header. Style-Back. Color="#AAAADD"

Data. Grid 所做的修改 Part I <asp: Data. Grid runat="server" id="My. Grid" Header. Style-Back. Color="#AAAADD" Alternating. Item. Style-Back. Color="#FFFFC 0" Border. Color="Black" Cell. Padding="2" Cell. Spacing="0" On. Edit. Command="Edit. Data" On. Update. Command="Update. Data" On. Cancel. Command="Cancel. Edit" Data. Key. Field="學號" Auto. Generate. Columns="False" >

Data. Grid 所做的修改 Part II <Columns> <asp: Edit. Command. Column Header. Text="修改" Item. Style-Wrap="False"

Data. Grid 所做的修改 Part II <Columns> <asp: Edit. Command. Column Header. Text="修改" Item. Style-Wrap="False" Edit. Text="編輯" Update. Text="更新" Cancel. Text="取消" /> <asp: Bound. Column Data. Field="學號" Header. Text="學號" Read. Only="True" /> <asp: Bound. Column Data. Field="姓名" Header. Text="姓名"/> <asp: Bound. Column Data. Field="國文" Header. Text="國文"/> <asp: Bound. Column Data. Field="英文" Header. Text="英文"/> <asp: Bound. Column Data. Field="數學" Header. Text="數學"/> </Columns> </asp: Data. Grid>

程式所做的修改 n 更新資料的SQL指令: SQL = "Update 成績單 Set 姓名=@姓名, 國文=@國 文, 英文=@英文, 數學=@數學 Where

程式所做的修改 n 更新資料的SQL指令: SQL = "Update 成績單 Set 姓名=@姓名, 國文=@國 文, 英文=@英文, 數學=@數學 Where 學號=@Key" n 「@學號」參數的相關敘述均需刪除: Cmd. Parameters. Add( New Ole. Db. Parameter("@學號", Ole. Db. Type. Integer)) TB(1) = e. Item. Cells(1). Controls(0) ' 學號 Text. Box Cmd. Parameters("@學號"). Value = Val(TB(1). Text)

Update 04. aspx – 設定 List. Box 及 Check. Box 欄位

Update 04. aspx – 設定 List. Box 及 Check. Box 欄位

Data. Grid 的安插 Edit. Command. Column 欄位。 n Bound. Column 欄位 。 n Hyper.

Data. Grid 的安插 Edit. Command. Column 欄位。 n Bound. Column 欄位 。 n Hyper. Link. Column 欄位。 n Template. Column 欄位。 n

Template. Column 欄位 安插之語法及結構 (1) <asp: Template. Column Header. Text="抬頭"> <Item. Template> 在此安插顯示資料的Server控制元件 </Item.

Template. Column 欄位 安插之語法及結構 (1) <asp: Template. Column Header. Text="抬頭"> <Item. Template> 在此安插顯示資料的Server控制元件 </Item. Template> <Edit. Item. Template> 在此安插編輯資料的Server控制元件 </asp: Template. Column>

Template. Column 欄位 安插之語法及結構 (2) n 例如: <asp: Template. Column Header. Text="姓名"> <Item. Template>

Template. Column 欄位 安插之語法及結構 (2) n 例如: <asp: Template. Column Header. Text="姓名"> <Item. Template> <asp: Label runat="server" Text='<%# Container. Data. Item("姓名") %>'/> </Item. Template> <Edit. Item. Template> <asp: Text. Box runat="server" id="姓名" Size=10 Text='<%# Container. Data. Item("姓名") %>'/> </Edit. Item. Template> </asp: Template. Column>

Update 04. aspx 網頁的欄位(2) 已婚欄位 <asp: Template. Column Header. Text="已婚"> <Item. Template> <asp: Label

Update 04. aspx 網頁的欄位(2) 已婚欄位 <asp: Template. Column Header. Text="已婚"> <Item. Template> <asp: Label runat="server" Text='<%# Container. Data. Item("已婚") %>'/> </Item. Template> <Edit. Item. Template> <asp: Check. Box runat="server" id="已婚" Checked='<%# Container. Data. Item("已婚") %>'/> </Edit. Item. Template> </asp: Template. Column>

Update 04. aspx 網頁的欄位(3) 性別欄位 <Edit. Item. Template> <asp: List. Box runat="server" id="性別" Rows=1

Update 04. aspx 網頁的欄位(3) 性別欄位 <Edit. Item. Template> <asp: List. Box runat="server" id="性別" Rows=1 Selected. Index='<%# 性別編號(Container. Data. Item("性別")) %>'> <asp: List. Item>男</asp: List. Item> <asp: List. Item>女</asp: List. Item> </asp: List. Box> </Edit. Item. Template>

Update 04. aspx 網頁的欄位(4) 血型欄位 <Edit. Item. Template> <asp: List. Box runat="server" id="血型" Rows=1

Update 04. aspx 網頁的欄位(4) 血型欄位 <Edit. Item. Template> <asp: List. Box runat="server" id="血型" Rows=1 Selected. Index='<%# 血型編號(Container. Data. Item("血型")) %>'> <asp: List. Item>A</asp: List. Item> <asp: List. Item>B</asp: List. Item> <asp: List. Item>O</asp: List. Item> <asp: List. Item>AB</asp: List. Item> </asp: List. Box> </Edit. Item. Template>

Update 04. aspx 網頁的欄位(5) Function 血型編號( 血型 As String ) As Integer If 血型

Update 04. 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

Update 04. aspx 網頁的欄位(6) Function 性別編號( 性別 As String ) As Integer If 性別

Update 04. aspx 網頁的欄位(6) Function 性別編號( 性別 As String ) As Integer If 性別 = "男" Then Return 0 If 性別 = "女" Then Return 1 End Function

Update 04. aspx 資料的更新(1) Dim Text姓名 As Text. Box Text姓名 = e. Item. Find.

Update 04. aspx 資料的更新(1) Dim Text姓名 As Text. Box Text姓名 = e. Item. Find. Control("姓名") Cmd. Parameters("@姓名"). Value = Text姓名. Text Dim List性別 As List. Box List性別 = e. Item. Find. Control("性別") Cmd. Parameters("@性別"). Value = List性別. Selected. Item. Text

Update 04. aspx 資料的更新 (2) Dim List血型 As List. Box List血型 = e. Item.

Update 04. aspx 資料的更新 (2) Dim List血型 As List. Box List血型 = e. Item. Find. Control("血型") Cmd. Parameters("@血型"). Value = List血型. Selected. Item. Text Dim Check已婚 As Check. Box Check已婚 = e. Item. Find. Control("已婚") Cmd. Parameters("@已婚"). Value = Check已婚. Checked

10 -3 Data. Set 物件與 XML 的讀寫

10 -3 Data. Set 物件與 XML 的讀寫

XML 格式的資料庫檔案 (1) n Score. xml Part I <? xml version="1. 0" standalone="yes"? >

XML 格式的資料庫檔案 (1) n Score. xml Part I <? xml version="1. 0" standalone="yes"? > <New. Data. Set> <xs: schema id="New. Data. Set" xmlns="" xmlns: xs="http: //www. w 3. org/2001/XMLSchema" xmlns: msdata="urn: schemas-microsoft-com: xml-msdata"> <xs: element name="New. Data. Set" msdata: Is. Data. Set="true" msdata: Locale="zh-TW"> <xs: complex. Type> <xs: choice max. Occurs="unbounded"> <xs: element name="成績單"> <xs: complex. Type> <xs: sequence>

XML 格式的資料庫檔案 (2) n Score. xml Part II <xs: element name="學號" type="xs: int" min.

XML 格式的資料庫檔案 (2) n Score. xml Part II <xs: element name="學號" type="xs: int" min. Occurs="0" /> <xs: element name="姓名" type="xs: string" min. Occurs="0" /> <xs: element name="國文" type="xs: short" min. Occurs="0" /> <xs: element name="英文" type="xs: short" min. Occurs="0" /> <xs: element name="數學" type="xs: short" min. Occurs="0" /> </xs: sequence> </xs: complex. Type> </xs: element> </xs: choice> </xs: complex. Type> </xs: element> </xs: schema>

Xml. Read. aspx Part I <%@ Import Namespace="System. Data" %> <script Language="VB" runat="server"> Sub

Xml. Read. aspx Part I <%@ Import Namespace="System. Data" %> <script Language="VB" runat="server"> Sub Page_Load(sender As Object, e As Event. Args) Dim Ds As New Data. Set Ds. Read. Xml( Server. Map. Path("Score. xml") ) My. Grid. Data. Source = Ds. Tables( "成績單" ). Default. View My. Grid. Data. Bind() End Sub </script>

Xml. Read. aspx Part II <Html> <Body Bg. Color="White"> <H 3>Xml. Read. aspx --

Xml. Read. aspx Part II <Html> <Body Bg. Color="White"> <H 3>Xml. Read. aspx -- 讀取 Score. xml 成為「成績單」資料表 <HR></H 3> <Center> <Form runat="server"> <asp: Data. Grid runat="server" id="My. Grid" Header. Style-Back. Color="#AAAADD" Alternating. Item. Style-Back. Color="#FFFFC 0" Border. Color="Black" Cell. Padding="2" Cell. Spacing="0" /> </Form> <p></Center> <HR></Body> </Html>

Xml. Write. aspx Part I <%@ Import Namespace="System. Data" %> <%@ Import Namespace="System. Data.

Xml. Write. aspx Part I <%@ Import Namespace="System. Data" %> <%@ Import Namespace="System. Data. Ole. Db" %> <script Language="VB" runat="server"> Sub Page_Load(sender As Object, e As Event. Args) Dim Conn As Ole. Db. Connection Dim Adpt As Ole. Db. Data. Adapter Dim Ds As Data. Set Dim Provider = "Provider=Microsoft. Jet. OLEDB. 4. 0" Dim Database = "Data Source=" & Server. Map. Path( "Sample. mdb" ) Conn = New Ole. Db. Connection( Provider & "; " & Data. Base ) Conn. Open()

PXml. Write. aspx Part II Dim SQL = "Select * From 成績單" Adpt =

PXml. Write. aspx Part II Dim SQL = "Select * From 成績單" Adpt = New Ole. Db. Data. Adapter( SQL, Conn ) Ds = New Dataset() Adpt. Fill(Ds, "成績單") ' 將 Data. Set 物件的內容寫到 Score 2. xml 檔案 Ds. Write. Xml( Server. Map. Path("Score 2. xml"), Xml. Write. Mode. Write. Schema ) Conn. Close() Response. Write( "已經將「成績單」資料表寫到 Score 2. xml!" ) End Sub </script>

10 -4 存取 SQL Server 資料庫

10 -4 存取 SQL Server 資料庫

轉換提供的資料表 -- 情況一 n 有安裝附屬於. NET Framework SDK 的 SQL Server Desktop Engine。

轉換提供的資料表 -- 情況一 n 有安裝附屬於. NET Framework SDK 的 SQL Server Desktop Engine。

轉換提供的資料表--情況二 (1) n 安裝有完整的 SQL Server: 開啟 ch 10/Sql 目錄,然後逐一修改每 一個. aspx 檔案的以下敘述: Conn

轉換提供的資料表--情況二 (1) n 安裝有完整的 SQL Server: 開啟 ch 10/Sql 目錄,然後逐一修改每 一個. aspx 檔案的以下敘述: Conn = New Sql. Connection("server=(local)Net. SDK; " & _ "database=pubs; " & _ "Trusted_Connection=Yes")

從 Ole. Db 到 Sql. Client (1) n 將 <%@ Import Namespace=“System. Data. Ole.

從 Ole. Db 到 Sql. Client (1) n 將 <%@ Import Namespace=“System. Data. Ole. Db” %> 標示改成: <%@ Import Namespace=“System. Data. Sql. Client” %>。 也就是說,我們原本使用 Ole. Db 命名空間的物件, 現在要改成用 Sql. Client 命名空間的物件。

從 Ole. Db 到 Sql. Client (2) n 將所有以 Ole. Db 開頭的物件,包含: Ole. Db.

從 Ole. Db 到 Sql. Client (2) n 將所有以 Ole. Db 開頭的物件,包含: Ole. Db. Connection Ole. Db. Command Ole. Db. Data. Reader Ole. Db. Data. Adapter … n 改成以 Sql 開頭的物件,如下: Sql. Connection Sql. Command Sql. Data. Reader Sql. Data. Adapter …

從 Ole. Db 到 Sql. Client (3) n 將建立 Connection 物件的敘述: Dim Provider =

從 Ole. Db 到 Sql. Client (3) n 將建立 Connection 物件的敘述: Dim Provider = "Provider=Microsoft. Jet. OLEDB. 4. 0" Dim Database = "Data Source=" & Server. Map. Path( "Sample. mdb" ) Conn = New Ole. Db. Connection( Provider & "; " & Data. Base ) Conn. Open() n 改成: Conn = New Sql. Connection("server=(local)Net. SDK; " & _ "database=pubs; " & _ "Trusted_Connection=Yes") Conn. Open

Route 01. aspx Part I <%@ Import Namespace="System. Data" %> <%@ Import Namespace="System. Data.

Route 01. aspx Part I <%@ Import Namespace="System. Data" %> <%@ Import Namespace="System. Data. Sql. Client" %> <script Language="VB" runat="server"> Sub Page_Load(sender As Object, e As Event. Args) ' 相關物件的宣告 Dim Conn As Sql. Connection ' 宣告一個 Connection 物件 Dim Adpt As Sql. Data. Adapter ' 宣告一個 Data. Adapter 物件 Dim Ds As Data. Set ' 宣告一個 Data. Set 物件 ' Connection 物件開啟 Sql Server, pub 資料庫 Conn = New Sql. Connection("server=(local)Net. SDK; " & _ "database=pubs; " & _ "Trusted_Connection=Yes") Conn. Open()

Route 01. aspx Part II ' Data. Adapter 物件開啟「成績單」資料表 Dim SQL = "Select *

Route 01. aspx Part II ' Data. Adapter 物件開啟「成績單」資料表 Dim SQL = "Select * From 成績單" Adpt = New Sql. Data. Adapter( SQL, Conn ) ' 將 Data. Adapter 物件所開啟的「成績單」資料表存放於 Data. Set 物件中 Ds = New Dataset() Adpt. Fill(Ds, "成績單") ' Data. Grid控制元件顯示「成績單」資料表 My. Grid. Data. Source = Ds. Tables( "成績單" ). Default. View My. Grid. Data. Bind() ' 關閉Sample. mdb資料庫 Conn. Close() End Sub </script>

Route 01. aspx Part III <Html> <Body Bg. Color="White"> <H 3>資料庫存取路徑一: Connection - Data.

Route 01. aspx Part III <Html> <Body Bg. Color="White"> <H 3>資料庫存取路徑一: Connection - Data. Adapter - Data. Set - Data. Grid <HR></H 3> <Center> <Form runat="server"> <asp: Data. Grid runat="server" id="My. Grid" Header. Style-Back. Color="#AAAADD" Alternating. Item. Style-Back. Color="#FFFFC 0" Border. Color="Black" Cell. Padding="2" Cell. Spacing="0" /> </Form> <p></Center> <HR></Body> </Html>

含有參數的 SQL 指令 Cmd. Parameters. Add( New Ole. Db. Parameter("@學號", Ole. Db. Type. Integer))

含有參數的 SQL 指令 Cmd. Parameters. Add( New Ole. Db. Parameter("@學號", Ole. Db. Type. Integer)) Cmd. Parameters. Add( New Ole. Db. Parameter("@姓名", Ole. Db. Type. Char, 10)) Cmd. Parameters. Add( New Ole. Db. Parameter("@國文", Ole. Db. Type. Small. Int)) Cmd. Parameters. Add( New Ole. Db. Parameter("@英文", Ole. Db. Type. Small. Int)) Cmd. Parameters. Add( New Ole. Db. Parameter("@數學", Ole. Db. Type. Small. Int)) n 改成 Cmd. Parameters. Add( New Sql. Parameter("@學號", Sql. Db. Type. Integer)) Cmd. Parameters. Add( New Sql. Parameter("@姓名", Sql. Db. Type. Char, 10)) Cmd. Parameters. Add( New Sql. Parameter("@國文", Sql. Db. Type. Small. Int)) Cmd. Parameters. Add( New Sql. Parameter("@英文", Sql. Db. Type. Small. Int)) Cmd. Parameters. Add( New Sql. Parameter("@數學", Sql. Db. Type. Small. Int))

欄位填入空白的問題 Function 血型編號( 血型 As String ) As Integer If Trim(血型) = "A" Then

欄位填入空白的問題 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