快速将珊瑚虫IP数据库转MS SQL2005的图文教程
发布时间:2020-12-12 09:42:05 所属栏目:MsSql教程 来源:网络整理
导读:首先需要准备: UL style="LIST-STYLE-TYPE: decimal" 最新的珊瑚虫IP数据库 http://update.cz88.net/soft/qqwry.rar Microsoft Office Access 2003 EiditPlus MS SQL2005 1.然后打开珊瑚虫IP数据库自带的 ShowIP.exe,选择解压,另存一个TXT文件: 2.用Eidi
首先需要准备: ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() USE[BasName] GO /******对象:UserDefinedFunction[dbo].[X16ToDe]脚本日期:09/19/200713:56:15******/ SETANSI_NULLSON GO SETQUOTED_IDENTIFIERON GO --============================================= --Author: --Createdate: --Description:转换IP为十进制 --============================================= CreateFUNCTION[dbo].[X16ToDe] ( @Old_IPnvarchar(15) ) RETURNSnumeric AS BEGIN DECLARE @CharIndexINT, @CurrPointINT, @SingleValueNVARCHAR(5), @Cachenumeric SET@CharIndex=1 SET@CurrPoint=CHARINDEX('.',@Old_IP,@CharIndex) SET@SingleValue=SUBSTRING(@Old_IP,@CharIndex,@CurrPoint-@CharIndex) SET@Cache=cast(@SingleValueasnumeric)*16777216 SET@CharIndex=@CurrPoint+1 SET@CurrPoint=CHARINDEX('.',@CurrPoint-@CharIndex) SET@Cache=@Cache+cast(@SingleValueasnumeric)*65536 SET@CharIndex=@CurrPoint+1 SET@CurrPoint=CHARINDEX('.',@CurrPoint-@CharIndex) SET@Cache=@Cache+cast(@SingleValueasnumeric)*256 SET@CharIndex=@CurrPoint+1 SET@SingleValue=SUBSTRING(@Old_IP,len(@Old_IP)-@CharIndex+1) SET@Cache=@Cache+cast(@SingleValueasnumeric) RETURN@Cache; END 代码如下:--建立十进制新表 USE[BasName] GO /******对象:Table[dbo].[IP_Real]脚本日期:09/19/200714:01:31******/ SETANSI_NULLSON GO SETQUOTED_IDENTIFIERON GO CreateTABLE[dbo].[IP_Real]( [startip][numeric](18,0)NULL, [endip][numeric](18, [country][nvarchar](50)NULL, [local][nvarchar](200)NULL )ON[PRIMARY] 这一步你可以自己按照你的情况来做,我是为了加快数据库索引的速度,所以将IP全部转换为十进制,存到一个新表里面。 --格式化省份 代码如下:Update[BasName].[dbo].[IP] SET[country]=replace([country],N'省',N'省') --删除CZ88.NET 代码如下:Update[BasName].[dbo].[IP] SET[country]=replace([country],N'CZ88.NET',N'') --将地区提出 Update[BasName].[dbo].[IP] SET[local]=SUBSTRING([country],CHARINDEX('',[country],1)+1,len([country])) --存为国家或省份 Update[BasName].[dbo].[IP] SET[country]=SUBSTRING([country],1)) --去处前后导空格 Update[BasName].[dbo].[IP] SET[country]=Rtrim(Ltrim([country])) ,[local]=Rtrim(Ltrim([local])) --转换IP为十进制,并写入新表 InsertINTO[BasName].[dbo].[IP_Real] ([startip] ,[endip] ,[country] ,[local]) Selectdbo.X16ToDe([startip]) ,dbo.X16ToDe([endip]) ,[local] FROM[BasName].[dbo].[IP] orderby[startip]ASC 最后测试一下看看: 程序代码 --测试 DECLARE@IPNumbernumeric set@IPNumber=dbo.X16ToDe('219.140.31.91') Select[startip] ,[local] FROM[BasName].[dbo].[IP_Real] Where[startip]<=@IPNumberand[endip]>=@IPNumber 基本所有的转换最多3分钟完成,比自己写应用程序转换要快的多。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |