欢迎您来到GIS动力

加入收藏 免费注册 用户登陆 帮助中心
首页 新闻动态 技术专栏 银杏树下 学习考研 软件下载 求职招聘 许愿瓶 节日祝福 用户中心 精彩推荐 资源搜索 地图
专栏导航: AO开发 | SO开发 | ArcGIS桌面 | 超图桌面 | 开发语言 | 数据库 | WebGIS | 银杏文学 | 研究生考题 | FreeMap FreeTalk
   您现在位于: 首页技术专栏ArcGIS应用与开发AO开发 → 正文
连接SQL Server的三种方法
07-12-18 00:00:00 作者:3s8 出处:3s8

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 中国3S吧 3s8.cn
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 专业的3S站 3s8.cn
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

(作者联系方式: liyanhenu@163.com )

(本文已被浏览 次)
发布人:admin
推荐给好友:发送给好友
上篇新闻:
下篇新闻:
相关评论
发表我的评论
  • 尊重网上道德,遵守《全国人大常委会关于维护互联网安全的决定》及中华人民共和国其他各项有关法律法;
  • 本站有权保留或删除您发表的任何评论内容;
  •   相关文章  
    ADO.net连接Sql2000时无法打开到 SQL Server 的连接解
    VC连接SQL SERVER数据库

    关于我们友情链接 ┋ 与我在线 ┋ 管理 ┋ TOP
     
    网站当前版本:GisPower CMS V3.0
    『GIS 动力』- http://www.gispower.org/
    联系我们:webmaster#gispower.org
    Copyright (c) 2003-2007 GisPOwer.Org. All Rights Reserved.
     

                   滇ICP备05006901号