IBinarySerialize.Write(BinaryWriter) IBinarySerialize.Write(BinaryWriter) IBinarySerialize.Write(BinaryWriter) IBinarySerialize.Write(BinaryWriter) Method

定义

将用户定义的类型 (UDT) 或用户定义的聚合转换为其二进制格式,以便保留。Converts a user-defined type (UDT) or user-defined aggregate into its binary format so that it may be persisted.

public:
 void Write(System::IO::BinaryWriter ^ w);
public void Write (System.IO.BinaryWriter w);
abstract member Write : System.IO.BinaryWriter -> unit
Public Sub Write (w As BinaryWriter)

参数

w
BinaryWriter BinaryWriter BinaryWriter BinaryWriter

UDT 或用户定义聚合所序列化到的 BinaryWriter 流。The BinaryWriter stream to which the UDT or user-defined aggregate is serialized.

示例

下面的示例演示Write BinaryWriter如何实现 udt 的方法, 该方法使用来序列化用户定义的二进制格式的 UDT。The following example shows the implementation of the Write method of a UDT, which uses a BinaryWriter to serialize the UDT in the user-defined binary format. Null 字符填充的目的是确保字符串值与 double 值完全分离, 因此, 在 Transact-sql 代码中将一个 UDT 与另一个 UDT 进行比较, 将字符串字节与字符串字节进行比较, 将双字节与双字节进行比较字节.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 void Write(System.IO.BinaryWriter w)
{        
    int maxStringSize = 20;
    string stringValue = "The value of PI: ";
    string paddedString; 
    double value = 3.14159;

    // Pad the string from the right with null characters.
    paddedString = stringValue.PadRight(maxStringSize, '\0');

    // Write the string value one byte at a time.
    for (int i = 0; i < paddedString.Length; i++)
    {
        w.Write(paddedString[i]);
    }

    // Write the double value.
    w.Write(value);
}
' 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

注解

将足够多的信息写入二进制流, 以Read允许方法重建 UDT 或用户定义的聚合。Write sufficient information to the binary stream to allow the Read method to reconstitute your UDT or user-defined aggregate.

适用于