gis
gis
管理员
管理员
  • 注册日期2003-07-16
  • 发帖数15947
  • QQ554730525
  • 铜币25339枚
  • 威望15364点
  • 贡献值0点
  • 银元0个
  • GIS帝国居民
  • 帝国沙发管家
  • GIS帝国明星
  • GIS帝国铁杆
阅读:1812回复:1

ArcMap连接SQL Server的三种方法

楼主#
更多 发布于:2003-08-11 18:23
程序在此,自己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

喜欢0 评分0
huangyhpig
路人甲
路人甲
  • 注册日期2003-08-21
  • 发帖数131
  • QQ
  • 铜币478枚
  • 威望0点
  • 贡献值0点
  • 银元0个
1楼#
发布于:2003-09-18 16:12
能否讲解一下,呵呵
举报 回复(0) 喜欢(0)     评分
游客

返回顶部