一、利用ESRI.ArcGIS.DataSourcesGDB连接PostgreSQL
环境准备
到ArcGis安装路径下查看可以支持的PostgreSQL版本
PostgreSQL安装配置
直连方式要求ArcSDE的客户端必须安装数据库的客户端类库,所以首先需要获取PostgreSQL的客户端。
但是ArcGIS Desktop是32位软件,需要的是32位的PostgreSQL类库。
另外需要将ArcGIS Desktop下数据库支持中的st_geometry.dll文件拷贝到PostgreSQL中的bin文件下
将PostgreSQL的bin目录下的 libeay32.dll;libiconv-2.dll;libintl-8.dll;libpq.dll;ssleay32.dll文件拷贝到Desktop 安装目录的bin目录下
PostgreSQL客户端下载链接
具体安装教程见https://www.yiibai.com/postgresql/install-postgresql.html
具体编程代码
Public Function OpensdeWorkspace(ByVal sServerName As String, ByRef sInstance As String, ByVal sUserName As String, ByVal sPassword As String, ByVal sDatabase As String, ByVal sVersionName As String) As IWorkspace
'ByVal Server As String,
Dim ws As IWorkspace = Nothing
Dim ws2 As IWorkspace2 = Nothing
Dim pProset As IPropertySet = New PropertySetClass
Dim pSqlFace As IWorkspaceFactory = New SdeWorkspaceFactoryClass()
pProset.SetProperty("SERVER", sServerName) '//博主这边是就是ip的值
pProset.SetProperty("INSTANCE", sInstance)
'// 实例 "sde:postgresql:" & "ip" & "," & "port"
pProset.SetProperty("DATABASE", sDatabase) '// 数据库名称
pProset.SetProperty("USER", sUserName) '// 用户名
pProset.SetProperty("PASSWORD", sPassword) '// 密码
pProset.SetProperty("VERSION", sVersionName) ' // 版本 默认SDE.DEFAULT
pProset.SetProperty("AUTHENTICATION_MODE", "DBMS")
Try
ws = pSqlFace.Open(pProset, 0)
Catch ex As Exception
MsgBox(ex.ToString & " sql连接失败 ")
End Try
Return ws
End Function
友情提醒:需要引用Imports ESRI.ArcGIS.DataSourcesGDB
二、利用Npsql连接PostgreSQL
安装Npgsql驱动
从网站http://pgfoundry.org/frs/?group_id=1000140&release_id=2106下载
下载后解压然后在项目中添加引用,点击浏览,找到解压所在路径,添加Npgsql.dll
具体代码
Public Sub Workspace2()
Dim NSd As NpgsqlDataReader = Nothing
Dim conn As NpgsqlConnection = New NpgsqlConnection("Server=ip;Port=port;User Id=用户名;Password=密码;Database=数据库名称")
Dim Command As NpgsqlCommand = New NpgsqlCommand("select * from 表名", conn)
conn.CreateCommand()
Try
conn.Open()
NSd = Command.ExecuteReader
Catch ex As Exception
MsgBox(ex.ToString & " sql连接失败 ")
End Try
'Dim str As String
'str = NSd.GetSchemaTable().Rows(1).Item(1).ToString()
'MsgBox(str & "hh ")
End Sub