问题描述:
通过C# 基于ArcEngine写ArcSDE连接(直连)的时候测试项目连接属性设置为如下:
tPropSet.SetProperty("User", "GISDATA"); tPropSet.SetProperty("Password", "GISDATA"); tPropSet.SetProperty("Instance", "sde:oracle11g:127.0.0.1/ORCL");
测试连接通过,但放到我的程序时刚开始是可以测试通过的,突然就测试失败了,于是乎我就添加属性参数如下:
pPropertySet.SetProperty("SERVER", "127.0.0.1"); pPropertySet.SetProperty("Database", orcl);
结果测试项目依然可以通过,但我的正式程序还是通不过("ORA-12560: TNS: 协议适配器错误"错误,但我Oracle服务器及客户端设置正常)
最后查找资料,发现直连SDE还有一种写法,修改后放到我的正式程序中执行顺利通过 代码如下:
pPropertySet.SetProperty("AUTHENTICATION_MODE", "DBMS"); pPropertySet.SetProperty("DB_CLIENT", "ORCL"); pPropertySet.SetProperty("IS_GEODATABSE", "true"); pPropertySet.SetProperty("DB_CONNECTION_PROPERTIES", "ORCL"); pPropertySet.SetProperty("SERVER", "127.0.0.1"); //1、oracle$后是客户端配置的实例名时 IP(SERVER)可以不设置 随便设置一个IP也可以 //2、oracle$后是服务器IP【:端口(如1521)】/服务器实例名 时 IP(SERVER)一定要填且必须对 // sde:oracle$orcl(客户端配置的实例名) 或者 sde:oracle$127.0.0.1[:1521]/orcl pPropertySet.SetProperty("INSTANCE", "sde:oracle$orcl"); pPropertySet.SetProperty("USER", "GISDATA"); pPropertySet.SetProperty("PASSWORD", "GISDATA"); //pPropertySet.SetProperty("VERSION", "SDE.DEFAULT");//默认版本可以不设置 tWorkspace = tWorkspaceFactory.Open(pPropertySet, 0);
总结:
1、直接通过Oracle Client 配置的服务名直连
在ArcCatalog中设置 可以直接设置客服端服务名orcl156连接成功后自动转成sde:oracle11g:orcl156。但该方式再ArcEngine中不支持 sde:oracle11g:orcl156这种写法。
如用代码想用Oracle Client 配置的服务名直连的话可以用 "sde:oracle$orcl156",详情见上面的实例代码。
2、直接通过Oracle 服务器实例名直连
2.1 ArcCatalog 10.2不写sde:oracle11g: 程序会默认加上
127.0.0.1[:1521]/orcl——>sde:oracle11g:127.0.0.1[:1521]/orcl (备注:ArcCatalog可以设置默认端口号)
2.2 采用IPropertySet设置连接参数
方式一:
IPropertySet pPropertySet = new PropertySetClass(); IWorkspaceFactory tWorkspaceFactory = new SdeWorkspaceFactoryClass(); //默认端口号1521时不能写,写了会报错 // Instance必须以 “sde:oracle11g:”开头 //tPropSet.SetProperty("Instance", "127.0.0.1/orcl");//报错 //tPropSet.SetProperty("Instance", "127.0.0.1:1521/orcl");//报错 tPropSet.SetProperty("Instance", "sde:oracle11g:127.0.0.1[:1522]/orcl"); tPropSet.SetProperty("Password", "123"); tPropSet.SetProperty("User", "gisdata"); tPropSet.SetProperty("Server", "127.0.0.1"); tPropSet.SetProperty("Database", "ORCL"); IWorkspace wrkspace =tWorkspaceFactory.Open(pPropertySet, 0);
方式二:
IPropertySet pPropertySet = new PropertySetClass(); IWorkspaceFactory tWorkspaceFactory = new SdeWorkspaceFactoryClass(); pPropertySet.SetProperty("INSTANCE", "sde:oracle11g"); pPropertySet.SetProperty("USER", "GISDATA"); //pPropertySet1.SetProperty("PASSWORD", "GISDATA@127.0.0.1");//不可以 //pPropertySet1.SetProperty("PASSWORD", "GISDATA@127.0.0.1:1521/orcl");//加上端口就报密码错误 pPropertySet.SetProperty("PASSWORD", "GISDATA@127.0.0.1/orcl");//可以 IWorkspace workspace = tWorkspaceFactory.Open(pPropertySet, 0);
方式三 该方式连接SDE最稳定
pPropertySet.SetProperty("AUTHENTICATION_MODE", "DBMS"); pPropertySet.SetProperty("DB_CLIENT", "ORCL"); pPropertySet.SetProperty("IS_GEODATABSE", "true"); pPropertySet.SetProperty("DB_CONNECTION_PROPERTIES", "ORCL"); pPropertySet.SetProperty("SERVER", "127.0.0.1"); //1、oracle$后是客户端配置的实例名时 IP(SERVER)可以不设置 随便设置一个IP也可以 //2、oracle$后是服务器IP【:端口(如1521)】/服务器实例名 时 IP(SERVER)一定要填且必须对 // sde:oracle$orcl(客户端配置的实例名) 或者 sde:oracle$127.0.0.1[:1521]/orcl(默认端口不报错) pPropertySet.SetProperty("INSTANCE", "sde:oracle$orcl"); pPropertySet.SetProperty("USER", "GISDATA"); pPropertySet.SetProperty("PASSWORD", "123"); //pPropertySet.SetProperty("VERSION", "SDE.DEFAULT");//默认版本可以不设置 tWorkspace = tWorkspaceFactory.Open(pPropertySet, 0);
3. 连接Sql Server数据库
3.1 ArcCatalog 10.2
连接SQL Server的连接字符串 实例:127.0.0.1,1433[MSSQLSER ] MSSQLSER写错或不写都可以连上 。实例前不能加 sde:sqlserver: 否则连不上。
3.2 ArcEngine
方式1:采用IPropertySet设置连接参数
实例(Instance)必须以 "sde:sqlserver:"开头,否则程序不知道连接的数据库类型。
tPropSet.SetProperty("Database", "GISDATA"); //MSSQLSERVER可以不设置,写错了也是可以连接的 //sde:sqlserver:127.0.0.1,1433[MSSQLSERVER] tPropSet.SetProperty("Instance","sde:sqlserver:127.0.0.1,1433MSSQLSERVER"); tPropSet.SetProperty("Password", "123"); tPropSet.SetProperty("User", "sa"); //如果设置几个还报错接着可以增加参数设置 //tPropSet.SetProperty("IS_GEODATABSE", "true"); //tPropSet.SetProperty("AUTHENTICATION_MODE", "DBMS"); //tPropSet.SetProperty("Version", "DBO.DEFAULT"); //注意SQL Server数据库SDE默认的版本不是SDE.DEFAULT而是DBO.DEFAULT //tPropSet.SetProperty("Server", "127.0.0.1");
方式2:采用SqlWorkspaceFactory接口的OpenFromString方法来连接SDE数据库
//dbclient=SQLServer;serverinstance=127.0.0.1,1433MSSQLSERVER;database=GISDATA;user=sa;password=123 Type factoryType = Type.GetTypeFromProgID("esriDataSourcesGDB.SqlWorkspaceFactory"); IWorkspaceFactory2 workspaceFactory2 = (IWorkspaceFactory2)Activator.CreateInstance(factoryType); String[] connectionProps = { "dbclient=SQLServer", @"serverinstance=127.0.0.1,1433MSSQLSERVER", "database=GISDATA", "user=sa","password=123" }; String connString = String.Join(";", connectionProps); // Open the workspace. IWorkspace workspace = workspaceFactory2.OpenFromString(connString, 0);
4 各版本的ArcCatalog连接SDE数据库
4.1 ArcGIS 9.3版本
INSTANCE sde:oracle11g (oracle9i oralce10g,服务器的 oracle版本)PASSWORDpass@198 (198,oraclenetmanager配置的 服务命名)
或者如格式:
INSTANCEsde:oracle11g
PASSWORDsde@192.168.1.198:1521/orcl
通过AE来实现的时候,不可以通过RDBMS来进行连接,而应该还是通过SDEWorkspace来进行
IWorkspaceFactory pWorkspaceFactory = new SdeWorkspaceFactoryClass(); IPropertySet propSet = new PropertySetClass(); propSet.SetProperty("SERVER", "192.168.16.46"); propSet.SetProperty("INSTANCE", "sde@oracle10g"); propSet.SetProperty("USER", "system"); propSet.SetProperty("PASSWORD", "****@orc"); propSet.SetProperty("VERSION", "SDE.DEFAULT"); IWorkspace pWorkspace = pWorkspaceFactory.Open(propSet, 0); IFeatureWorkspace pFeatWS = pWorkspace as IFeatureWorkspace;
4.2 ArcGIS 10.1版本
1.INSTANCE sde:oracle11g:192.168.1.198/orcl (oracle10g或 oracle9i)(数据库是默认端口号1521时)
INSTANCE sde:oracle11g:192.168.1.198:1622/orcl(数据库不是 是默认端口号1521,而是1622时)
如果数据库端口是默认端口1521则ip后不能带端口号,否则报错;
如果不是默认端口1521,则需要带上端口如: sde:oracle11g:192.168.1.198:1622/orcl
2.INSTANCE sde:oracle11g:198
其中198为oracleCilent Net Manager配置的名称
3.INSTANCEsde:oracle11gPASSWORDpass@198
在我们系统中instance对应的是服务
4.2 ArcGIS 10.2版本
sde:oracle11g:127.0.0.1:1521/orcl 默认端口号1521写上也不会报错,不写也不会报错
sde:oracle11g:127.0.0.1:1521/orcl
sde:oracle11g:orclclient orclclient客户端设置的服务名(oracleCilent Net Manager配置的名称) //ArcEnginge不支持此种写好 但支持这种写法sde:oracle$orclclient
//sde:oracle11g:127.0.0.1:1521/orcl 报错 tPropSet.SetProperty("Instance", "sde:oracle11g:127.0.0.1/orcl"); tPropSet.SetProperty("Instance", "127.0.0.1/orcl");//必须加 sde:oracle11g:
SQL Server
INSTANCE 127.0.0.1
127.0.0.1,1433
127.0.0.1,1433MSSQLSERVER 端口一旦不是1433时必须写,MSSQLSERVER写错也可以连上
SQL Server 不支持sde:SqlServer:127.0.0.1,1433MSSQLSERVER但ArcEngine却支持
IWorkspace 解析出来:
SERVER="127.0.0.1"
INSTANCE="sde:sqlserver:127.0.0.1,1433"
DBCLIENT="sqlserver"
DB_CONNECTION_PROPERTIES="127.0.0.1,1433"
DATABASE="GISDATA"
IS_GEODATABASE="true"
AUTHENTICATION_MODE="DBMS"
CONNPROP-REV="Rev1.0"
USER="sa"
PASSWORD="