- 'Default.aspx.vb
- Imports System.Data
- Imports System.Data.SqlClient
- Partial Class _Default
- Inherits System.Web.UI.Page
- Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
- Dim DataSource As String = ""
- Dim Database As String = ""
- Dim Provider As String = "Data Source=" & DataSource & ";Initial Catalog=" & Database & ";Integrated Security=True"
- Try
- Dim Con = New SqlConnection(Provider)
- Con.Open()
- Dim Cmd = New SqlCommand("SELECT * FROM Employees", Con)
- Dim Reader = Cmd.ExecuteReader()
- While (Reader.Read())
- Response.Write("First Name: " & Reader("FirstName") & " " & "Surname: " & Reader("Surname") & "<br/>")
- End While
- Catch ex As Exception
- Response.Write(ex.Message)
- End Try
- End Sub
- End Class
VB.Net - Connect to SqlServer Database
This code snippet shows how to connect to an SqlServer database and read values using the SqlDataReader class.
No comments:
Post a Comment