/// <summary> /// 创建新的PersonalGeodatabase /// </summary> /// <param name="ParentDirectory">创建MDB路径</param> /// <param name="strName">MDB名称</param> public static bool CreatePGDB(string ParentDirectory, string strName) { IWorkspace Ws = null; try { IWorkspaceFactory2 workspaceFactory = (IWorkspaceFactory2) new AccessWorkspaceFactoryClass(); string strFilePath = ParentDirectory + strName; if (System.IO.File.Exists(strFilePath)) { System.IO.File.Delete(strFilePath); } IWorkspaceName workspacename = workspaceFactory.Create(ParentDirectory, strName, null, 0); string connectionstring = "DATABASE=" + workspacename.PathName; Ws = workspaceFactory.OpenFromString(connectionstring, 0); } catch (Exception exp) { Hy.Common.Utility.Log.OperationalLogManager.AppendMessage(exp.ToString()); return(false); } finally { if (Ws != null) { Marshal.ReleaseComObject(Ws); Ws = null; } } return(true); }
/// <summary> /// 创建shape图层 /// </summary> /// <param name="strShpPath"></param> /// <param name="strFtName"></param> /// <returns></returns> public static IFeatureClass CreateShpFile(string strShpPath, string strFtName, string strAliasFtName, ISpatialReference pSpatial) { string connectionstring = "DATABASE=" + strShpPath; IWorkspaceFactory2 pFactory = (IWorkspaceFactory2) new ShapefileWorkspaceFactoryClass(); IWorkspace workspace = pFactory.OpenFromString(connectionstring, 0); IFeatureWorkspace ipFtWs = (IFeatureWorkspace)workspace; //创建字段IFields IFields pFields = new FieldsClass(); IFieldsEdit pFieldsEdit = (IFieldsEdit)pFields; ///创建几何类型字段 IField pField = new FieldClass(); IFieldEdit pFieldEdit = (IFieldEdit)pField; ////设置FID字段 //IFieldEdit ipFldEdit = new FieldClass(); //(__uuidof(Field)); //ipFldEdit.Name_2 = "FID"; //ipFldEdit.AliasName_2 = "唯一标志码"; //ipFldEdit.Type_2 = esriFieldType.esriFieldTypeOID; //pFieldsEdit.AddField(ipFldEdit); pFieldEdit.Name_2 = "Shape"; pFieldEdit.AliasName_2 = "几何类型"; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry; IGeometryDef pGeomDef = new GeometryDefClass(); IGeometryDefEdit pGeomDefEdit = (IGeometryDefEdit)pGeomDef; pGeomDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPoint; pGeomDefEdit.SpatialReference_2 = pSpatial; pFieldEdit.GeometryDef_2 = pGeomDef; pFieldsEdit.AddField(pField); IFeatureClass _featureClass = ipFtWs.CreateFeatureClass(strFtName, pFields, null, null, esriFeatureType.esriFTSimple, "Shape", ""); //更新图层别名 //IClassSchemaEdit ipEdit = (IClassSchemaEdit)_featureClass; //ipEdit.AlterAliasName(strAliasFtName); pFactory = null; workspace = null; ipFtWs = null; return(_featureClass); }
/// <summary> /// 打开FileGeoDatabase /// </summary> /// <param name="ppOutWS"></param> /// <param name="strFullPath"></param> /// <returns></returns> public static bool OpenFGDB(ref IWorkspace ppOutWS, string strFullPath) { try { string connectionstring = "DATABASE=" + strFullPath; IWorkspaceFactory2 pFactory = (IWorkspaceFactory2) new FileGDBWorkspaceFactoryClass(); ppOutWS = pFactory.OpenFromString(connectionstring, 0); } catch (Exception exp) { Hy.Common.Utility.Log.OperationalLogManager.AppendMessage(exp.ToString()); return(false); } return(true); }
/// <summary> /// 通过连接字符串,获取SDE数据库连接 /// </summary> /// <param name="sConnStr"></param> /// <returns></returns> public IWorkspace OpenSDEFromString(string sConnStr) { if (string.IsNullOrEmpty(sConnStr)) { return(null); } try { Type factoryType = Type.GetTypeFromProgID("esriDataSourcesGDB.SdeWorkspaceFactory"); IWorkspaceFactory2 workspaceFactory2 = (IWorkspaceFactory2)Activator.CreateInstance(factoryType); return(workspaceFactory2.OpenFromString(sConnStr, 0)); } catch (Exception ep) { ErrorMessage = "获取SDE数据库连接失败" + ep.Message; return(null); } }
} // ConnectToGDB private static void UpdateDatalayerInfo(ITable table, Boolean verbose) { String server = ""; String dbclient = ""; String instanceOrig = ""; String instanceNew = ""; IDataLayer dataLayer = null; IRasterLayer rasterLayer = null; if (table is IRasterLayer) { rasterLayer = (IRasterLayer)table; dataLayer = (IDataLayer)rasterLayer; } else { dataLayer = (IDataLayer)table; } IName n = dataLayer.DataSourceName; IDatasetName dsn = (IDatasetName)n; IWorkspaceName2 wsn = (IWorkspaceName2)dsn.WorkspaceName; IWorkspaceFactory2 wsFact = (IWorkspaceFactory2)wsn.WorkspaceFactory; IWorkspace2 workspace = (IWorkspace2)wsFact.OpenFromString(wsn.ConnectionString.ToString(), 0); // Access connection properties, update, and re-apply IPropertySet propertySet = wsn.ConnectionProperties; // Confirm original server = propertySet.GetProperty("SERVER").ToString(); instanceOrig = propertySet.GetProperty("INSTANCE").ToString(); if (verbose) { Console.WriteLine("Original: {0}, {1}", server, instanceOrig); } // Only need to set if currently app server, check if "instance" string has a colon in it // Is there a way to check this based on the list by source so that we don't have to check every layer? if (instanceOrig.Contains(":") == false) { // Determine what kind of DBMS we are working with (Oracle, SqlServer, etc.) IDatabaseConnectionInfo3 dbmsConnInfo = (IDatabaseConnectionInfo3)workspace; // Set the DBCLIENT to the appropriate dbms switch (dbmsConnInfo.ConnectionDBMS) { case esriConnectionDBMS.esriDBMS_Oracle: dbclient = "Oracle"; instanceNew = String.Format("{0}/{1}", dbmsConnInfo.ConnectionServer, dbmsConnInfo.ConnectedDatabaseEx); break; case esriConnectionDBMS.esriDBMS_SQLServer: dbclient = "SQLServer"; instanceNew = dbmsConnInfo.ConnectionServer; break; case esriConnectionDBMS.esriDBMS_PostgreSQL: dbclient = "PostgreSQL"; instanceNew = dbmsConnInfo.ConnectionServer; break; default: dbclient = ""; break; } // Set to new target propertySet.SetProperty("DBCLIENT", dbclient); propertySet.SetProperty("DB_CONNECTION_PROPERTIES", instanceNew); // INSTANCE needs to be cleared out for Raster layers if (table is IRasterLayer) { propertySet.SetProperty("INSTANCE", "");//propertySet.SetProperty("INSTANCE", String.Format("sde:{0}",dbclient.ToLower())); } wsn.ConnectionProperties = propertySet; // Confirm target was set propertySet = null; propertySet = wsn.ConnectionProperties; if (verbose) { Console.WriteLine("NEW: {0}, {1}", propertySet.GetProperty("DBCLIENT"), propertySet.GetProperty("DB_CONNECTION_PROPERTIES")); Console.WriteLine("----------------"); } } // End if } // UpdateDatalayerInfo