1. 程式人生 > >ArcEngine中打開各種數據源(WorkSpace)的連接

ArcEngine中打開各種數據源(WorkSpace)的連接

sqli nec path serve rom qlite cef 端口號 連接

1.創建workspacefactory

 //方式1
 Type factoryShpType = Type.GetTypeFromProgID("esriDataSourcesFile.ShapefileWorkspaceFactory");//Shp
 Type factoryGdbType = Type.GetTypeFromProgID("esriDataSourcesGDB.FileGDBWorkspaceFactory");//Gdb
 Type factorySdeType = Type.GetTypeFromProgID("esriDataSourcesGDB.SdeWorkspaceFactory");//Sde
 Type factoryMdbType = Type.GetTypeFromProgID("esriDataSourcesGDB.AccessWorkspaceFactory");//Mdb
 Type factorySqliteType = Type.GetTypeFromProgID("esriDataSourcesGDB.SqlWorkspaceFactory");//Sqlite
 Type factoryCadType = Type.GetTypeFromProgID("esriDataSourcesFile.CadWorkspaceFactory");//Cad
 IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)Activator.CreateInstance(factory***Type);

 ------------------------------------------------------------------------------
 //方式2
 IWorkspaceFactory wksGdbFactory = new FileGDBWorkspaceFactoryClass();
 IWorkspaceFactory wksSdeFactory = new SdeWorkspaceFactoryClass();
 IWorkspaceFactory wksMdbFactory = new AccessWorkspaceFactoryClass();
 IWorkspaceFactory2 wksRasterFactory = new RasterWorkspaceFactoryClass();

2.調用workspacefactory的方法打開數據源

 //MDB,GDB,SDE文件路徑
 string strDbPath = "文件路徑";
 IWorkspace wks = wksFactory.OpenFromFile(strDbPath, 0);

 //SDE也可通過連接信息打開
 IPropertySet propSet=new PropertySetClass();
 propSet.SetProperty("server","服務器機器名" );
 propSet.SetProperty("instance","SDE運行的端口號");
 propSet.SetProperty("user","用戶名");
 propSet.SetProperty("password","口令" );
 propSet.SetProperty("password","口令" );
 IWorkspace wks = wksFactory.Open(propSet, 0);

PS:獲取MDB,GDB文件路徑時要註意,“右鍵數據庫文件=》屬性=》安全=》對象名稱”,此處的文件路徑有問題,如要選路徑可從資源管理器的文件路徑復制。
string connectionString = string.Format("Provider=ESRI.GeoDB.OleDB.1;Data Source={0};Extended Properties=workspacetype=esriDataSourcesGDB.FileGDBWorkspaceFactory.1;Geometry=WKB", strDbPath);

ArcEngine中打開各種數據源(WorkSpace)的連接