在SQL中使用CLR提供基本函数对二进制数据进行解析与构造
二进制数据包的解析一般是借助C#等语言,在通讯程序中解析后形成字段,再统一单笔或者批量(表类型参数)提交至数据库,在通讯程序中,存在BINARY到struct再到table的转换。
现借助CLR提供基本的INT2HEX(小端)等函数,在SQL中直接解析数据包。
基本函数
- [Microsoft.SqlServer.Server.SqlFunction(Name = "Time2UTCBin")]
- public
static SqlBinary Time2UTCBin(DateTime time) - {
- return
new SqlBinary(BitConverter.GetBytes((uint)(MyTime.ConverDateTimeToJavaMilliSecond(time) / 1000))); - }
- [Microsoft.SqlServer.Server.SqlFunction(Name = "UTCBin2Time")]
- public
static SqlDateTime UTCBin2Time(byte[] data,int offset) - {
- return
new SqlDateTime(MyTime.ConverDateTimeFromJavaMilliSecond(BitConverter.ToUInt32(data, offset) * 1000L)); - }
- [Microsoft.SqlServer.Server.SqlFunction(Name = "getSum")]
- public
static SqlByte Sum(byte[] buffer, int startPos, int endPos) - {
- byte b = 0;
- for (int i = startPos; i <= endPos; i++)
- {
- b ^= buffer[i];
- }
- return b;
- }
- [Microsoft.SqlServer.Server.SqlFunction(Name = "updateSum")]
- public
static SqlBinary updateSum(byte[] buffer, int startPos, int endPos, int sumPos) - {
- byte b = 0;
- for (int i = startPos; i <= endPos; i++)
- {
- b ^= buffer[i];
- }
- buffer[sumPos] = b;
- return
new SqlBinary(buffer); - }
- [Microsoft.SqlServer.Server.SqlFunction(Name = "Int2Bin")]
- public
static SqlBinary Int2Bin(int number) - {
- return
new SqlBinary(BitConverter.GetBytes(number)); - }
- [Microsoft.SqlServer.Server.SqlFunction(Name = "Long2Bin")]
- public
static SqlBinary Long2Bin(long number) - {
- return
new SqlBinary(BitConverter.GetBytes(number)); - }
- [Microsoft.SqlServer.Server.SqlFunction(Name = "Bin2Int")]
- public
static SqlInt32 Bin2Int(byte[] data, int offset) - {
- return
new SqlInt32(BitConverter.ToInt32(data,offset)); - }
- [Microsoft.SqlServer.Server.SqlFunction(Name = "Bin2Long")]
- public
static SqlInt64 Bin2Long(byte[] data, int offset) - {
- return
new SqlInt64(BitConverter.ToInt64(data, offset)); - }
- [Microsoft.SqlServer.Server.SqlFunction(Name = "getByte")]
- public
static SqlByte getByte(byte[] data, int offset) - {
- return
new SqlByte(data[offset]); - }
- [Microsoft.SqlServer.Server.SqlFunction(Name = "getBytes")]
- public
static SqlBytes getBytes(byte[] data, int offset,int count) - {
- byte[] temp = new
byte[count]; - Array.Copy(data, offset, temp, 0, count);
- return
new SqlBytes(temp); - }
数据包的结构体(表类型)
SQL中借助CLR实现的转换函数
- (
- @offset INT = 1 ,
- @withDeviceID BIT = 0
- )
- (
- [DeviceID] UNIQUEIDENTIFIER ,
- Meter INT ,
- run INT ,
- dead INT ,
- StartTime DATETIME ,
- EndTime DATETIME
- )
- IF @withdeviceid = 1
- SET @sized = 16
- SET @sized = 0
- WITH sub
- @size) binDATA
- FROM sys_id
- WHERE id < @c
- )
- INSERT @emv
- ( rawdata ,
- DeviceID ,
- hardwareno ,
- meter ,
- run ,
- WORK ,
- dead ,
- starttime ,
- endtime ,
- emvtype ,
- emvno
- )
- END ,
- CASE @withDeviceID
- END ,
- dbo.Bin2Int(bindata, 0 + @sized) RecordNo ,
- dbo.Bin2Int(bindata, 4 + @sized) Meter ,
- dbo.Bin2Int(bindata, 8 + @sized) run ,
- dbo.Bin2Int(bindata, 16 + @sized) dead ,
- dbo.utcbin2time(bindata, 20 + @sized) StartTime ,
- dbo.utcbin2time(bindata, 24 + @sized) EndTime ,
- dbo.getByte(bindata, 61 + @sized) RecordType ,
- dbo.getByte(bindata, 62 + @sized) EMVNo
- FROM sub
- (
- @emv DeviceTranscationEMV READONLY ,
- @withDeviceID BIT = 0
- )
- SET @bin = 0x0
- IF @withDeviceID = 0
- + dbo.int2Bin(meter)
- + dbo.int2Bin(run)
- + dbo.int2Bin(dead)
- + dbo.time2utcbin(starttime)
- + dbo.time2utcbin(endtime)
- 0, 62, 63)
- FROM @emv
- + dbo.updatesum(dbo.int2Bin([HardwareNo])
- + dbo.time2utcbin(starttime)
- + dbo.time2utcbin(endtime)
- FROM @emv
- RETURN @bin
测试代码
- PRINT N'构造EMV数据,转换为BIN,然后再转换回EMV数据'
- go
- DECLARE @emv DeviceTranscationEMV
- WITH data
- 15 Meter ,
- 100 run ,
- 80 WORK ,
- 20 dead ,
- DATEADD(s, id, '2014-9-1 12:50:01') StartTime ,
- DATEADD(mi, id, '2014-9-1 13:23:11') EndTime ,
- 4 RecordType ,
- 0 EMVNo
- FROM dbo.Sys_ID
- WHERE id < 100
- )
- ( rawdata ,
- deviceid ,
- emvtype ,
- emvno ,
- hardwareno ,
- meter ,
- run ,
- work ,
- dead ,
- starttime ,
- endtime
- )
- SELECT 0x00 ,
- NEWID() ,
- recordtype ,
- emvno ,
- recordno ,
- meter ,
- run ,
- work ,
- dead ,
- starttime ,
- EndTime
- FROM data
- SELECT *
- FROM @emv
- PRINT @data
- SELECT *
执行结果:
资源:
在SQL中使用CLR提供基本函数对二进制数据进行解析与构造的更多相关文章
- sql中根据逗号分隔,查出多行数据
--sql中根据逗号分隔,查出多行数据 select a.DiscussID,b.LocationID from (select DiscussID,LocationID=c ...
- sql中的 where 、group by 和 having 用法解析
--sql中的 where .group by 和 having 用法解析 --如果要用到group by 一般用到的就是“每这个字” 例如说明现在有一个这样的表:每个部门有多少人 就要用到分组的技术 ...
- ORA-01406:提取的列值被截断 ; SQL Server :将截断字符串或二进制数据
oracle 数据库可以正常连接,表数据也可以正常读取, 但在程序中相同的位置,有时会报错,有时不会报错,有的电脑会报错,有的不会 报错内容为 ORA-01406:提取的列值被截断 查了网上提供的一些 ...
- 在SQL中直接把查询结果转换为JSON数据
下面这篇中,已经有准备一些数据: <MS SQL server对象类型type>https://www.cnblogs.com/insus/p/10903916.html 为前端服务,直接 ...
- SQL中 根据行号设置每行数据的排序数值
根据行号自动把当前行号插入到某列中 实现排序 update tempTable set DisplayOrder = right( CAST(rownum as NVARCHAR),5) from( ...
- sql中关闭自增,并插入数据
ET IDENTITY_INSERT 允许将显式值插入表的标识列中. 语法 SET IDENTITY_INSERT [ database.[ owner.] ] { table } { ON | OF ...
- sql 中如何查询某一列的数据在另一个表中有没有?
假设表table1,列a,表table2,列bselect a from table1where a not in(select b from table2)
- Sql 中的as是什么意思 + 无列名注入解析
相当于取别名 这里结合一下无列名注入的知识点: 这种方法在第十届SWPUCTF的web1——广告招租里考到了:
- SQL中采用Newtonsoft.Json处理json字符串
原文 SQL中采用Newtonsoft.Json处理json字符串 使用环境: SQL Server2005; VS2010; 关于SQL中部署CLR程序集的方法,网上一搜一大把,需要了解的自行查阅, ...
随机推荐
- JS打印页面指定区域
错误的写法: //打印 function printPage(areaId) { if (parent.$("#PrinFrame").length == 0) { parent. ...
- 利用T4模板生成ASP.NET Core控制器的构造函数和参数
前言 在ASP.NET Core中引入了DI,并且通过构造函数注入参数,控制器中会大量使用DI注入各种的配置参数,如果配置注入的参数比较多,而且各个控制器需要的配置参数都基本一样的话,那么不断重复的复 ...
- inner join on, left join on, right join on的区别与介绍
Table A aid adate 1 a1 2 a2 3 a3 TableB bid bdate 1 b1 2 b2 4 b4 两个表a,b相连接, ...
- Lua使用心得(2)
在lua脚本调用中,如果我们碰到一种不好的脚本,例如: while 1 do do end 那我们的程序主线程也会被阻塞住.那我们如何防止这种问题呢?下面就给出一个解决的办法. 首先为了不阻塞主线程, ...
- 介绍开源的.net通信框架NetworkComms框架 源码分析(十一)PacketBuilder
原文网址: http://www.cnblogs.com/csdev Networkcomms 是一款C# 语言编写的TCP/UDP通信框架 作者是英国人 以前是收费的 目前作者已经开源 许可是 ...
- 查看SQL Server多实例的版本
通过 select @@version 查看当前的 SQL Server 安装的版本: 结果返回的是 SQL Server 2008 R2 (SP1),可安装的明明是 SQL Server 2012 ...
- 使用MySQL WorkBench导入数据库
1. 在MySQL WorkBench的Server Administrator中双击要连接的数据库: 2. 点击左边的Data Import/Restore; 3. Import from Dump ...
- MySQL: @variable vs. variable. Whats the difference?
MySQL: @variable vs. variable. Whats the difference? up vote351down votefavorite 121 In another qu ...
- 修正 Memo 設定為 ReadOnly 後, 無法有複製的功能
问题:当 Memo 設定為 ReadOnly = True 後, 选取一段文字后,無法有複製的功能. 适用:XE6 Android 系统(目前 iOS 还找不到方法) 修正方法: 请将源码 FMX.P ...
- [转]JavaScript程序编码规范
原文:http://javascript.crockford.com/code.html 作者:Douglas Crockford 译文:http://www.yeeyan.com/articles/ ...