• 当前位置:首页>>ASP教程>>ASP数据库相关>>sp+语法介绍(六)----数据库篇
  • sp+语法介绍(六)----数据库篇
  • /*
    文章出处:http://www.ASPCool.com 转载请注明,谢谢!
    */列表,数据和数据棒定
    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>


    这个程序的运行例子在
    http://tutorial.superexpert.com/quickstart/ASPplus/samples/webforms/intro/intro8.ASPx

    [1]

  • 上一篇:结合asp和存储过程做的搜索程序
    下一篇:在ASP中用集合成批操作数据库(二)