C# Version type value and binary binary type conversion in SQL Server

Applications written in C# language can control the version number of each release through the Version class provided by the .NET Framework framework, so as to better control each version update iteration.

Version numbers consist of two to four components: major, minor, build, and revision .

The format of the version number is as follows, with optional components shown in square brackets ([ and ]):

major.minor[.build[.revision]]

 A new instance of the Version class can be initialized with the specified major version number, minor version number, build number, and revision number  .

Example:

// 实例
Version(Major, Minor, Build, Revision);

/*
其中, 
Marjor:主要版本号
Minor:次要版本号
Build:内部版本号
Revision:修订号

new Version(1, 0, 0, 1); 
或
new Version("1.0.0.1");
*/

C# code snippet:

/ 创建Version类对象
var version = new Version("1.0.0.1");

/* 注:使用Version类作为EF实体对象属性的类型进行映射,对应SQL Server表的字段类型为:binary(16) */

Because the actual business needs to persistently store the version number information of each update of the application in the SQL Server database, the Entity Framework entity framework allows direct mapping using the Version class as an attribute type.

Note: Entity Framework is called EF for short.

The EF Entity Framework automatically converts the mapping Version type value into a fixed-length (16-bit) binary type (binary) value in the SQL Server database for storage.

Analysis of binary(16) type value conversion for Version type mapping:

The version number value stored in the SQL Server data table (after conversion) format: 0x00000001 00000000 [00000000 00000000 ].

It is necessary to perform hexadecimal number conversion on the version number value (decimal) of each bit, and then combine each version number (hexadecimal number) into a new hexadecimal value.

like:

Version number: 1.0.0.1

Convert hex:

       1                          0                         0                     1

0x00000001      0x00000000      0x00000000      0x00000001

Combine new hexadecimal numbers ( Note: Except for the first digit from left to right to retain the "0x" hexadecimal prefix, other version number conversions will remove the prefix ):

0x00000001 00000000 00000000 00000001

The SQL Server database does not provide the conversion function of the binary type value (binary) generated by the C# language Version class. In order to realize the insertion of data that can interactively convert the Version type by writing SQL statements , a custom version number conversion binary type processing function (dbo .versionstrtobinary).

SQL code:

/*
功能:将版本号(如:1.0.0.1)转换为长度为 16 个字节的固定长度二进制(binary)数据处理
说明:C#开发语言使用的Version类型值(即:版本号)转换成SQL Server数据库中存储二进制类型[binary]处理
*/
IF EXISTS (SELECT * FROM sys.objects WHERE NAME='versionstrtobinary')
    DROP FUNCTION dbo.versionstrtobinary;
GO
CREATE FUNCTION dbo.versionstrtobinary
(
    @version varchar(max)
)
RETURNS binary(16)
AS
BEGIN
    DECLARE @value int
    DECLARE @bin binary(16)
    DECLARE @binStr nvarchar(max)
    DECLARE @minor varchar(max)   -- 版本号
    
    DECLARE @versionStr nvarchar(max) = @version;

    DECLARE @j int = 1;

    DECLARE @i int = 0;
    set @i = CHARINDEX('.', @versionStr); 

    DECLARE @flag bit = 1;
       
    WHILE @flag = 1
    BEGIN
        if (@i = 0)
        BEGIN
            SET @minor = @versionStr;
        END
        ELSE
        BEGIN
            -- 截取版本号值
            set @minor = SUBSTRING(@versionStr, 1, @i-1);
        END

        -- 将版本号值转换为int类型
        set @value = CAST(@minor as int);

        -- 注:sys.fn_varbintohexsubstring()函数第1个参数表示是否保留0x前缀,1为保留,0为不保留
        if (@j = 1)
        BEGIN            
            set @binStr = sys.fn_varbintohexsubstring(1, CONVERT(VARBINARY(50), @value), 1, 0);
        END
        else
        BEGIN
            set @binStr = @binStr + sys.fn_varbintohexsubstring(0, CONVERT(VARBINARY(50), @value), 1, 0);
        END
    
        set @versionStr = SUBSTRING(@versionStr, @i+1, LEN(@versionStr) - @i);
        set @i = CHARINDEX('.', @versionStr);

        if (@j = LEN(@version) - 3)
        BEGIN
            break;
        END

        set @j = @j + 1;
    END

    set @bin = convert(binary(16), @binStr, 1);  -- 如果字符串前面有0x字符,那么使用风格1(设置style=1);如果字符串前面没有0x字符,那么使用风格2(设置style=2)
    
    RETURN @bin
END



-- 测试函数(0x00000001000000000000000000000006)
select dbo.versionstrtobinary('1.0.0.6');

 -- 插入Version类型数据SQL语句
 INSERT Versions (Version) VALUES (dbo.versionstrtobinary('1.0.0.1')); 
 GO

References:

Version class (System) | Microsoft Learn

binary 和 varbinary (Transact-SQL) - SQL Server | Microsoft Learn

Guess you like

Origin blog.csdn.net/LZD_jay/article/details/129329610