IBinarySerialize.Write Method (BinaryWriter)

 

Converts a user-defined type (UDT) or user-defined aggregate into its binary format so that it may be persisted.

Namespace:   Microsoft.SqlServer.Server
Assembly:  System.Data (in System.Data.dll)

Sub Write (
	w As BinaryWriter
)

Parameters

w
Type: System.IO.BinaryWriter

The BinaryWriter stream to which the UDT or user-defined aggregate is serialized.

Write sufficient information to the binary stream to allow the Read method to reconstitute your UDT or user-defined aggregate.

The following example shows the implementation of the Writemethod of a UDT, which uses a BinaryWriter to serialize the UDT in the user-defined binary format. The purpose of the null character padding is to ensure that the string value is completely separated from the double value, so that one UDT is compared to another in Transact-SQL code, string bytes are compared to string bytes and double bytes are compared to double bytes.

' The binary layout is as follows:
'    Bytes 0 - 19: string text, padded to the right with null characters
'    Bytes 20+: Double value
Public Sub Write(ByVal w As System.IO.BinaryWriter) _
  Implements Microsoft.SqlServer.Server.IBinarySerialize.Write

    Dim maxStringSize As Integer = 20
    Dim stringValue As String = "The value of PI: "
    Dim paddedString As String
    Dim value As Double = 3.14159

    ' Pad the string from the right with null characters.
    paddedString = stringValue.PadRight(maxStringSize, ControlChars.NullChar)


    ' Write the string value one byte at a time.
    Dim i As Integer
    For i = 0 To paddedString.Length - 1 Step 1 
        w.Write(paddedString(i))
    Next

    ' Write the double value.
    w.Write(value)

End Sub

.NET Framework
Available since 2.0
Return to top
Show: