主题:asp.net学习日志(2)(把SQL Server中数据表的内容显示在网页上)
[em16]
今天通过了使用数据读取器读取数据的代码
<%@ Page Language="VB" debug="true" %>
<%@import Namespace="System.Data" %>
<%@import Namespace ="System.Data.SQLClient" %>
<html >
<head runat="server">
<title>datareader</title>
</head>
<script runat ="server" >
Sub page_load(ByVal sender As Object, ByVal e As EventArgs)
Dim myconnection As New SqlConnection
myconnection = New SqlConnection("server =(local);uid=sa;pwd=;database=student")
Dim mycommand As New SqlCommand
mycommand = New SqlCommand("select * from student", myconnection)
myconnection.Open()
Dim dr As SqlDataReader = mycommand.ExecuteReader
mydatagrid.DataSource = dr
mydatagrid.DataBind()
myconnection.Close()
End Sub</script>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="mydatagrid" runat="server" BackColor="Yellow" BorderColor="Black" CellPadding="3" EnableViewState="False" Font-Names="宋体" Font-Size="Larger" Width="700px">
<HeaderStyle BorderColor="#AAAADD" />
</asp:GridView>
</div>
</form>
</body>
</html>
困难没什么,就是要注意myconnection = New SqlConnection("server =(local);uid=sa;pwd=;database=student")中,服务器的密码、数据库要跟自己的相符。
看我的成果:
[img]http://117.img.pp.sohu.com/images/2007/5/29/7/11/1136d6420d9.jpg[/img]
今天通过了使用数据读取器读取数据的代码
<%@ Page Language="VB" debug="true" %>
<%@import Namespace="System.Data" %>
<%@import Namespace ="System.Data.SQLClient" %>
<html >
<head runat="server">
<title>datareader</title>
</head>
<script runat ="server" >
Sub page_load(ByVal sender As Object, ByVal e As EventArgs)
Dim myconnection As New SqlConnection
myconnection = New SqlConnection("server =(local);uid=sa;pwd=;database=student")
Dim mycommand As New SqlCommand
mycommand = New SqlCommand("select * from student", myconnection)
myconnection.Open()
Dim dr As SqlDataReader = mycommand.ExecuteReader
mydatagrid.DataSource = dr
mydatagrid.DataBind()
myconnection.Close()
End Sub</script>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="mydatagrid" runat="server" BackColor="Yellow" BorderColor="Black" CellPadding="3" EnableViewState="False" Font-Names="宋体" Font-Size="Larger" Width="700px">
<HeaderStyle BorderColor="#AAAADD" />
</asp:GridView>
</div>
</form>
</body>
</html>
困难没什么,就是要注意myconnection = New SqlConnection("server =(local);uid=sa;pwd=;database=student")中,服务器的密码、数据库要跟自己的相符。
看我的成果:
[img]http://117.img.pp.sohu.com/images/2007/5/29/7/11/1136d6420d9.jpg[/img]