-----------------------------------------------------------

New Message on BDOTNET

-----------------------------------------------------------
From: AnujAhuja
Message 5 in Discussion

 
Hi Ramu, 
While taking a simple example I'll try to explain how we can use disconnect Connection 
to connect get data form the database in object oriented world. 
Take Example of Order Class, Following Diagram shows relationship 
  
Following will explain Source code will explain rest of things  
' Create new Access Database Order.mdb 
' Create new Table Called Order with following fields 
' OrderNumber : Number 
' Orderdate : Date/Time 
' ItemName : Text 
' And then either modify Connection String according to path of access  
' database file or place it at c: 
  
Public Class DataManager 
' Database handing class 
' Can have singleton implementation 
' This class will have ADO.net connection in disconnected mode 
' When ever data is requested then only it opens the connection, get data, close 
connection and return data to the client 
Private strConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data 
Source=C:\Order.mdb" 
Private oConnection As Data.OleDb.OleDbConnection 
Private oCommand As Data.OleDb.OleDbCommand 
Public Sub New() 
Try 
oConnection = New Data.OleDb.OleDbConnection(strConnectionString) 
oCommand = New Data.OleDb.OleDbCommand() 
oCommand.Connection = oConnection 
oCommand.CommandType = CommandType.Text 
Catch ex As Exception 
MsgBox("Cant able to create connection object!") 
End Try 
End Sub 
Public Function GetData(ByVal Sql As String) As DataTable 
oCommand.CommandText = Sql 
Try 
Dim oAdapter As New Data.OleDb.OleDbDataAdapter(oCommand) 
Dim oDataTable As New DataTable() 
' Hear connection will be opened, used and closed by the adapter 
oAdapter.Fill(oDataTable) 
Return oDataTable 
Catch ex As Exception 
MsgBox("Cant able to execute SQL!") 
Return Nothing 
End Try 
End Function 
Public Function SaveData(ByVal Sql As String) As Boolean 
oCommand.CommandText = Sql 
Try 
oConnection.Open() 
oCommand.ExecuteNonQuery() 
oConnection.Close() 
Return True 
Catch ex As Exception 
MsgBox("Cant able to execute SQL!") 
Return False 
End Try 
End Function 
End Class 
Public Class Order 
' Simple Order Class 
Public OrderNumber As Integer 
Public OrderDate As Date 
Public ItemName As String 
Private IsNew As Boolean 
Dim oDataManager As New DataManager() 
Public Sub New(ByVal OrderNumber As Integer) 
' If requested Order Number Exits then it will load details from Order Table  
' Otherwise it will set IsNew Flag to True so that Insert statement can be used for  
' saving data in Order Table for the first time 
Dim SQL As String = "Select * from [Order] where OrderNumber = " & 
OrderNumber.ToString 
Dim oDatatable As New DataTable() 
oDatatable = oDataManager.GetData(SQL) 
Me.OrderNumber = OrderNumber 
If oDatatable.Rows.Count <> 0 Then 
OrderDate = CDate(oDatatable.Rows(0)("OrderDate").ToString) 
ItemName = oDatatable.Rows(0)("ItemName").ToString 
IsNew = False 
Else 
IsNew = True 
End If 
End Sub 
Public Sub Save() 
If IsNew Then 
oDataManager.SaveData("INSERT INTO [Order] Values(" & OrderNumber & "," & 
OrderDate.ToShortDateString & ", '" & ItemName & "' )") 
IsNew = False 
Else 
oDataManager.SaveData(String.Format("UPDATE [Order] SET Orderdate = {0}, ItemName = 
'{1}' WHERE OrderNumber={2}", OrderDate.ToShortDateString, ItemName, OrderNumber)) 
End If 
End Sub 
End Class 
Public Class MainClass 
Public Shared Sub main() 
' Creating Order Object of Existing Order details in Order Table i.e. Order Number 1 
Console.WriteLine(" Creating Order Object of Existing Order Number 1") 
Dim oOrder As New Order(1) 
Console.WriteLine("Order Number :" & oOrder.OrderNumber) 
Console.WriteLine("Order Date :" & oOrder.OrderDate) 
Console.WriteLine("Item Name :" & oOrder.ItemName) 
' Creating Order object who's detail is not present in Order Table and Saving it 
Console.WriteLine(" Creating Order object who's details are not present and Saving 
it") 
Dim anotherOrder As New Order(2) 
anotherOrder.ItemName = "IBM" 
anotherOrder.OrderDate = Now 
Console.WriteLine("Order Number :" & anotherOrder.OrderNumber) 
Console.WriteLine("Order Date :" & anotherOrder.OrderDate) 
Console.WriteLine("Item Name :" & anotherOrder.ItemName) 
anotherOrder.Save() 
Console.ReadLine() 
End Sub 
End Class 
  
I hope this will help  
                  - Anuj 
(Sky is the Limit) 
 
View Attachment(s):
http://groups.msn.com/bdotnet/_notifications.msnw?type=msg&mview=1&parent=1&item=18874
-----------------------------------------------------------

To stop getting this e-mail, or change how often it arrives, go to your E-mail 
Settings.
http://groups.msn.com/bdotnet/_emailsettings.msnw

Need help? If you've forgotten your password, please go to Passport Member Services.
http://groups.msn.com/_passportredir.msnw?ppmprop=help

For other questions or feedback, go to our Contact Us page.
http://groups.msn.com/contact

If you do not want to receive future e-mail from this MSN group, or if you received 
this message by mistake, please click the "Remove" link below. On the pre-addressed 
e-mail message that opens, simply click "Send". Your e-mail address will be deleted 
from this group's mailing list.
mailto:[EMAIL PROTECTED]

Reply via email to