Click here to Skip to main content
Licence CPOL
First Posted 4 Jul 2005
Views 28,893
Bookmarked 24 times

Use of custom attributes to serialise objects to/from SQL Server tables

By | 1 Nov 2005 | Article
Shows how you can use custom attributes to save .NET classes directly to an SQL Server table.
 
Part of The SQL Zone sponsored by
See Also

Introduction

This class library is a quick implementation to use custom attributes to marshal a .NET class to/from an SQL Server database table. It is fairly simple in that it assumes a one-to-one mapping between the class and the table.

Classes involved

DatabaseTableAttribute

This class inherits Attribute and is used to identify the table that the object gets read/written to.

DatabaseFieldAttribute

This class inherits Attribute and identifies the field that the property gets read/written to.

SQLTableInterop

This class handles the interop between the object and the table.

SQLUtilities

Class that contains some utility functions for SQL Server.

Usage example

Suppose we have a table [Currency] defined thus:

CREATE TABLE Currency
(
  [Currency Code] char(3), --key
  [Currency Name] varchar(200)
)

Then we could create a .NET class that saves and reads from this table thus:

   <DatabaseTableAttribute("CURRENCY")> _
   Public Class Staff

   Private _Code As String
   Private _Name As String

   Public Sub New(Byval Code As String)
      _Code = Code
      Dim SQLTableInterop As New SQLTableInterop(ConnectionString)
      SQLTableInterop.GetObjectDataFromTable(Me)
   End Sub

   <DatabaseFieldAttribute("Currency Code",True)> _
        Public ReadOnly Property CurrencyCode() As String
            Get
                Return _Code
            End Get
        End Property

   <DatabaseFieldAttribute("Currency Name",False)>  _
        Public Property CurrencyName() As String
            Get
                Return _Name
            End Get
            Set(ByVal Value As String)
                If Value <> _Name Then
                    _Name = Value
                End If
            End Set
        End Property

    Public Sub Save()
      Dim SQLTableInterop As New SQLTableInterop(ConnectionString)
      SQLTableInterop.SetObjectDataToTable(Me)
    End Sub

Obviously the usefulness of this utility DLL increases for larger tables.

History

  • 01 Nov 2005 - Changed the code to use a parameterised query rather than building the values in to prevent SQL injection type vulnerability.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Duncan Edwards Jones

Software Developer (Senior)
JP Morgan
Ireland Ireland

Member

Follow on Twitter Follow on Twitter
C# / SQL Server developer
Microsoft MVP 2006, 2007
Visual Basic .NET

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralUpdate : 2005-11-01 PinmemberDuncan Edwards Jones5:58 1 Nov '05  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 1 Nov 2005
Article Copyright 2005 by Duncan Edwards Jones
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid