当前位置:编程学习 > C#/ASP.NET >>

求VB ASP.NET高手解答apx文件代码语句详细解释

第一个

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Page Language="VB" Debug="true" %>

<html>
 
<script language="VB" runat="server">

    Sub Page_Load(Sender As Object, E As EventArgs)

        Dim DS As DataSet
        'Dim MyConnection As SqlConnection
        Dim MyCommand As SqlDataAdapter

        'MyConnection = New SqlConnection("server=(local)\NetSDK;database=pubs;Trusted_Connection=yes")
       Dim MyConnection As SqlConnection = New SqlConnection("server=localhost;uid=sa;password=;database=pubs")
        MyCommand = New SqlDataAdapter("select * from Authors", MyConnection)

        ds = new DataSet()
        MyCommand.Fill(ds,"authors")

        MyDataGrid.DataSource=ds.Tables("authors").DefaultView
        MyDataGrid.DataBind()
    End Sub

</script>

<body>

  <h3><font face="宋体">DataGrid 控件的简单选择</font></h3>

  <ASP:DataGrid id="MyDataGrid" runat="server"
    Width="700"
    BackColor="#ccccff"
    BorderColor="black"
    ShowFooter="false"
    CellPadding=3
    CellSpacing="0"
    Font-Name="宋体"
    Font-Size="8pt"
    HeaderStyle-BackColor="#aaaadd"
    EnableViewState="false"
  />

</body>
</html>
第二个

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

<html>
 

<script language="VB" runat="server">

    Dim MyConnection As SqlConnection

    Sub Page_Load(Sender As Object, E As EventArgs)

    MyConnection=New SqlConnection("server=localhost;uid=sa;password=;database=pubs")
        If Not (IsPostBack)
            BindGrid()
        End If
    End Sub

    Sub MyDataGrid_Delete(Sender As Object, E As DataGridCommandEventArgs)

        Dim MyCommand As SqlCommand
        Dim DeleteCmd As String = "DELETE from Employee where emp_id = @Id"

        MyCommand = New SqlCommand(DeleteCmd, MyConnection)
        MyCommand.Parameters.Add(New SqlParameter("@Id", SqlDbType.NVarChar, 11))
        MyCommand.Parameters("@Id").Value = MyDataGrid.DataKeys(CInt(E.Item.ItemIndex))

        MyCommand.Connection.Open()

        Try
            MyCommand.ExecuteNonQuery()
            Message.InnerHtml = "<b>已删除记录</b><br>" & DeleteCmd
        Catch Exc As SQLException
            Message.InnerHtml = "错误:未能删除记录"
            Message.Style("颜色") = "红色"
        End Try

        MyCommand.Connection.Close()

        BindGrid()
    End Sub

    Sub BindGrid()

        Dim DS As DataSet
        Dim MyCommand As SqlDataAdapter
        MyCommand = New SqlDataAdapter("select * from Employee", MyConnection)

        DS = new DataSet()
        MyCommand.Fill(DS, "员工")

        MyDataGrid.DataSource=DS.Tables("员工").DefaultView
        MyDataGrid.DataBind()
    End Sub

</script>

<body style="font: 10.5pt 宋体">

  <form runat="server">

    <h3><font face="宋体">删除数据行</font></h3>

    <span id="Message" EnableViewState="false" style="font: arial 11pt;" runat="server"/>

    <ASP:DataGrid id="MyDataGrid" runat="server"
      Width="800"
      BackColor="#ccccff"
      BorderColor="black"
      ShowFooter="false"
      CellPadding=3
      CellSpacing="0"
      Font-Name="Verdana"
      Font-Size="8pt"
      HeaderStyle-BackColor="#aaaadd"
      DataKeyField="emp_id"
      OnDeleteCommand="MyDataGrid_Delete"
    >

      <Columns>
         <asp:ButtonColumn Text="删除员工" CommandName="Delete"/>
      </Columns>

    </ASP:DataGrid>

  </form>

</body>
</html>
第三个

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

<html>
 

<script language="VB" runat="server">

    Sub Page_Load(Sender As Object, E As EventArgs)

        Dim DS As DataSet
        Dim MyConnection As SqlConnection
        Dim MyCommand As SqlDataAdapter
     MyConnection = New SqlConnection("server=localhost;uid=sa;password=;database=pubs")
     'MyConnection = New SqlConnection("server=localhost;uid=sa;password=123;database=pubs")

        MyCommand = New SqlDataAdapter("select * from Titles", MyConnection)

        DS = New DataSet()
        MyCommand.Fill(DS, "titles")

        MyRepeater.DataSource = DS.Tables("titles").DefaultView
        MyRepeater.DataBind()
    End Sub

</script>

<body topmargin="0" leftmargin="0" marginwidth="0" marginheight="0">

  <!-- #include virtual="header.inc" -->
<%'<!-- #include virtual="/quickstart/aspplus/samples/webforms/customize/header.inc" -->%>

  <ASP:Repeater id="MyRepeater" runat="server">

      <HeaderTemplate>

        <table width="100%" style="font: 9pt 宋体">
          <tr style="background-color:DFA894">
            <th>
              书名
            </th>
            <th>
              书名 ID
            </th>
            <th>
              类型
            </th>
            <th>
              出版商 ID
            </th>
            <th>
              价格
            </th>
          </tr>

      </HeaderTemplate>

      <ItemTemplate>

        <tr style="background-color:FFECD8">
          <td>
            <%# DataBinder.Eval(Container.DataItem, "title") %>
          </td>
          <td>
            <%# DataBinder.Eval(Container.DataItem, "title_id") %>
          </td>
          <td>
            <%# DataBinder.Eval(Container.DataItem, "type") %>
          </td>
          <td>
            <%# DataBinder.Eval(Container.DataItem, "pub_id") %>
          </td>
          <td>
            <%# DataBinder.Eval(Container.DataItem, "price", "$ {0}") %>
          </td>
        </tr>

      </ItemTemplate>

      <FooterTemplate>

        </table>

      </FooterTemplate>

  </ASP:Repeater>
   <!-- #include virtual="footer.inc" -->


  <%'<!-- #include virtual="/quickstart/aspplus/samples/webforms/customize/footer.inc" -->%>

</body>
</html>

答案:第一部分:

 'MyConnection = New SqlConnection("server=(local)\NetSDK;database=pubs;Trusted_Connection=yes")
这个是指定你链接的数据库;

MyCommand = New SqlDataAdapter("select * from Titles", MyConnection)

这个是执行语句;

MyCommand.Fill(DS, "titles");

把你取到的数据放到"DS";

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

绑定数据中的"title"字段的值

MyDataGrid.DataSource=ds.Tables("authors").DefaultView
MyDataGrid.DataBind()

控件数据绑定;

第二部分:

MyCommand.ExecuteNonQuery();
执行操作;

第三部分和第一部分雷同;

 

我建议你自己写个对数据库操作的类,不要每张页面都写那么多没有用的,冗余的...

上一个:哪个大哥能把这个代码写成ASP收信文件啊??
下一个:asp.net ,girview代码绑定两个表的代码?

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,