'import namespaces 'without these some of the commands would not be recognized Imports System.Data.SqlClient Imports System.Data Imports System.Text Imports Microsoft.ApplicationBlocks.Data Imports System.Configuration.ConfigurationManager Partial Public Class DataReaderPage Inherits System.Web.UI.Page Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 'connectionString is defined in the web.config file - see below Dim ConnectionString As String = ConnectionStrings("myConnectionString").ConnectionString Dim con As New SqlConnection(ConnectionString) Dim sql As String = "select * from course" Dim cmd As New SqlCommand(sql, con) con.Open() Dim reader As SqlDataReader = cmd.ExecuteReader() Dim htmlStr As New StringBuilder("") Do While reader.Read() htmlStr.Append("<strong>") htmlStr.Append(reader("course_title")) htmlStr.Append("</strong><br />") Loop reader.Close() con.Close() 'this is a label defined on the aspx page htmlContent.Text = htmlStr.ToString End Sub End Class
<connectionStrings> <add name="PRIMarketingConnectionString" connectionString="Data Source=SERVERNAME;Initial Catalog=DBNAME;Persist Security Info=True;User ID=LOGIN;Password=PASSWORD" providerName="System.Data.SqlClient" /> </connectionStrings>