[讨论]怎样提高VB+SQL数据处理速度
<p>我用VB将CAD图形中的图元信息写入到SQL中,当数据量大的时候感觉特慢,请问有什么方法优化吗?下面是我的代码:</p><p><br/>Option Explicit</p><p>Dim acadApp As AcadApplication </p><p>Dim acadDoc As AcadDocument </p><p>'Dim acaddoc As AcadModelSpace<br/>Const LB_ITEMFROMPOINT = &H1A9</p><p>Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _<br/> (ByVal hWnd As Long, ByVal wMsg As Long, _<br/> ByVal wParam As Long, lParam As Any) As Long<br/>Private Sub Command1_Click()<br/>'On Error Resume Next<br/><br/> Set acadApp = GetObject(, "AutoCAD.Application.16")<br/> If Err Then<br/> MsgBox "AUTOCAD图形软件未打开!"<br/> End<br/> End If<br/> </p><p> If Err Then<br/> Err.Clear<br/> ' 创建一个新的AutoCAD应用程序对象<br/> Set acadApp = CreateObject("AutoCAD.Application.16")<br/> <br/> If Err Then<br/> MsgBox Err.Description<br/> Exit Sub<br/> End If<br/> End If<br/> <br/> ' 显示AutoCAD应用程序<br/> acadApp.Visible = True<br/> Set acadDoc = acadApp.ActiveDocument<br/> Dim cn As New ADODB.Connection<br/> acadDoc.SendCommand "zoom" & vbCr & "e" & vbCr</p><p>Frame1.Visible = False<br/>Frame2.Visible = False<br/>Command1.Visible = False<br/>Command2.Visible = False<br/>Line2.Visible = False<br/>Line3.Visible = False<br/>Line1.Visible = False<br/> Dim gdp As New ADODB.Recordset<br/> Dim gdpp As New ADODB.Recordset<br/> Dim gdeo As New ADODB.Recordset<br/> Dim gdpv As New ADODB.Recordset<br/> Dim gdv33 As New ADODB.Recordset<br/> Dim gdv3 As New ADODB.Recordset<br/> Dim gdlp As New ADODB.Recordset<br/> Dim gg01 As New ADODB.Recordset<br/> Dim gg03 As New ADODB.Recordset<br/> Dim tdsyz As String, tdzl As String, qsxz As String, sjyt As String<br/> Dim sqllj As String, gdv3lj As String, gg01lj As String, gg03lj As String, gdplj As String<br/> Dim gdlplj, gdpvlj As String<br/> Dim ftype(0 To 2) As Integer<br/> Dim fdata(0 To 2) As Variant<br/> Dim gdsx As Variant<br/> Dim maxvid, maxvidd As Long, vid As Long, eoid As Long, pid As Long<br/> Dim maxga01 As String, newga01 As String<br/> Dim maxpid As Long, newpid As Long, maxvn As Long, newvn As Long<br/> Dim zdpx(), zdpxgd(), t() As Variant '宗地排序数组<br/> Dim jfmj As Double '街坊面积<br/> Dim ltime As String<br/> Dim djh, zdh, jfh As String<br/> Dim zds As Integer '宗地数<br/> sjyt = Trim(Text7.Text)<br/> tdsyz = Trim(Text6.Text)<br/> qsxz = Trim(Text8.Text)<br/> <br/> ltime = Date + Time<br/> ftype(0) = 0: fdata(0) = "LWPOLYLINE"<br/> ftype(1) = 8: fdata(1) = "界址线"<br/> ftype(2) = 70: fdata(2) = 1<br/> 'ftype(2) = 70: fdata(2) = 1<br/> Dim ddzb() As Double '顶点坐标<br/> Dim dds, mode, jzds As Integer<br/> Dim zb As Variant<br/> Dim i As Integer<br/> sqllj = "provider=sqloledb.1;password=" & Text4.Text & " ;persist security info=true;user id=" & Trim(Text3.Text) & ";initial catalog=" & Text2.Text & " ;data source=" & Text1.Text & ""<br/> cn.Open sqllj<br/> jfh = Trim(Text5.Text)<br/> gdv3lj = "select maxvid=max(vid) from gdv3"<br/> gdv3.Open gdv3lj, cn, adOpenForwardOnly, adLockBatchOptimistic<br/> Do While Not gdv3.EOF<br/> maxvid = gdv3.Fields("maxvid")<br/> gdv3.MoveNext<br/> Loop<br/>gdv3.Close</p><p>gdplj = "select maxpid=max(pid), js=count(pid) from gdp"<br/>gdp.Open gdplj, cn, adOpenForwardOnly, adLockBatchOptimistic<br/>If Not gdp.EOF Then<br/>If gdp.Fields("js") > 0 Then<br/> maxpid = gdp.Fields("maxpid")<br/> Else<br/> maxpid = 0<br/> End If<br/>End If<br/>gdp.Close</p><p> gg01.Open "select maxga01=max(ga01)from gg01 where ga01 like '200%'", cn, adOpenForwardOnly, adLockBatchOptimistic<br/> If Not gg01.EOF Then<br/> 'If gg01.Fields("maxga01") <> Null Then<br/> maxga01 = gg01.Fields("maxga01")<br/> ' Else<br/> ' maxga01 = "2007320506100000"<br/> ' End If<br/> <br/> End If<br/> gg01.Close<br/> <br/> newga01 = pdga01(maxga01)<br/> gdeo.Open "select * from gdeo where description like " & jfh & " ", cn, adOpenForwardOnly, adLockBatchOptimistic<br/> If Not gdeo.EOF Then<br/> eoid = gdeo.Fields("eoid")<br/> Else<br/> MsgBox "数据库中没有本街坊信息、请查实!“"<br/> End<br/> End If<br/> gdeo.Close</p><p> gdv3lj = "select maxvn=max(vn) , js =count(vn)from gdv3 where eoid=" & eoid & " and vt='100' and vid in (select pvid from gdpv where pid in (select lpid from gdlp where isvirtual=0))"</p><p> gdv3.Open gdv3lj, cn, adOpenForwardOnly, adLockBatchOptimistic<br/> If Not gdv3.EOF Then<br/> If gdv3.Fields("js") > 0 Then<br/> maxvn = gdv3.Fields("maxvn")<br/> End If<br/> 'gdv3.MoveNext<br/> End If<br/>gdv3.Close</p><p> i = 0<br/>On Error Resume Next<br/> Dim xzj As AcadSelectionSet<br/> If Not IsNull(acadDoc.SelectionSets.Item("st")) Then<br/> Set xzj = acadDoc.SelectionSets.Item("st")<br/> xzj.Delete<br/> End If<br/> Set xzj = acadDoc.SelectionSets.Add("st") '新建选择集<br/> 'MsgBox xzj.Name<br/> xzj.Select acSelectionSetAll, , , ftype, fdata '选择宗地<br/> ReDim zdpxgd(xzj.count - 1, 3)<br/> 'ReDim zdpxgd(xzj.Count - 1, 1)<br/> ReDim t(xzj.count - 1, 3)<br/> If xzj.count = 0 Then<br/> MsgBox "没有界址线!"<br/> End<br/> Else<br/> Dim ty As AcadEntity<br/> For Each ty In xzj<br/> If ty.Close = True Then<br/> dds = (UBound(ty.Coordinates) + 1) / 2<br/> zb = ty.Coordinates<br/> jfmj = ty.Area<br/> ReDim ddzb(dds * 3 - 1)<br/> ReDim sjzb(dds - 1, 1)<br/> For i = 0 To dds - 1<br/> 'MsgBox ty.Coordinates(2 * i)<br/> ddzb(3 * i) = zb(2 * i)<br/> ddzb(3 * i + 1) = zb(2 * i + 1)<br/> ddzb(3 * i + 2) = 0<br/> sjzb(i, 0) = zb(2 * i)<br/> sjzb(i, 1) = zb(2 * i + 1)<br/> Next i<br/>gdsx = tqjfh(ddzb)<br/>zdh = gdsx(0)<br/>tdzl = gdsx(1)<br/> If zdh <> "" Then<br/> jfh = Trim(Text5.Text)<br/> djh = jfh + zdh<br/> 'newpid = maxpid + 1<br/>zdpxgd(zds, 0) = zdh<br/>zdpxgd(zds, 1) = ddzb<br/>zdpxgd(zds, 2) = jfmj<br/>zdpxgd(zds, 3) = tdzl<br/>zds = zds + 1<br/>End If<br/>Else<br/> MsgBox "本宗地界址线没有封闭!请检查!"<br/> End If<br/> Next<br/> End If<br/> '以上提取宗地号和宗地界址线坐标表。<br/> '以下对宗地号由大到小排序。<br/> Dim sd, sdd, j, ii As Integer<br/>For i = 1 To zds - 1<br/> For j = i - 1 To zds - 1<br/> sd = Val(zdpxgd(i - 1, 0))<br/> sdd = Val(zdpxgd(j, 0))<br/> If sd > sdd Then<br/> t(i, 0) = zdpxgd(i - 1, 0)<br/> t(i, 1) = zdpxgd(i - 1, 1)<br/> t(i, 2) = zdpxgd(i - 1, 2)<br/> t(i, 3) = zdpxgd(i - 1, 3)<br/> zdpxgd(i - 1, 0) = zdpxgd(j, 0)<br/> zdpxgd(i - 1, 1) = zdpxgd(j, 1)<br/> zdpxgd(i - 1, 2) = zdpxgd(j, 2)<br/> zdpxgd(i - 1, 3) = zdpxgd(j, 3)<br/> zdpxgd(j, 0) = t(i, 0)<br/> zdpxgd(j, 1) = t(i, 1)<br/> zdpxgd(j, 2) = t(i, 2)<br/> zdpxgd(j, 3) = t(i, 3)<br/> End If<br/> Next j<br/> Next i<br/> '宗地号由小到大排序完毕。<br/> ' newga01 = pdga01(maxga01)<br/> '新增宗地数据开始上传<br/>Bar1.Top = 1300<br/> Bar1.Width = 2500<br/> Bar1.Left = 1000<br/> Bar1.Min = 0<br/> Bar1.Max = zds<br/> For ii = 0 To zds - 1<br/> 'newpid = maxpid + 1<br/>Bar1.Value = ii<br/> <br/> djh = jfh + zdpxgd(ii, 0)<br/> gdplj = "select * from gdp where pid in (select ga33 from gg01 where ga18 = '" & djh & "' )"<br/> gdp.Open gdplj, cn, adOpenDynamic, adLockPessimistic<br/> If gdp.EOF Then<br/> 'cn.Execute "set identity_insert gdp on"<br/> 'cn.Execute "insert into gdp values( " & newpid & ", '100' ,'" & zdpxgd(ii, 0) & " '," & eoid & " ,0,0,0,0.0,0.0,0,0.0, 0,'" & ltime & " '," & zdpxgd(ii, 2) & ",1)"<br/> cn.Execute "insert into gdp values( '100' ,'" & zdpxgd(ii, 0) & " '," & eoid & " ,0,0,0,0.0,0.0,0,0.0, 0,'" & ltime & " '," & zdpxgd(ii, 2) & ",1)"<br/> gdpp.Open "select maxpid=max(pid) from gdp ", cn, adOpenDynamic, adLockPessimistic<br/> If Not gdpp.EOF Then<br/> newpid = gdpp.Fields("maxpid")<br/> End If<br/> gdpp.Close<br/> cn.Execute "insert into gdlp values(" & newpid & ",'" & sjyt & "','" & tdsyz & "','" & newga01 & "',1,0,0.0,0.0,1)"<br/> 'cn.Execute "insert into gg01 values( '" & newga01 & " ','0','','','" & Left(djh, 6) & " ','" & Mid(djh, 7, 3) & " ','" & Mid(djh, 10, 3) & " ','" & zdh & " ','" & ltime & " ','002','','','','','','','','','','','','" & djh & " ',' ',' ',' ','0','0',' ',' ',' ',' ','Y',0,0,'',0," & maxpid + 1 & ",' Y ',0,0,0,'" & jmj & "',0,'','0',0)"<br/> gg01.Open "select * from gg01", cn, adOpenDynamic, adLockPessimistic<br/> ' If gg01 Then<br/> gg01.AddNew<br/> gg01.Fields("ga01") = newga01<br/> gg01.Fields("ga02") = "0"<br/> gg01.Fields("ga03") = tdsyz<br/> gg01.Fields("ga031") = Left(djh, 6)<br/> gg01.Fields("ga032") = Mid(djh, 7, 3)<br/> gg01.Fields("ga033") = Mid(djh, 10, 3)<br/> gg01.Fields("ga034") = zdpxgd(ii, 0)<br/> gg01.Fields("ga04") = ""<br/> gg01.Fields("ga05") = ltime<br/> gg01.Fields("ga051") = "002"<br/> gg01.Fields("ga06") = ""<br/> gg01.Fields("ga07") = ""<br/> gg01.Fields("ga08") = zdpxgd(ii, 3)<br/> gg01.Fields("ga09") = ""<br/> 'gg01.Fields("ga10") = ""<br/> gg01.Fields("ga11") = ""<br/> gg01.Fields("ga12") = ""<br/> gg01.Fields("ga13") = ""<br/> gg01.Fields("ga14") = ""<br/> gg01.Fields("ga15") = ""<br/> gg01.Fields("ga16") = "1"<br/> gg01.Fields("ga17") = ""<br/> gg01.Fields("ga18") = Left(djh, 6) + Mid(djh, 7, 3) + Mid(djh, 10, 3) + zdpxgd(ii, 0)<br/> gg01.Fields("ga19") = ""<br/> gg01.Fields("ga21") = Null<br/> gg01.Fields("ga22") = Null<br/> gg01.Fields("ga23") = 0<br/> gg01.Fields("ga24") = Val(sjyt)<br/> gg01.Fields("ga25") = ""<br/> gg01.Fields("ga251") = ""<br/> gg01.Fields("ga26") = ""<br/> gg01.Fields("ga27") = ""<br/> gg01.Fields("ga28") = "Y"<br/> gg01.Fields("ga29") = ""<br/> gg01.Fields("ga30") = 0<br/> gg01.Fields("ga31") = ""<br/> gg01.Fields("ga32") = "N"<br/> gg01.Fields("ga33") = newpid<br/> gg01.Fields("ga34") = "Y"<br/> gg01.Fields("ga35") = Null<br/> gg01.Fields("ga36") = Null<br/> gg01.Fields("ga37") = Null<br/> gg01.Fields("ga221") = zdpxgd(ii, 2)<br/> gg01.Fields("ga222") = Null<br/> gg01.Fields("ga38") = Null<br/> gg01.Fields("ga39") = Null<br/> gg01.Fields("ga381") = Null<br/> gg01.Update<br/> gg01.MoveNext<br/> 'End If<br/> <br/> gg01.Close<br/> <br/> zb = zdpxgd(ii, 1)<br/> jzds = (pdws(zb) + 1) / 3<br/> For i = 0 To jzds - 1<br/> ' For i = 0 To zdpxgd(ii, 1).cone<br/> gdv3lj = "select * from gdv3 where x=" & zb(3 * i + 1) & " and y= " & zb(3 * i) & " and vt=100"<br/> gdv3.Open gdv3lj, cn, adOpenForwardOnly, adLockBatchOptimistic<br/> <br/> If Not gdv3.EOF Then<br/> vid = gdv3.Fields("vid")<br/> newvn = gdv3.Fields("vn")<br/> Else<br/> vid = maxvid + 1<br/> newvn = maxvn + 1<br/> maxvn = maxvn + 1<br/> cn.Execute "set identity_insert gdv3 on"<br/> cn.Execute "insert into gdv3 values ( " & eoid & ",' " & newvn & "'," & zb(3 * i + 1) & "," & zb(3 * i) & ",0,2,1,1,0,'" & ltime & "',100)"<br/> End If<br/> gdv33.Open "select maxvid=max(vid) from gdv3 ", cn, adOpenForwardOnly, adLockBatchOptimistic<br/> If Not gdv33.EOF Then<br/> maxvidd = gdv33.Fields("maxvid")<br/> If maxvidd > maxvid Then<br/> vid = maxvidd<br/> End If<br/> maxvid = maxvidd<br/> End If<br/> gdv33.Close<br/> <br/> ' cn.Execute "set identity_insert gdeov on"<br/> <br/> cn.Execute "insert into gdpv values (" & newpid & "," & i + 1 & "," & vid & ")"<br/> cn.Execute "insert into gg03 values (" & newga01 & "," & i + 1 & "," & i + 1 & " ," & newvn & ",null,4,null,null,2,3,null)"<br/> ' maxvid = maxvid + 1<br/> 'maxvn = maxvn + 1<br/> gdv3.Close<br/> Next<br/> <br/> maxpid = maxpid + 1<br/> newga01 = pdga01(newga01)<br/> 'MsgBox ty.ObjectName</p><p> End If 'If gdp.EOF Then<br/> gdp.Close<br/> Next<br/> MsgBox "数据上传完毕!"<br/> End<br/> End<br/> <br/> <br/> <br/> <br/>End Sub</p><p>Function tqjfh(xxzb) As Variant '提取宗地号、土地坐落<br/> On Error Resume Next<br/> Dim jfh(0 To 1) As String<br/> Dim zjxzj As AcadSelectionSet<br/> If Not IsNull(acadDoc.SelectionSets.Item("zjst")) Then<br/> Set zjxzj = acadDoc.SelectionSets.Item("zjst")<br/> zjxzj.Delete<br/> End If<br/> Set zjxzj = acadDoc.SelectionSets.Add("zjst") '新建选择集<br/> Dim ftype As Variant, fdata As Variant<br/> Call creatssetfilter(ftype, fdata, 0, "Text", 8, "宗地注记,土地座落")<br/> 'ReDim gpCode(0 To 1) As Integer<br/> ''gpCode(0) = 0<br/> ''gpCode(1) = 8<br/> '' ReDim dataValue(0 To 1) As Variant<br/> ''dataValue(0) = "TEXT"<br/> '' dataValue(1) = "宗地注记"<br/> <br/> '' Dim groupCode As Variant, dataCode As Variant<br/> '' groupCode = gpCode<br/> '' dataCode = dataValue<br/> <br/> zjxzj.SelectByPolygon acSelectionSetWindowPolygon, xxzb, ftype, fdata<br/> If zjxzj.count = 0 Then<br/> MsgBox "没有宗地号,请检查!"<br/> ' End<br/> Else<br/> <br/> 'zjxzj.SelectByPolygon acSelectionSetWindowPolygon, pointsArray, groupCode, dataCode<br/> If zjxzj.Item(0).Layer = "宗地注记" Then<br/> jfh(0) = zjxzj.Item(0).TextString '<br/> jfh(1) = zjxzj.Item(1).TextString<br/> Else<br/> jfh(1) = zjxzj.Item(0).TextString '<br/> jfh(0) = zjxzj.Item(1).TextString<br/> End If<br/> <br/> End If<br/> <br/> '以上提取街坊坐标和街坊号<br/>tqjfh = jfh<br/>zjxzj.Delete<br/>End Function<br/>Function pdga01(maxga01) As String '判断调查表号<br/>Dim hm As String<br/>hm = Val(Str$(Right(maxga01, 6)) + 1)<br/>Select Case Len(hm)<br/> Case 1: hm = "00000" + hm<br/> Case 2: hm = "0000" + hm<br/> Case 3: hm = "000" + hm<br/> Case 4: hm = "00" + hm<br/> Case 5: hm = "0" + hm<br/> End Select<br/>pdga01 = Left(maxga01, 10) + hm</p><p>End Function<br/>Function pdws(mArray As Variant) As Integer<br/>Dim i As Integer<br/>Dim Ret As Integer<br/>Dim ErrF As Boolean<br/>Dim arrayrange As Integer<br/>ErrF = False<br/>On Error GoTo ErrHandle<br/>'判断代入的参数是否为数组<br/>If Not IsArray(mArray) Then<br/>pdws = -1<br/>Exit Function<br/>End If<br/>'VB中数组最大为60<br/>For i = 1 To 60<br/>'用UBound函数判断某一维的上界,如果大数组的实际维数时产生超出范围错误,<br/>' 此时我们通过Resume Next 来捕捉错这个错误<br/>Ret = UBound(mArray, i)<br/>If ErrF Then Exit For<br/>Next i<br/>'最后返回<br/>arrayrange = Ret<br/>Exit Function<br/>ErrHandle:<br/>'Ret = i - 1<br/>'ErrF = True<br/>'Resume Next<br/>pdws = Ret<br/>End Function</p><p>Private Sub CommandButton2_Click()<br/>End<br/>End Sub</p><p></p><p><br/>Private Sub Command2_Click()<br/>End<br/>End Sub<br/>Public Sub creatssetfilter(ByRef filtertype As Variant, ByRef filterdata As Variant, ParamArray filter()) '选择集过滤器<br/>If UBound(filter) Mod 2 = 0 Then<br/> MsgBox "filter 参数无效"<br/> Exit Sub<br/>End If<br/>Dim ftype() As Integer<br/>Dim ftada() As Variant<br/>Dim count As Integer<br/>count = (UBound(filter) + 1) / 2<br/>ReDim ftype(count - 1)<br/>ReDim fdata(count - 1)<br/>Dim i As Integer<br/>For i = 0 To count - 1<br/> ftype(i) = filter(2 * i)<br/> fdata(i) = filter(2 * i + 1)<br/> Next i<br/> filtertype = ftype<br/> filterdata = fdata<br/>End Sub<br/></p> 不了解,帮顶
页:
[1]