Click here to Skip to main content
15,881,380 members
Articles / Database Development / SQL Server / SQL Server 2008

SqlTimeSpan

Rate me:
Please Sign up or sign in to vote.
5.00/5 (5 votes)
30 Jul 2009CPOL19 min read 44.6K   156   31  
Porting of System.TimeSpan as a SQL Server UDT.
using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;

[Serializable]
[SqlUserDefinedType(Format.UserDefined
    , MaxByteSize = 9
    , IsByteOrdered = true)]
public struct LongUdt : INullable, IBinarySerialize
{
    private bool m_Null;
    public long _longField;

    public override string ToString()
    {
        return _longField.ToString();
    }

    public bool IsNull
    {
        get
        {
            // Put your code here
            return m_Null;
        }
    }

    public static LongUdt Null
    {
        get
        {
            LongUdt h = new LongUdt();
            h.m_Null = true;
            return h;
        }
    }

    public static LongUdt Parse(SqlString s)
    {
        if (s.IsNull)
            return Null;
        LongUdt u = new LongUdt();
        u._longField = long.Parse(s.Value);
        return u;
    }

   
    #region IBinarySerialize Members

    void IBinarySerialize.Write(System.IO.BinaryWriter w)
    {
        // WARNING! No Null check here.

        // ----------------------------------
        // Int64 (long) version
        //long u = var1;
        //w.Write(u);


        // ----------------------------------
        // UInt64 (ulong) version        
        ulong u = (ulong)(_longField + long.MaxValue + 1);
        w.Write(u);
    }

    void IBinarySerialize.Read(System.IO.BinaryReader r)
    {
        // WARNING! No Null check here.

        // ----------------------------------
        // Int64 (long) version
        //var1 = r.ReadInt64();

        // ----------------------------------
        // UInt64 (ulong) version   
        ulong u = r.ReadUInt64();
        _longField = (long)(u - long.MaxValue - 1);
    }
    #endregion
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Software Developer (Senior)
United States United States
Gio has been writing programs for almost 10 years. Sometime on 2005, he decided to cut his hair and remove his nail polish so he could enter a commercial building in downtown Manila and submit his resume. That bold action eventually led him to the city by the bay in the sunshine state. He really like it there because he's still able to experience hurricanes which makes him very nostalgic albeit disappointed because it doesn't automatically translate to work suspension. If he's not outside watching squirrels play, he explores esoteric topics using .NET and SQL Server. And like squirrels, Gio is a very docile yet hardworking creature. If ever you see faults in his work, be gentle with the criticisms. He greatly appreciates this and rest assured your advice would be embodied in his future works.

Comments and Discussions