Infolinks In Text Ads

Wednesday, 17 August 2011

Visual Basic .NET Database Programming

This simple program shows how to retrieve data from the FinAccounting database using DataReader and Data Commands. When the code in the program is executed, the data in the FinAccounting database is displayed in two textbox controls.








We use the SqlDataReader class to read data. The SqlDataReader class is defined in the System.Data.SqlClient namespace. So at the beginning of the program we need to use this class and we do so by adding the namespace. The Imports statement does this task.
Imports System.Data.SqlClient 

Public Class Form1 Inherits System.Windows.Forms.Form 

´Declare and setup the Connection string 

Dim str_connection As String = "Data Source=SYS1;Integrated Security=SSPI;
Initial Catalog=FinAccounting" 

´Declare and build a query string
Dim str_sql_user_select As String = "SELECT * FROM Accounts" 

Dim mycon As SqlConnection
Dim comUserSelect As SqlCommand 

´Declaring the DataReader object 

Dim myreader As SqlDataReader 

Private Sub Form1_Load(ByVal sender As Object,ByVal e As System.EventArgs)
Handles MyBase.Load 

´Instantiate the connection object
mycon = New SqlConnection(str_connection) 

´Instantiate the command object
comUserSelect = New SqlCommand(str_sql_user_select, mycon) 

TextBox1.Text = ""
TextBox2.Text = "" 

´Opening a Connection with the Open() method 

mycon.Open() 

´Creating a DataReader object by using
´the ExecuteReader() method of the Command object.  

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 

Creating a Structure and Accessing Data from a Structure in visual basic 2005

A structure is a user defined type, which is used to hold multiple types of related data. A Structure is a generalization of a user defined data type (UDT). In other words, we combine variables of different data types to create a structure. We use structures when we do not want the additional functionality of an extendable class and when the structure will not contain large amount of a data. Structures are a replacement of UDT in visual basic 6.0. Compared to UDTs structures are easier to implement. We can create structure, when we want a single variable to hold multiple types of related data. In visual basic 2005, we can create a structure using Structure and End Structure statements. The members of the structure are declared in between the Structure and End Structure statements. For example, we can maintain Stock details in a single variable, we can do it as follows: 

 Structure stock_details
 Public Inv_No as String
 Public Inv_Date as Date
 Public ItemName as String
 Public Qty as Integer
 End Structure 

 The variables declared inside a structure are called data members. The access modifiers that we can use with a structure and the Access Scope defined by each access modifier for the strtucture are listed below.
  1. Public
  2. A structure declared with the Public keyword is accessible from anywhere within or outside the application. This is the default access mode.
  3. Friend
  4. A structure declared with the Friend keyword s accessible from within the program that contains its declaration and from anywhere else in the same program.
  5. Private
  6. A structure declared with the Private keyword is accessible only from within its declaration context, including any nested procedures.
  7. Protected
  8. A structure declared with the Protected keyword is accessible only from within its own class or from a derived class.
  9. Protected Friend
  10. A structure declared with the Protected Friend keyword is accessible from within the same assembly and in the derived classes.
 We can also include procedures as members of a structure. For example, if you want to include a procedure to check the value entered for the stock variable, we can define the structure as shown below: 

Structure stock_details 

	Public Inv_No As String
	Public Inv_Date As Date
	Public ItemName As String
	Public Qty As Integer 

Public Sub CheckQty(BYVal Qty As Integer)
	If Qty <  10 Then
		Qty=10  ´Qty is set to a default value 10
	End If
End Sub

End Structure
VB.NET - Database(pdf)

Twitter Delicious Facebook Digg Stumbleupon Favorites More