Presentation is loading. Please wait.

Presentation is loading. Please wait.

Database Handling Class and Service

Similar presentations


Presentation on theme: "Database Handling Class and Service"— Presentation transcript:

1 Database Handling Class and Service

2 ADO.Net Objects Database Classes Data Source Forms Reports

3 Single-Record-Handling Classes
Retrieves a single record from the database and makes it available to your application in the form of an object. The fields in the record are exposed as the object’s properties. Any actions performed by the data (updates, calculations, etc.) are exposed as the object’s methods.

4 Single-Record-Handling Class Example
Imports System.Data.OleDb Public Class Customer Public cid As String Public CName As String Public City As String Public Rating As String Private hiddenexist As Boolean Private cn As OleDb.OleDbConnection Public ReadOnly Property RecExist() As Boolean Get RecExist = hiddenexist End Get End Property

5 Dim objConn As New OleDbConnection(strConn)
Public Sub getData(ByVal SearchID As String) Dim strConn As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\SalesDB2007.accdb” Dim objConn As New OleDbConnection(strConn) Dim strSQL As String = "select * from customer where cid = '" & SearchID & "'" Dim objComm As New OleDbCommand(strSQL, objConn) objConn.Open() Dim objDataReader As OleDbDataReader objDataReader = objComm.ExecuteReader() If objDataReader.Read() = False Then hiddenexist = False Else hiddenexist = True cid = objDataReader("cid") CName = objDataReader("CName") City = objDataReader("City") Rating = objDataReader("Rating") End If objConn.Close() End Sub

6 Public Sub SaveNew() Dim strConn As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\SalesDB2007.accdb" Dim objConn As New OleDbConnection(strConn) objConn.Open() Dim strSQLInsert As String strSQLInsert = "Insert into Customer values ('" strSQLInsert = strSQLInsert & cid & "','" & CName & "','" strSQLInsert = strSQLInsert & City & "','" & Rating & "')" Dim objCommInsert As New OleDbCommand(strSQLInsert, objConn) objCommInsert.ExecuteNonQuery() objConn.Close() End Sub

7 Enter CID, then use the GetData method to retrieve record

8 Code Example Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim myCust As New Customer() myCust.getData(TextBox1.Text) If myCust.RecExist Then TextBox2.Text = myCust.CName TextBox3.Text = myCust.City TextBox4.Text = myCust.Rating Else MessageBox.Show("record not exist") End If End Sub

9 Using the SaveNew Method to Add A New Customer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim newCust As New Customer() newCust.cid = TextBox1.Text newCust.CName = TextBox2.Text newCust.City = TextBox3.Text newCust.Rating = TextBox4.Text newCust.SaveNew() TextBox1.Text = "" TextBox2.Text = "" TextBox3.Text = "" TextBox4.Text = "" End Sub

10 From Class to Service

11 What is a service? A service is a program that can be interacted with via well-defined message exchanges.

12 .Net Implements a service as an assembly
Basic unit of deployment. Implemented as Dynamic Link Library, DLL.

13 Difference between Assembly and Class
A class defined in a project is available to that project only. Once a class is compiled into an assembly it can be used by any projects. To create an assembly: Start a Class Library project

14 Steps to Create An Assembly
Start a Class Library project Create classes You can also use existing classes defined in other projects by Project/Add Existing Item Save project Select Build/Build to compile the code. When the class library is compiled successfully, an assembly is created and stored in the project’s Bin/Release folder. Example: If a testClassLib project is created in C:\VS2008Examples folder, then the assembly is found in: C:\VS2008Examples\testClassLib\testClassLib\bin\Release

15 Using the Assembly Reference the assembly: Project/Add Reference and use the Browse button to select the assembly. Import the assembly. If the class is created in ClassLibrary1 library project, then: Imports ClassLibrary1

16 Code Using the Class in Assembly Inserting a New Customer
Imports ClassLibrary1 Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim newCust As New Customer() newCust.cid = TextBox1.Text newCust.CName = TextBox2.Text newCust.City = TextBox3.Text newCust.Rating = TextBox4.Text newCust.SaveNew() TextBox1.Text = "" TextBox2.Text = "" TextBox3.Text = "" TextBox4.Text = "" End Sub End Class


Download ppt "Database Handling Class and Service"

Similar presentations


Ads by Google