Purpose :To retrieve data from the database using Data Reader and Data Commands. When this code is executed, the data in a database is displayed in two textbox controls.
Preparation:
Design a form using .NET environment and place two textbox controls on a form.
Design and create a table using SQL Server 2000.
Name the Database as FinAccounting.
Name the Table as AccountsTable.
Name the form as Form1
Name the controls on the form as Textbox1 and Textbox2.
Tasks:
1. Establish the connection with the Database using Connection object.
2. Execute the command.
3. The data will be read by the Datareader object and the contents of the first record is displayed in the textboxes.
Imports System.Data.SqlClient
Public Class Form1
Inherits System.Windows.Forms.Form
Dim str_sql_user_select As String = âSELECT * FROM AccountsTableâ
Dim str_connection As String = âData Source=VSDOTNET;Integrated Security=SSPI;Initial Catalog=FinAccountingâ
Dim mycon As SqlConnection
Dim comUserSelect As SqlCommand
Dim myreader As SqlDataReader
Private Sub Form1_Load(ByVal sender As Object, ByVal e As system.EventArgs) Handles
MyBase.Load
mycon = New SqlConnection(str_connection)
âInstantiate the commands
comUserSelect = New SqlCommand(str_sql_user_select, mycon)
TextBox1.Text = â â
TextBox2.Text = â â
mycon.Open()
myreader = comUserSelect.ExecuteReader
If (myreader.Read = True) Then
TextBox1.Text = myreader(0)
TextBox2.Text = myreader(1)
Else
MsgBox(âYou have reached eofâ)
End If
End Sub
End Class
Regards
Bhar
Books for programmers
http://www.vkinfotek.com