程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> ASP編程 >> ASP入門教程 >> Asp+語法介紹(六)----數據庫篇

Asp+語法介紹(六)----數據庫篇

編輯:ASP入門教程
ASP+ 給我們提供了一套數據表格和數據列表的控件。這些控件可以幫助我們定制我們UI(user interFace 用戶界面)而不去考
慮一種數據庫或者其他的數據庫。例如:在下面的例子中,我們將要介紹一下<ASP:datagrid runat=server>控件是怎麼樣通過
sql 語句給我們提供數據的
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SQL" %>

<Html>
   <head>
      <link rel="stylesheet"href="intro.CSS">
   </head>

   <script language="VB" runat=server>

       Sub SubmitBtn_Click(Sender As Object, E As EventArgs)

          Dim DS As DataSet
          Dim MyConnection As SQLConnection
          Dim MyCommand As SQLDataSetCommand
          '以下是數據庫聯結
          MyConnection = New SQLConnection("server=localhost;uid=sa;pwd=;database=pubs")
          MyCommand = New SQLDataSetCommand("select * from Titles where type='" + 
Category.SelectedItem.Value + "'", myConnection)

          DS = new DataSet()
          MyCommand.FillDataSet(DS, "Titles")

          MyList.DataSource = DS.Tables("Titles").DefaultVIEw
          MyList.DataBind()

       End Sub


   </script>

   <body>

       <center>

       <form action="intro75.ASPx" method="post" runat="server">

           <ASP:adrotator AdvertisementFile="ads.XML" BorderColor="black" BorderWidth=1 runat="server"/>

           <h3> Name: <ASP:textbox id="Name" runat="server"/>

           Category:  <ASP:dropdownlist id="Category" runat=server>
                         <asp:listitem >psychology</ASP:listitem>
                         <asp:listitem >business</ASP:listitem>
                         <asp:listitem >popular_comp</ASP:listitem>
                      </ASP:dropdownlist>

           <ASP:button type=submit text="Lookup" OnClick="SubmitBtn_Click" runat="server"/>

           <p>

           <ASP:DataGrid id="MyList" HeaderStyle-BackColor="#aaaadd" BackColor="#ccccff" runat="server"/>

       </form>

       </center>

   </body>

</Html>
這個例子的運行示例在
http://tutorial.superexpert.com/quickstart/aspplus/samples/webforms/intro/intro75.ASPx

數據表格(data grid)控件 <ASP:datagrid runat=server> 
給我們提供了一種非常簡單的方法用傳統的UI截面去顯示數據查詢的結果.Asp+ 的開發者現在還可以通過<ASP:dataList 
runat=server>
來定制數據列表顯示來定制信息

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SQL" %>

<Html>
   <head>
      <link rel="stylesheet"href="intro.CSS">
   </head>

   <script language="VB" runat=server>

       Sub SubmitBtn_Click(Sender As Object, E As EventArgs)

          Dim DS As DataSet
          Dim MyConnection As SQLConnection
          Dim MyCommand As SQLDataSetCommand

          MyConnection = New SQLConnection("server=localhost;uid=sa;pwd=;database=pubs")
          MyCommand = New SQLDataSetCommand("select * from Titles where type='" + 
Category.SelectedItem.Value + "'", myConnection)

          DS = new DataSet()
          MyCommand.FillDataSet(DS, "Titles")

          MyList.DataSource = DS.Tables("Titles").DefaultVIEw
          MyList.DataBind()

       End Sub

   </script>

   <body>

       <center>

       <form action="intro8.ASPx" method="post" runat="server">

           <ASP:adrotator AdvertisementFile="ads.XML" BorderColor="black" BorderWidth=1 runat="server"/>

           <h3> Name: <ASP:textbox id="Name" runat="server"/>

           Category:  <ASP:dropdownlist id="Category" runat=server>
                         <asp:listitem >psychology</ASP:listitem>
                         <asp:listitem >business</ASP:listitem>
                         <asp:listitem >popular_comp</ASP:listitem>
                      </ASP:dropdownlist>

           <ASP:button type=submit text="Lookup" OnClick="SubmitBtn_Click" runat="server"/>

           <p>

           <ASP:datalist id="MyList" repeatcolumns="2" borderwidth="0" runat="server">

             <template name="itemtemplate">

                 <table>
                   <tr>

                     <td>
                         <img src='<%# DataBinder.Eval
(Container.DataItem, "title_id", "/quickstart/ASPplus/images/title-{0}.gif") %>'> 
                     </td>

                     <td width=250 valign=top>

                         <b><%# DataBinder.Eval(Container.DataItem, "title") %></b>

                         <br><br>

                         PRice: <%# DataBinder.Eval(Container.DataItem, "price", "${0}") %> 
                     </td>

                   </tr>
                 </table>

             </template>

           </ASP:datalist>

       </form>

       </center>

   </body>

</Html>
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved