db_simple.aspx
<%@ Page Language="VB" EnableViewState="False" %>
<%@ Import Namespace = "System.Data" %>
<%@ Import Namespace = "System.Data.SQLClient" %>
<script language="VB" runat="server">
Sub Page_Load(sender As Object, e As EventArgs)
Dim myConnection As SqlConnection
Dim myCommand As SqlCommand
Dim myDataReader As SqlDataReader
' Create a new Connection object that connects to our SQL Server.
' It's running on a separate machine located at the IP address
' 10.2.2.133. The name of the database is "samples".
myConnection = New SqlConnection("Data Source=10.2.2.133;" _
& "Initial Catalog=samples;" _
& "User Id=samples;Password=password;")
' Create a new Command object that uses the Connection object we
' just created and selects all the records from our "scratch" table.
myCommand = New SqlCommand("SELECT * FROM scratch;", myConnection)
' Open the connection to the database
myConnection.Open
' Use the ExecuteReader method of the Command object to execute
' our query and return the results via a DataReader
myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
' Specify the DataReader as the source of the data for our DataGrid
' and then DataBind to display the data in our DataGrid.
SimpleDataGrid.DataSource = myDataReader
SimpleDataGrid.DataBind()
' Close our DataReader and Connection
myDataReader.Close()
myConnection.Close()
End Sub
</script>
<html>
<head>
<title>ASP.NET Simple Database Sample</title>
</head>
<body>
<form runat="server">
<asp:DataGrid id="SimpleDataGrid" runat="server" />
</form>
<hr />
<p>
Click <a href="http://www.asp101.com/samples/db_simple_aspx.asp">here</a>
to read about and download the source code.
</p>
</body>
</html>