查询一个80万的IP地址数据库,VB程序会在查询的时候停顿,必须在查询完成后才能进行其它操作,请教高手如何处理?Function fwip(ipp)
Call LinkDB
Dim iCount As Long
Dim strUsers As String
Dim adoRs As New ADODB.Recordset
Dim strTemp As String
'ipp = "25.65.45.78"
If ipp <> "unknown" And ipp <> "" And InStr(ipp, ".") > 1 Then
sip = Split(ipp, ".")
ssip = sip(0) * 256 * 256 * 256 + sip(1) * 256 * 256 + sip(2) * 256 + sip(3)
SQL = ("select top 1 ip2,country,city from address where ip2 > " & ssip & " order by ip2 asc ")
adoRs.open SQL, Conns, 1, 1
If Not adoRs.EOF Then
Add = adoRs("country") & adoRs("city")
Else
Add = "未知地址"
End If
adoRs.Close
End If
Conns.Close
Set adoRs = Nothing
Set Conns = NothingEnd FunctionPrivate Function LinkDB() '连接数据库代码
On Error GoTo LinkDB_Err
Dim strConn As String
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & App.Path & "\msnabaga.mdb" '设置连接字串,连接数据库
Conns.open strConn
Exit FunctionLinkDB_Err:
MsgBox Err.Number & vbCrLf & Err.Description, , GstrAppTitle
End Function
Call LinkDB
Dim iCount As Long
Dim strUsers As String
Dim adoRs As New ADODB.Recordset
Dim strTemp As String
'ipp = "25.65.45.78"
If ipp <> "unknown" And ipp <> "" And InStr(ipp, ".") > 1 Then
sip = Split(ipp, ".")
ssip = sip(0) * 256 * 256 * 256 + sip(1) * 256 * 256 + sip(2) * 256 + sip(3)
SQL = ("select top 1 ip2,country,city from address where ip2 > " & ssip & " order by ip2 asc ")
adoRs.open SQL, Conns, 1, 1
If Not adoRs.EOF Then
Add = adoRs("country") & adoRs("city")
Else
Add = "未知地址"
End If
adoRs.Close
End If
Conns.Close
Set adoRs = Nothing
Set Conns = NothingEnd FunctionPrivate Function LinkDB() '连接数据库代码
On Error GoTo LinkDB_Err
Dim strConn As String
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & App.Path & "\msnabaga.mdb" '设置连接字串,连接数据库
Conns.open strConn
Exit FunctionLinkDB_Err:
MsgBox Err.Number & vbCrLf & Err.Description, , GstrAppTitle
End Function
解决方案 »
- 如何清除所画图形?
- 又来问问题来了?:)如何将vba文件生成exe文件。
- 动态数组问题
- 不能加载工程的解决方法
- 急、、在VB程序中如何能够禁止用户更改系统时间?
- 关于timer组件,解决给分
- 请问,在commondialog中怎么判断用户点选的是“确定”还是“取消”,谢谢
- 在VB中如何实现文字的上下滚动显示
- Access的中有类似于Oracle的事务处理机制吗?在线等待!
- 请大家看看我的一段实时曲线的代码出了什么问题?
- 哪位能给讲一下为什么函数调用中参数中 byval 变量名 这种格式是正确的 而用 byref 变量名 这种格式时提示 缺少表达式。
- 哪位能给讲一下为什么函数调用中参数中 byval 变量名 这种格式是正确的 而用 byref 变量名 这种格式时提示 缺少表达式。
这个查询语句一点效率都没有....
--> 生成测试数据: [tableip]
if object_id('[tableip]') is not null drop table [tableip]
create table [tableip] (id int,startip varchar(15),endip varchar(15),address nvarchar(6))
insert into [tableip]
select 1,'024.089.048.000','024.089.063.255','ARIN' union all
select 2,'024.089.064.000','024.089.127.255','加拿大' union all
select 3,'024.089.128.000','024.089.191.255','美国' union all
select 4,'024.089.192.000','024.089.255.255','加拿大' union all
select 5,'024.090.000.000','024.100.063.255','美国' union all
select 6,'024.100.064.000','024.103.255.255','ARIN' union all
select 7,'024.104.000.000','024.104.159.255','美国' union all
select 8,'024.104.160.000','024.104.255.255','ARIN'--SQL查询如下:go--这个函数不用改.直接搬着来用.
CREATE FUNCTION dbo.f_IP2Int(
@ip varchar(15)
)RETURNS bigint
AS
BEGIN
DECLARE @re bigint
SET @re=0
SELECT @re=@re+LEFT(@ip,CHARINDEX('.',@ip+'.')-1)*ID
,@ip=STUFF(@ip,1,CHARINDEX('.',@ip+'.'),'')
FROM(
SELECT ID=CAST(16777216 as bigint)
UNION ALL SELECT 65536
UNION ALL SELECT 256
UNION ALL SELECT 1)a
RETURN(@re)
END
GO--要查的IP
DECLARE @ip VARCHAR(15);SET @ip='24.93.121.22';SELECT *
FROM [tableip]
WHERE dbo.f_IP2Int(@ip)>=dbo.f_IP2Int(startip)
and dbo.f_IP2Int(@ip)<=dbo.f_IP2Int(endip) ;
GO--删除测试表,函数.
DROP TABLE [tableip]
DROP FUNCTION dbo.f_IP2Intid startip endip address
----------- --------------- --------------- -------
5 024.090.000.000 024.100.063.255 美国(1 行受影响)
我的应用范围纯真IP库.条数41W条
想快注意几点
1.IP1,IP2在数据库里建立索引
2.查询时不要用rs.open用set rs=con.execute()
这样查起来非常快。根据经验普通比较好的个人PC上能实现1秒钟10-20条查询
你上面的
SQL = ("select top 1 ip2,country,city from address where ip2 > " & ssip & " order by ip2 asc ")
这样写不对头
因为IP库都是这样的原理起始IP(ip1) 结束IP(ip2) 地区名 国家名无需top来取回多条记录
所以应该这样来
Set sRs = sCon.Execute("select country from IPLIST Where ip1<=" & rIP & " And ip2>=" & rIP)上面提的都注意到了每秒上10条是很容易的