Monday, July 18, 2005

Data Access Application Block for .NET

I got someinfo from MSDN(2004 july).it's very interesting. have a look.

Are you involved in the design and development of data access code for .NET-based applications? Have you ever felt that you write the same data access code again and again? Have you wrapped data access code in helper functions that let you call a stored procedure in one line? If so, the Microsoft Data Access Application Block for .NET is for you.

The Data Access Application Block encapsulates performance and resource management best practices for accessing Microsoft SQL Server databases. It can easily be used as a building block in your own .NET-based application. If you use it, you will reduce the amount of custom code you need to create, test, and maintain.

So we can Create One data access class for all da application wich access Data base.


Public Shared Sub PrepareCommand(ByVal command As SqlCommand, _
ByRef connection As SqlConnection, _
ByVal commandType As CommandType, _
ByVal commandText As String, _
ByRef mustCloseConnection As Boolean)


If (command Is Nothing) Then Throw New ArgumentNullException("command")
If (commandText Is Nothing OrElse commandText.Length = 0) Then Throw New ArgumentNullException("commandText")

If connection.State <> ConnectionState.Open Then
connection.Open()
mustCloseConnection = True
Else
mustCloseConnection = False
End If

command.Connection = connection
command.CommandText = commandText
command.CommandType = commandType
Return

End Sub

Comman Select Statement

Private Shared Function PrepareSQLSelect(ByVal strSQL As SqlSelect) As String

If (strSQL Is Nothing) Then
Throw New ArgumentNullException("strSQL")
Else
If strSQL.TableName = "" Then
Throw New ArgumentNullException("strSQL.TableName")
End If
End If


'create SQL Select
Dim str As New StringBuilder(QuerySize)
With strSQL
If .FieldName <> "" Then
str.Append("SELECT ")
str.Append(.FieldName)
str.Append(" FROM ")
str.Append(.TableName)
Else
str.Append("SELECT * FROM ")
str.Append(.TableName)
End If


If .CmdWhere <> "" Then
str.Append(" WHERE ")
str.Append(.CmdWhere)
End If
If .SortBy <> "" Then
str.Append(" ORDER BY ")
str.Append(.SortBy)
End If
End With
Return str.ToString
End Function


Like wise we can write other functions like Insert,Delete and update.once You Create SQLHELPER Class, i think your data accessing will be Nothing

Source
http://www.gotdotnet.com/workspaces/workspace.aspx?id=c20d12b0-af52-402b-9b7c-aaeb21d1f431

No comments: