阅读:1812回复:1
ArcMap连接SQL Server的三种方法
程序在此,自己look啦,呵呵!
Private Sub txtCollectID_AfterUpdate_thru_Access() 'This method uses an Access .mdb that has a link to the SQL Server Collection table through ODBC 'It checks to see whether a record exists in the Collections db already based on the Collection field On Error GoTo MyError dblCollID = Val(txtCollectID.Value) Dim m_adoCon As ADODB.Connection Set m_adoCon = New ADODB.Connection Dim strSQL As String Dim m_accWS As IWorkspace Dim sPath As String 'Modify the path to the .mdb file accordingly sPath = "C:\Kevin\Arc_VBA_testing\VAFWIS.mdb" 'Verify that file exists If Dir(sPath) = "" Then Debug.Print "file not found : " & sPath Exit Sub End If m_adoCon.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source = C:\Kevin\Arc_VBA_testing\VAFWIS.mdb" m_adoCon.Open Dim ADOrst As ADODB.Recordset Set ADOrst = New ADODB.Recordset Set ADOrst.ActiveConnection = m_adoCon strSQL = "Select * from dbo_Collections where Collection = " & dblCollID ADOrst.Open strSQL, m_adoCon, adOpenForwardOnly, adLockOptimistic If (Not ADOrst.EOF) Then frmVAFWIS.lblNotifyYes.Visible = True Else frmVAFWIS.lblNotifyNo.Visible = True End If ADOrst.Close Set ADOrst = Nothing m_adoCon.Close Set m_adoCon = Nothing MyError: If Err.Number <> 0 Then MsgBox "There was an error " & Err.Description End If End Sub Private Sub txtCollectID_AfterUpdate_thru_SQL_Server() 'This method uses a more direct connection to the SQL Server Collection table through OLE DB 'It checks to see whether a record exists in the Collections db already based on the Collection field On Error GoTo MyError dblCollID = Val(txtCollectID.Value) '++ Create and populate a new property set Dim pPropset As IPropertySet Set pPropset = New PropertySet pPropset.SetProperty "CONNECTSTRING", "Provider=SQLOLEDB;Data source=CARL;Initial Catalog=VAFWIS;User ID=sa;Password=gis" '++ Create a new workspacefactory/workspace Dim pFeatureWorkspace As IFeatureWorkspace Dim pWorkspaceFact As IWorkspaceFactory Set pWorkspaceFact = New OLEDBWorkspaceFactory Set pFeatureWorkspace = pWorkspaceFact.Open(pPropset, 0) 'Open the table Dim pTable As ITable Set pTable = pFeatureWorkspace.OpenTable("Collections") 'Set up the query Dim pQueryFilter As IQueryFilter Set pQueryFilter = New QueryFilter pQueryFilter.WhereClause = "Collection = " & dblCollID If pTable.RowCount(pQueryFilter) > 0 Then frmVAFWIS.lblNotifyYes.Visible = True Else frmVAFWIS.lblNotifyNo.Visible = True End If MyError: If Err.Number <> 0 Then MsgBox "There was an error " & Err.Number End If End Sub Private Sub txtCollectID_AfterUpdate_thru_ArcCatalog() 'This method use a link to the SQL Server Collection table created through ArcCatalog through OLE DB 'It checks to see whether a record exists in the Collections db already based on the Collection field dblCollID = Val(txtCollectID.Value) On Error GoTo MyError Dim pFeatureWorkspace As IFeatureWorkspace Dim pWorkspaceFact As IWorkspaceFactory Set pWorkspaceFact = New OLEDBWorkspaceFactory Dim pWorkspace As IWorkspace '-- connection file Dim sFile As String sFile = "C:\Documents and Settings\gis\Application Data\ESRI\ArcCatalog\ConnectionToCarl.odc" Set pFeatureWorkspace = pWorkspaceFact.OpenFromFile(sFile, 0) Dim pTable As ITable Set pTable = pFeatureWorkspace.OpenTable("tblSQL") 'Set up the query Dim pQueryFilter As IQueryFilter Set pQueryFilter = New QueryFilter pQueryFilter.WhereClause = "Collection = " & dblCollID If pTable.RowCount(pQueryFilter) > 0 Then frmVAFWIS.lblNotifyYes.Visible = True Else frmVAFWIS.lblNotifyNo.Visible = True End If Exit Sub MyError: If Err.Number <> 0 Then MsgBox "There was an error " & Err.Number End If End Sub |
|
1楼#
发布于:2003-09-18 16:12
能否讲解一下,呵呵
|
|