Saving enum to Sql Server
How do I save an Enum type in SqlServer?
Also, I know I can store an entire object in SqlServer 2005. Does someone know how?
[128 byte] By [
jhammer] at [2007-11-20 2:09:59]

# 1 Re: Saving enum to Sql Server
Hello,
You can save Enum/ objec in Db using User defined type. Please refer following. I have defined CollectionsBE class and using it in SQL server as datatype.
Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.SqlTypes
Imports Microsoft.SqlServer.Server
Namespace MyProjects.CLR
<Serializable()> _
<Microsoft.SqlServer.Server.SqlUserDefinedType(Format.UserDefined, IsByteOrdered:=False, MaxByteSize:=8000)> _
Public Structure CollectionsBE
Implements INullable
Implements IBinarySerialize
Public Overrides Function ToString() As String
' Put your code here
Return Me.ToString
End Function
Public ReadOnly Property IsNull() As Boolean Implements INullable.IsNull
Get
' Put your code here
Return m_Null
End Get
End Property
Private Function ValidatePoint() As Boolean
Return True
End Function
Public Property Value() As String
Get
Return m_Value.ToString
End Get
Set(ByVal value As String)
m_Value = value
End Set
End Property
Public Property Description() As String
Get
Return m_Description.ToString
End Get
Set(ByVal value As String)
m_Description = value
End Set
End Property
Public Property Condition() As String
Get
Return m_Condition.ToString
End Get
Set(ByVal value As String)
m_Condition = value
End Set
End Property
Public Shared ReadOnly Property Null() As CollectionsBE
Get
Dim h As CollectionsBE = New CollectionsBE
h.m_Null = True
Return h
End Get
End Property
Public Shared Function Parse(ByVal s As SqlString) As CollectionsBE
If s.IsNull Then
Return Null()
End If
Dim u As CollectionsBE = New CollectionsBE
Dim sR() As String = Split(s.ToString, ";")
u.m_Value = sR(0).ToString
u.m_Description = sR(1).ToString()
u.m_Condition = IIf(sR(2).ToString() = "", "", sR(2)).ToString()
' Put your code here
Return u
End Function
' This is a place-holder method
Public Function Method1() As String
' Put your code here
Return "Hello - " + m_Value.ToString + " " + m_Description.ToString()
End Function
Private Sub Read(ByVal a As System.IO.BinaryReader) Implements IBinarySerialize.Read
m_Value = a.ReadString
m_Description = a.ReadString
m_Condition = a.ReadString
End Sub
Private Sub Write(ByVal b As System.IO.BinaryWriter) Implements IBinarySerialize.Write
b.Write(m_Value.ToString)
b.Write(m_Description.ToString)
b.Write(m_Condition.ToString)
End Sub
' Private member
Private m_Null As Boolean
Private m_Value As SqlString
Private m_Description As SqlString
Private m_Condition As SqlString
End Structure
End Namespace
Check the following SQL statements.
DECLARE @I_Collections CollectionsBE
SET @I_Collections = Convert(CollectionsBE, 'IND;SYSTEM;real')
INSERT INTO CUSTOMERS(IS_DELETED, CREATED_BY, CREATED_DATE, MODIFIED_BY, MODIFIED_DATE, CUS_TYPE)
VALUES(0, @I_Collections.Description, GETDATE(), @I_Collections.Description, GETDATE(), @I_Collections.Value)