In this section, I will show you how to connect Data Access Layer(DAL) to previously designed database customer table in Part -1. But in order to access the database we will require connection string, so let's define the connection string called “LocalSqlServer” in web.config file.
NOTE: Considering it is a quick and dirty example, please keep in mind that DAL depends on customer object in this given example, so you will have to visit the Part 3 where Business Object is created, copy that code first. You will have to comment Data Access Layer calls in Customer class Temporarily, once your DAL methods is ready, you will have to uncomment them to make it work.
Web.Config<connectionStrings><remove name="LocalSqlServer" />
<add name="LocalSqlServer" connectionString="Data Source=Vishwa;Database=Example;User ID=eUser;Password=ePassword;" providerName="System.Data.SqlClient" /></connectionStrings>Data Access Layer Code:
DataAccess.vb: This class will be consumed by Customer class of Business Logic Layer (BLL) and allow to Get, Insert, Update and Delete customer record. Please note that there is a reference of BLL Namespace for Customer class in this class, which will described in BLL section (Part 3).
Option Explicit OnOption Strict OnImports System.Data
Imports System.Data.SqlClient
Imports Vishwa.Example.Business
Namespace Vishwa.Example.Data
Public Class DataAccess' Author : Vishwa Mohan
' Date : 10/15/2006
' Class : Data Accesss Manager
‘ Design Pattern: Singleton' Purpose: An Example to demonstrate Customer Data Access Management
Private Shared instance As New DataAccess#Region "--Customer Functions--- "Public Shared Function GetCustomers(ByVal custID As Integer) As CustomerDim custRecord As New CustomerUsing connection As New SqlConnection(GetConnectionString())Using command As New SqlCommand("dbo.Usp_GetCustomer", connection)command.CommandType = CommandType.StoredProcedurecommand.Parameters.Add(New SqlParameter("@CustID", SqlDbType.Int)).Value = custIDcommand.Connection.Open()Using reader As SqlDataReader = command.ExecuteReader(CommandBehavior.CloseConnection)
Dim tempCustomer As New CustomercustRecord.CustID = CInt(reader.Item("Cust_ID"))custRecord.CustName = reader.Item("Cust_Name").ToString
custRecord.CustAddress = reader.Item("Cust_Address").ToString
custRecord.CustDOB = CDate(reader.Item("Cust_DOB"))custRecord.DateCreated = CDate(reader.Item("Date_Created"))custRecord.DateModified = CDate(reader.Item("Date_Modified"))End Using
End Using
End Using
Return custRecord
End FunctionPublic Shared Function GetAllCustomers() As Generic.List(Of Customer)Dim list As New Generic.List(Of Customer)()Using connection As New SqlConnection(GetConnectionString())Using command As New SqlCommand("dbo.Usp_GetCustomers", connection)command.CommandType = CommandType.StoredProcedurecommand.Connection.Open()Using reader As SqlDataReader = command.ExecuteReader(CommandBehavior.CloseConnection)
Do While (reader.Read())Dim tempCustomer As New CustomertempCustomer.CustID = CInt(reader.Item("Cust_ID"))tempCustomer.CustName = reader.Item("Cust_Name").ToString
tempCustomer.CustAddress = reader.Item("Cust_Address").ToString
tempCustomer.CustDOB = CDate(reader.Item("Cust_DOB"))tempCustomer.DateCreated = CDate(reader.Item("Date_Created"))tempCustomer.DateModified = CDate(reader.Item("Date_Modified"))list.Add(tempCustomer)Loop
End Using
End Using
End Using
Return list
End FunctionPublic Shared Function InsertCustomer(ByVal custInfo As Customer) As IntegerDim returnValue As Integer = -1Using connection As New SqlConnection(GetConnectionString())Using command As New SqlCommand("dbo.Usp_InsertCustomer", connection)With command
.CommandType = CommandType.StoredProcedure.Parameters.Add(New SqlParameter("@ReturnID", SqlDbType.Int)).Direction = ParameterDirection.ReturnValue.Parameters.Add(New SqlParameter("@CustName", SqlDbType.VarChar, 50)).Value = custInfo.CustName.Parameters.Add(New SqlParameter("@CustDOB", SqlDbType.DateTime)).Value = custInfo.CustDOB.Parameters.Add(New SqlParameter("@CustAddress", SqlDbType.VarChar, 100)).Value = custInfo.CustAddress.Connection.Open().ExecuteNonQuery()returnValue = CInt(command.Parameters("@ReturnID").Value)End WithEnd Using
End Using
Return returnValue
End FunctionPublic Shared Function UpdateCustomer(ByVal custInfo As Customer) As IntegerDim returnValue As Integer = -1Using connection As New SqlConnection(GetConnectionString())Using command As New SqlCommand("dbo.Usp_UpdateCustomer", connection)With command
.CommandType = CommandType.StoredProcedure.Parameters.Add(New SqlParameter("@ReturnID", SqlDbType.Int)).Direction = ParameterDirection.ReturnValue.Parameters.Add(New SqlParameter("@CustID", SqlDbType.Int)).Value = custInfo.CustID.Parameters.Add(New SqlParameter("@CustName", SqlDbType.VarChar, 50)).Value = custInfo.CustName.Parameters.Add(New SqlParameter("@CustDOB", SqlDbType.DateTime)).Value = custInfo.CustDOB.Parameters.Add(New SqlParameter("@CustAddress", SqlDbType.VarChar, 100)).Value = custInfo.CustAddress.Connection.Open().ExecuteNonQuery()returnValue = CInt(command.Parameters("@ReturnID").Value)End WithEnd Using
End Using
Return returnValue
End FunctionPublic Shared Function DeleteCustomer(ByVal custInfo As Customer) As IntegerDim returnValue As Integer = -1Using connection As New SqlConnection(GetConnectionString())Using command As New SqlCommand("dbo.Usp_DeleteCustomer", connection)With command
.CommandText = "Usp_DeleteCustomer"
.CommandType = CommandType.StoredProcedure.Parameters.Add(New SqlParameter("@ReturnID", SqlDbType.Int)).Direction = ParameterDirection.ReturnValue.Parameters.Add(New SqlParameter("@CustID", SqlDbType.Int)).Value = custInfo.CustID.Connection.Open().ExecuteNonQuery()returnValue = CInt(command.Parameters("@ReturnID").Value)End WithEnd Using
End Using
Return returnValue
End Function#End Region
#Region "Common Connection Functions"Private Shared Function GetConnectionString() As StringTry
Return ConfigurationManager.ConnectionStrings.Item("LocalSqlServer").ConnectionStringCatch exc As ExceptionReturn ""End TryEnd Function#End Region
#Region "Private Constructor"Private Sub New()End Sub#End Region
End ClassEnd NamespaceNow you can move on to Business Logic Layer in Part 3
Sunday, October 15, 2006
Developing 3-Tier Application in .NET 2.0 – Part 2
Subscribe to:
Post Comments (Atom)
5 comments:
Good day !.
You re, I guess , perhaps very interested to know how one can make real money .
There is no need to invest much at first. You may start earning with as small sum of money as 20-100 dollars.
AimTrust is what you thought of all the time
AimTrust represents an offshore structure with advanced asset management technologies in production and delivery of pipes for oil and gas.
It is based in Panama with affiliates everywhere: In USA, Canada, Cyprus.
Do you want to become an affluent person?
That`s your choice That`s what you really need!
I`m happy and lucky, I started to take up real money with the help of this company,
and I invite you to do the same. If it gets down to choose a correct companion utilizes your funds in a right way - that`s AimTrust!.
I make 2G daily, and my first deposit was 1 grand only!
It`s easy to join , just click this link http://lovokucoh.100freemb.com/hihyky.html
and lucky you`re! Let`s take our chance together to feel the smell of real money
Good day !.
might , perhaps curious to know how one can collect a huge starting capital .
There is no initial capital needed You may commense to get income with as small sum of money as 20-100 dollars.
AimTrust is what you thought of all the time
The firm represents an offshore structure with advanced asset management technologies in production and delivery of pipes for oil and gas.
It is based in Panama with offices around the world.
Do you want to become an affluent person?
That`s your choice That`s what you really need!
I feel good, I started to get income with the help of this company,
and I invite you to do the same. If it gets down to select a correct companion who uses your savings in a right way - that`s AimTrust!.
I make 2G daily, and my first deposit was 1 grand only!
It`s easy to get involved , just click this link http://ulabusuhu.virtue.nu/utybev.html
and lucky you`re! Let`s take our chance together to become rich
Well I agree but I dream the post should have more info then it has.
Hello!
You may probably be very curious to know how one can manage to receive high yields on investments.
There is no initial capital needed.
You may commense to get income with a sum that usually is spent
on daily food, that's 20-100 dollars.
I have been participating in one project for several years,
and I'm ready to share my secrets at my blog.
Please visit my pages and send me private message to get the info.
P.S. I earn 1000-2000 per day now.
http://theblogmoney.com
Until recently I thought earning online, or creating a home based small business as an impossible task. Lately after interacting some of the industry experts, I developed a methodology to earn continuous stream of income. The mantra is to earn money thorugh multiple streams, this will cut down on the risks involved. Do check my site, where I have listed out all free and easy money making oppotunities, check[url=http://www.earn-online-money.com]Earn Easy Online Money[/url]. Do feel free to comment about the methods, your feedback is important for me. Regards
Post a Comment