Esempio n. 1
0
        /// <summary>
        /// 设置PDB、GDB工作区
        /// </summary>
        /// <param name="sFilePath">文件路径</param>
        /// <param name="wstype">工作区类型</param>
        /// <returns>输出错误Exception</returns>
        public bool SetWorkspace(string sFilePath, string type, out Exception eError)
        {
            eError = null;

            try
            {
                IPropertySet pPropSet = new PropertySetClass();
                switch (type)
                {
                case "PDB":
                    AccessWorkspaceFactory pAccessFact = new AccessWorkspaceFactoryClass();
                    pPropSet.SetProperty("DATABASE", sFilePath);
                    m_Workspace = pAccessFact.Open(pPropSet, 0);
                    pAccessFact = null;
                    m_connset   = "PDB;" + sFilePath;
                    break;

                case "GDB":
                    FileGDBWorkspaceFactoryClass pFileGDBFact = new FileGDBWorkspaceFactoryClass();
                    pPropSet.SetProperty("DATABASE", sFilePath);
                    m_Workspace  = pFileGDBFact.Open(pPropSet, 0);
                    pFileGDBFact = null;
                    m_connset    = "GDB;" + sFilePath;
                    break;
                }

                pPropSet = null;
                return(true);
            }
            catch (Exception eX)
            {
                return(false);
            }
        }
Esempio n. 2
0
        private IWorkspace GetWorkspace(string sFilePath, int wstype)
        {
            IWorkspace pWks = null;

            try
            {
                IPropertySet pPropSet = new PropertySetClass();
                switch (wstype)
                {
                case 1:
                    AccessWorkspaceFactory pAccessFact = new AccessWorkspaceFactoryClass();
                    pPropSet.SetProperty("DATABASE", sFilePath);
                    pWks        = pAccessFact.Open(pPropSet, 0);
                    pAccessFact = null;
                    break;

                case 2:
                    FileGDBWorkspaceFactoryClass pFileGDBFact = new FileGDBWorkspaceFactoryClass();
                    pPropSet.SetProperty("DATABASE", sFilePath);
                    pWks         = pFileGDBFact.Open(pPropSet, 0);
                    pFileGDBFact = null;
                    break;

                case 3:
                    break;
                }
                pPropSet = null;
                return(pWks);
            }
            catch
            {
                return(null);
            }
        }
Esempio n. 3
0
        /// <summary>
        /// 设置PDB、GDB工作区
        /// </summary>
        /// <param name="sFilePath">文件路径</param>
        /// <param name="wstype">工作区类型</param>
        /// <returns>输出错误Exception</returns>
        private IWorkspace SetWorkspace(string sFilePath, enumWSType wstype, out Exception eError)
        {
            eError = null;
            IWorkspace pWorkspace = null;

            try
            {
                IPropertySet pPropSet = new PropertySetClass();
                switch (wstype)
                {
                case enumWSType.PDB:
                    AccessWorkspaceFactory pAccessFact = new AccessWorkspaceFactoryClass();
                    pPropSet.SetProperty("DATABASE", sFilePath);
                    pWorkspace  = pAccessFact.Open(pPropSet, 0);
                    pAccessFact = null;
                    break;

                case enumWSType.GDB:
                    FileGDBWorkspaceFactoryClass pFileGDBFact = new FileGDBWorkspaceFactoryClass();
                    pPropSet.SetProperty("DATABASE", sFilePath);
                    pWorkspace   = pFileGDBFact.Open(pPropSet, 0);
                    pFileGDBFact = null;
                    break;
                }
                pPropSet = null;
                return(pWorkspace);
            }
            catch (Exception eX)
            {
                eError = eX;
                return(null);
            }
        }
Esempio n. 4
0
        /*
         * A utility function to open the File Geodatabase workspace
         */
        public static IWorkspace openFileGeodatabaseWorkspace(ref log4net.ILog log, string workspaceDirectory, string workspaceFolderName)
        {
            // attempting to open the file geodatabase
            log.Debug("Attempting to open the File Geodatabase.");
            log.Debug("Workspace: " + workspaceDirectory);
            log.Debug("Directory: " + workspaceFolderName);
            try
            {
                // verify the workspace does already exist
                log.Debug("Checking to see if the referenced File Geodatabase exists or not.");
                if (System.IO.Directory.Exists(workspaceDirectory + "\\" + workspaceFolderName))
                {
                    // File Geodatabase exist, create/configure a properties set
                    log.Debug("Creating a property set object.");
                    IPropertySet propertySet = new PropertySetClass();

                    // set the database value
                    log.Debug("Setting the DATABASE value of the property set.");
                    propertySet.SetProperty("DATABASE", workspaceDirectory + "\\" + workspaceFolderName);

                    // create a new workspace factory to open the FDGB
                    log.Debug("Creating a new workspace factory to open the file geodatabase.");
                    IWorkspaceFactory workspaceFactory = new FileGDBWorkspaceFactoryClass();

                    // Open the FDGB and return it
                    log.Debug("Opening and returning the workspace.");
                    return(workspaceFactory.Open(propertySet, 0));
                }
                else
                {
                    // Referenced File Geodatabase (FGDB) does ont exist
                    log.Warn("Referenced File Geodatabase (FGDB) does not exist");
                    throw new System.Exception(Properties.Resources.fgdb_GeodatabaseUtilities_openFileGeodatabaseWorkspace_notfound);
                }
            }
            catch (System.Exception ex)
            {
                // an error occured, log and throw
                log.Error(ex);
                throw new System.Exception(Properties.Resources.fgdb_GeodatabaseUtilities_openFileGeodatabaseWorkspace_unknown);
            }
        }
Esempio n. 5
0
        /// <summary>
        /// 设置PDB、GDB、SHP工作区
        /// </summary>
        /// <param name="sFilePath">文件路径</param>
        /// <param name="wstype">工作区类型</param>
        /// <returns>输出错误Exception</returns>
        private IWorkspace SetWorkspace(string sFilePath, string dbType, out Exception eError)
        {
            eError = null;
            IWorkspace   pWS      = null;
            IPropertySet pPropSet = new PropertySetClass();

            try
            {
                switch (dbType)
                {
                case "PDB":
                    AccessWorkspaceFactory pAccessFact = new AccessWorkspaceFactoryClass();
                    pPropSet.SetProperty("DATABASE", sFilePath);
                    pWS         = pAccessFact.Open(pPropSet, 0);
                    pAccessFact = null;
                    break;

                case "GDB":
                    FileGDBWorkspaceFactoryClass pFileGDBFact = new FileGDBWorkspaceFactoryClass();
                    pPropSet.SetProperty("DATABASE", sFilePath);
                    pWS          = pFileGDBFact.Open(pPropSet, 0);
                    pFileGDBFact = null;
                    break;

                case "SHP":
                    ShapefileWorkspaceFactory pFileSHPFact = new ShapefileWorkspaceFactory();
                    pPropSet.SetProperty("DATABASE", sFilePath);
                    pWS          = pFileSHPFact.Open(pPropSet, 0);
                    pFileSHPFact = null;
                    break;
                }
                pPropSet = null;
                return(pWS);
            }
            catch (Exception eX)
            {
                eError = eX;
                return(null);
            }
        }
Esempio n. 6
0
 /// <summary>
 /// 打开更新目标工作空间
 /// </summary>
 /// <returns>返回模板工作空间</returns>
 private IWorkspace OpenTempleteWorkSpace()
 {
     if (this._DesDBType == "PDB")
     {
         IWorkspaceFactory pWorkspaceFactory = new AccessWorkspaceFactoryClass();
         return(pWorkspaceFactory.Open(this._PropertySet, 0));
     }
     else if (this._DesDBType == "GDB")
     {
         IWorkspaceFactory pWorkspaceFactory = new FileGDBWorkspaceFactoryClass();
         return(pWorkspaceFactory.Open(this._PropertySet, 0));
     }
     else if (this._DesDBType == "SDE")
     {
         IWorkspaceFactory pWorkspaceFactory = new SdeWorkspaceFactoryClass();
         return(pWorkspaceFactory.Open(this._PropertySet, 0));
     }
     else
     {
         return(null);
     }
 }
Esempio n. 7
0
        private void listViewEx2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listViewEx2.SelectedItems.Count == 0)
            {
                return;
            }
            if (listViewEx2.SelectedItems.Count > 1)
            {
                SysCommon.Error.ErrorHandle.ShowFrmErrorHandle("信息提示", "请只选择一行");
                return;
            }
            listViewEx1.Items.Clear();
            ListViewItem         pListItem      = listViewEx2.SelectedItems[0];
            string               path           = pListItem.SubItems[0].Text;
            string               type           = pListItem.SubItems[1].Text;
            string               lastPath       = path.Substring(path.LastIndexOf('.') + 1).ToLower();
            IWorkspace           mWorkSpace     = null;
            List <IFeatureLayer> mFeatureLayers = new List <IFeatureLayer>();

            if (type == "Personal Geodatabase")
            {
                try
                {
                    if (lastPath == "mdb")
                    {
                        AccessWorkspaceFactory pAccessFact = new AccessWorkspaceFactoryClass();
                        IPropertySet           pPropSet    = new PropertySetClass();
                        pPropSet.SetProperty("DATABASE", path);
                        mWorkSpace = pAccessFact.Open(pPropSet, 0);
                    }
                    else if (lastPath == "gdb")
                    {
                        FileGDBWorkspaceFactoryClass pFileGDBFact = new FileGDBWorkspaceFactoryClass();
                        IPropertySet pPropSet = new PropertySetClass();
                        pPropSet.SetProperty("DATABASE", path);
                        mWorkSpace = pFileGDBFact.Open(pPropSet, 0);
                    }

                    if (pAllEditInfo.ContainsKey(mWorkSpace))
                    {
                        mFeatureLayers = pAllEditInfo[mWorkSpace];
                        foreach (IFeatureLayer i in mFeatureLayers)
                        {
                            listViewEx1.Items.Add(i.Name.ToString());
                        }
                        listViewEx1.Refresh();
                    }
                    pSelectWorkspace = mWorkSpace;
                }
                catch (System.Exception ex)
                {
                    //******************************************
                    //guozheng added System Exception log
                    if (SysCommon.Log.Module.SysLog == null)
                    {
                        SysCommon.Log.Module.SysLog = new SysCommon.Log.clsWriteSystemFunctionLog();
                    }
                    SysCommon.Log.Module.SysLog.Write(ex);
                    //******************************************
                }
            }
        }
Esempio n. 8
0
        //根据XML节点获取连接信息
        public static object GetDBInfoByXMLNode(XmlElement dbElement, string strPath)
        {
            try
            {
                string strType     = dbElement.GetAttribute("类型");
                string strServer   = dbElement.GetAttribute("服务器");
                string strInstance = dbElement.GetAttribute("服务名");
                string strDB       = dbElement.GetAttribute("数据库");
                if (strPath != "")
                {
                    strDB = strPath + strDB;
                }
                string strUser     = dbElement.GetAttribute("用户");
                string strPassword = dbElement.GetAttribute("密码");
                string strVersion  = dbElement.GetAttribute("版本");

                IPropertySet pPropSet = null;
                switch (strType.Trim().ToLower())
                {
                case "pdb":
                    pPropSet = new PropertySetClass();
                    AccessWorkspaceFactory pAccessFact = new AccessWorkspaceFactoryClass();
                    if (!File.Exists(strDB))
                    {
                        FileInfo filePDB = new FileInfo(strDB);
                        pAccessFact.Create(filePDB.DirectoryName, filePDB.Name, null, 0);
                    }
                    pPropSet.SetProperty("DATABASE", strDB);
                    IWorkspace pdbWorkspace = pAccessFact.Open(pPropSet, 0);
                    pAccessFact = null;
                    return(pdbWorkspace);

                case "gdb":
                    pPropSet = new PropertySetClass();
                    FileGDBWorkspaceFactoryClass pFileGDBFact = new FileGDBWorkspaceFactoryClass();
                    if (!Directory.Exists(strDB))
                    {
                        DirectoryInfo dirGDB = new DirectoryInfo(strDB);
                        pFileGDBFact.Create(dirGDB.Parent.FullName, dirGDB.Name, null, 0);
                    }
                    pPropSet.SetProperty("DATABASE", strDB);
                    IWorkspace gdbWorkspace = pFileGDBFact.Open(pPropSet, 0);
                    pFileGDBFact = null;
                    return(gdbWorkspace);

                case "sde":
                    pPropSet = new PropertySetClass();
                    IWorkspaceFactory pSdeFact = new SdeWorkspaceFactoryClass();
                    pPropSet.SetProperty("SERVER", strServer);
                    pPropSet.SetProperty("INSTANCE", strInstance);
                    pPropSet.SetProperty("DATABASE", strDB);
                    pPropSet.SetProperty("USER", strUser);
                    pPropSet.SetProperty("PASSWORD", strPassword);
                    pPropSet.SetProperty("VERSION", strVersion);
                    IWorkspace sdeWorkspace = pSdeFact.Open(pPropSet, 0);
                    pSdeFact = null;
                    return(sdeWorkspace);

                case "access":
                    System.Data.Common.DbConnection dbCon = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strDB);
                    dbCon.Open();
                    return(dbCon);

                case "oracle":
                    string strOracle = "Data Source=" + strDB + ";Persist Security Info=True;User ID=" + strUser + ";Password="******";Unicode=True";
                    System.Data.Common.DbConnection dbConoracle = new OracleConnection(strOracle);
                    dbConoracle.Open();
                    return(dbConoracle);

                case "sql":
                    string strSql = "Data Source=" + strDB + ";Initial Catalog=" + strInstance + ";User ID=" + strUser + ";Password=" + strPassword;
                    System.Data.Common.DbConnection dbConsql = new SqlConnection(strSql);
                    dbConsql.Open();
                    return(dbConsql);

                default:
                    break;
                }

                return(null);
            }
            catch (Exception e)
            {
                //*******************************************************************
                //guozheng added
                if (ModuleData.v_SysLog != null)
                {
                    ModuleData.v_SysLog.Write(e, null, DateTime.Now);
                }
                else
                {
                    ModuleData.v_SysLog = new SysCommon.Log.clsWriteSystemFunctionLog();
                    ModuleData.v_SysLog.Write(e, null, DateTime.Now);
                }
                //********************************************************************
                return(null);
            }
        }
Esempio n. 9
0
        private void btnOk_Click(object sender, EventArgs e)
        {
            if (listViewEx.CheckedItems.Count == 0)
            {
                SysCommon.Error.ErrorHandle.ShowFrmErrorHandle("提示", "未选择对象,无法生成历史!");
                return;
            }

            Exception err = null;

            //获取现势库连接信息

            SysCommon.Gis.SysGisDataSet sourceSysGisDataSet = new SysCommon.Gis.SysGisDataSet();
            switch (comBoxType.Text)
            {
            case "SDE":
                sourceSysGisDataSet.SetWorkspace(txtServer.Text, txtInstance.Text, txtDB.Text, txtUser.Text, txtPassword.Text, txtVersion.Text, out err);
                break;

            case "GDB":
                sourceSysGisDataSet.SetWorkspace(txtDB.Text, SysCommon.enumWSType.GDB, out err);
                break;

            case "PDB":
                sourceSysGisDataSet.SetWorkspace(txtDB.Text, SysCommon.enumWSType.PDB, out err);
                break;

            default:
                break;
            }

            if (err != null || sourceSysGisDataSet.WorkSpace == null)
            {
                SysCommon.Error.ErrorHandle.ShowFrmErrorHandle("提示", "未设置用户数据库连接或连接失败,请检查!");
                return;
            }

            //获取历史库连接信息

            IPropertySet pPropSet        = new PropertySetClass();
            IWorkspace   pTagetWorkspace = null;

            try
            {
                switch (comboBoxTypeHistory.Text)
                {
                case "PDB":
                    AccessWorkspaceFactory pAccessFact = new AccessWorkspaceFactoryClass();
                    if (!File.Exists(txtDBHistory.Text))
                    {
                        FileInfo filePDB = new FileInfo(txtDBHistory.Text);
                        pAccessFact.Create(filePDB.DirectoryName, filePDB.Name, null, 0);
                    }
                    pPropSet.SetProperty("DATABASE", txtDBHistory.Text);
                    pTagetWorkspace = pAccessFact.Open(pPropSet, 0);

                    break;

                case "GDB":
                    FileGDBWorkspaceFactoryClass pFileGDBFact = new FileGDBWorkspaceFactoryClass();
                    DirectoryInfo dirGDB = new DirectoryInfo(txtDBHistory.Text);
                    pFileGDBFact.Create(dirGDB.Parent.FullName, dirGDB.Name.Substring(0, dirGDB.Name.Length - 4), null, 0);
                    pPropSet.SetProperty("DATABASE", txtDBHistory.Text);
                    pTagetWorkspace = pFileGDBFact.Open(pPropSet, 0);

                    break;

                case "SDE":
                    IWorkspaceFactory pSdeFact = new SdeWorkspaceFactoryClass();
                    pPropSet.SetProperty("SERVER", txtServerHistory.Text);
                    pPropSet.SetProperty("INSTANCE", txtInstanceHistory.Text);
                    pPropSet.SetProperty("DATABASE", txtDBHistory.Text);
                    pPropSet.SetProperty("USER", txtUserHistory.Text);
                    pPropSet.SetProperty("PASSWORD", txtPasswordHistory.Text);
                    pPropSet.SetProperty("VERSION", txtVersionHistory.Text);
                    pTagetWorkspace = pSdeFact.Open(pPropSet, 0);

                    break;

                default:
                    break;
                }
            }
            catch (Exception er)
            {
                //*******************************************************************
                //guozheng added
                if (ModData.SysLog != null)
                {
                    ModData.SysLog.Write(er, null, DateTime.Now);
                }
                else
                {
                    ModData.SysLog = new SysCommon.Log.clsWriteSystemFunctionLog();
                    ModData.SysLog.Write(er, null, DateTime.Now);
                }
                //********************************************************************
            }

            if (pTagetWorkspace == null)
            {
                SysCommon.Error.ErrorHandle.ShowFrmErrorHandle("提示", "未设置历史库连接或连接失败,请检查!");
                return;
            }

            this.Cursor = System.Windows.Forms.Cursors.AppStarting;

            List <string> lstData = new List <string>();

            try
            {
                //根据用户数据库结构创建历史库
                foreach (ListViewItem aItem in listViewEx.CheckedItems)
                {
                    if (aItem.Tag.ToString() == "FD")
                    {
                        IFeatureDataset tagetFeatureDataset = null;
                        IFeatureDataset pFeatureDataset     = sourceSysGisDataSet.GetFeatureDataset(aItem.Text, out err);
                        if (!(pTagetWorkspace as IWorkspace2).get_NameExists(esriDatasetType.esriDTFeatureDataset, aItem.Text + "_GOH"))
                        {
                            tagetFeatureDataset = (pTagetWorkspace as IFeatureWorkspace).CreateFeatureDataset(aItem.Text + "_GOH", (pFeatureDataset as IGeoDataset).SpatialReference);
                        }
                        else
                        {
                            tagetFeatureDataset = (pTagetWorkspace as IFeatureWorkspace).OpenFeatureDataset(aItem.Text + "_GOH");
                        }

                        IEnumDataset pEnumDs = pFeatureDataset.Subsets;
                        pEnumDs.Reset();
                        IDataset pDs = pEnumDs.Next();
                        while (pDs != null)
                        {
                            IFeatureClass pFeatureClass = pDs as IFeatureClass;
                            if (pFeatureClass != null)
                            {
                                if (!(pTagetWorkspace as IWorkspace2).get_NameExists(esriDatasetType.esriDTFeatureClass, pDs.Name + "_GOH"))
                                {
                                    CreateFeatCls(tagetFeatureDataset, pFeatureClass, pDs.Name + "_GOH", out err);
                                }
                                lstData.Add(pDs.Name);
                            }
                            pDs = pEnumDs.Next();
                        }
                    }
                    else if (aItem.Tag.ToString() == "FC")
                    {
                        IFeatureClass pFeatureClass = sourceSysGisDataSet.GetFeatureClass(aItem.Text, out err);
                        if (!(pTagetWorkspace as IWorkspace2).get_NameExists(esriDatasetType.esriDTFeatureClass, aItem.Text + "_GOH"))
                        {
                            CreateFeatCls(pTagetWorkspace as IFeatureWorkspace, pFeatureClass, aItem.Text + "_GOH", out err);
                        }
                        lstData.Add(aItem.Text);
                    }
                }
            }
            catch (Exception er)
            {
                //*******************************************************************
                //guozheng added
                if (ModData.SysLog != null)
                {
                    ModData.SysLog.Write(er, null, DateTime.Now);
                }
                else
                {
                    ModData.SysLog = new SysCommon.Log.clsWriteSystemFunctionLog();
                    ModData.SysLog.Write(er, null, DateTime.Now);
                }
                //********************************************************************
                this.Cursor = System.Windows.Forms.Cursors.Default;
                SysCommon.Error.ErrorHandle.ShowFrmErrorHandle("提示", "创建历史库结构失败!");
                return;
            }

            //遍历现势库数据FC进行数据移植
            Dictionary <string, string> dicFieldsPair = new Dictionary <string, string>();

            dicFieldsPair.Add("OBJECTID", "SourceOID");
            Dictionary <string, object> dicValue = new Dictionary <string, object>();

            dicValue.Add("FromDate", DateTime.Now.ToString("u"));
            dicValue.Add("ToDate", DateTime.MaxValue.ToString("u"));//.ToString("YYYY-MM-DD HH:MI:SS"));
            dicValue.Add("State", 0);
            (pTagetWorkspace as IWorkspaceEdit).StartEditing(false);
            bool res = true;

            progressBarXLay.Maximum = lstData.Count;
            progressBarXLay.Minimum = 0;
            progressBarXLay.Value   = 0;
            foreach (string aFeatClsName in lstData)
            {
                labelXMemo.Text = "正在进行图层" + aFeatClsName + "...";
                Application.DoEvents();
                int            cnt           = 0;
                int            allcnt        = 0;
                IFeatureCursor featureCursor = null;
                IFeatureClass  tagetFeatCls  = null;
                try
                {
                    featureCursor = sourceSysGisDataSet.GetFeatureCursor(aFeatClsName, "", null, "", out err, out cnt, out allcnt);
                    tagetFeatCls  = (pTagetWorkspace as IFeatureWorkspace).OpenFeatureClass(aFeatClsName + "_GOH");
                }
                catch (Exception ex)
                {
                    //*******************************************************************
                    //guozheng added
                    if (ModData.SysLog != null)
                    {
                        ModData.SysLog.Write(ex, null, DateTime.Now);
                    }
                    else
                    {
                        ModData.SysLog = new SysCommon.Log.clsWriteSystemFunctionLog();
                        ModData.SysLog.Write(ex, null, DateTime.Now);
                    }
                    //********************************************************************
                    SysCommon.Error.ErrorHandle.ShowFrmErrorHandle("提示", ex.Message);
                    return;
                }

                progressBarXFeat.Maximum = cnt;
                progressBarXFeat.Minimum = 0;
                progressBarXFeat.Value   = 0;
                ModDBOperator.NewFeatures(tagetFeatCls, featureCursor, dicFieldsPair, dicValue, true, false, progressBarXFeat, out err);
                Marshal.ReleaseComObject(featureCursor);
                progressBarXLay.Value++;

                labelXMemo.Text = "";
                Application.DoEvents();
                if (err != null)
                {
                    res = false;
                    break;
                }
            }
            (pTagetWorkspace as IWorkspaceEdit).StopEditing(res);

            this.Cursor = System.Windows.Forms.Cursors.Default;
            if (res)
            {
                if (m_DbProjectElement != null)
                {
                    try
                    {
                        XmlElement aElement = m_DbProjectElement.SelectSingleNode(".//内容//历史库//连接信息") as XmlElement;
                        SaveObjDB(aElement, listViewEx.Items[0].Text + "_GOH");
                    }
                    catch (Exception er)
                    {
                        //*******************************************************************
                        //guozheng added
                        if (ModData.SysLog != null)
                        {
                            ModData.SysLog.Write(er, null, DateTime.Now);
                        }
                        else
                        {
                            ModData.SysLog = new SysCommon.Log.clsWriteSystemFunctionLog();
                            ModData.SysLog.Write(er, null, DateTime.Now);
                        }
                        //********************************************************************
                        return;
                    }
                }
                SysCommon.Error.ErrorHandle.ShowFrmErrorHandle("提示", "生成历史库成功!");
                this.Close();
            }
            else
            {
                SysCommon.Error.ErrorHandle.ShowFrmErrorHandle("提示", "生成历史库失败!");
            }
        }
Esempio n. 10
0
        private static IWorkspace GetWorkspace()
        {
            try
            {
                //Create a new AccessWorkspaceFactoryClass
                IWorkspaceFactory wsf = new FileGDBWorkspaceFactoryClass();

                ESRI.ArcGIS.esriSystem.IPropertySet ps = new PropertySetClass();  //C:\ArcFM\CAFM\Sample Data\Geodatabases
                ps.SetProperty("DATABASE", @"C:\Code\Git\GreatDivideTrail\Database\GDT.gdb");

                IWorkspace ws = wsf.Open(ps, 0);
                return ws;
            }
            catch (Exception ex)
            {
                _log.Error("ERROR - ", ex);
                //Throw the error, because we can't recover from this error!
                throw (new ApplicationException("Unable to open workspace", ex));
            }
        }
Esempio n. 11
0
        private void btnOk_Click(object sender, EventArgs e)
        {
            if (txtProjectName.Text == "")
            {
                SysCommon.Error.ErrorHandle.ShowFrmErrorHandle("信息提示", "请填写输出文件名称 !");
                return;
            }

            if (!Directory.Exists(txtSavePath.Text.Trim()))
            {
                SysCommon.Error.ErrorHandle.ShowFrmErrorHandle("信息提示", "请选择保存路径 !");
                return;
            }

            if (listViewEx.CheckedItems.Count == 0)
            {
                SysCommon.Error.ErrorHandle.ShowFrmErrorHandle("信息提示", "请选择数据版本信息 !");
                return;
            }

            string WorkSpacePath = txtSavePath.Text.Trim() + "\\" + txtProjectName.Text.Trim();

            IPropertySet pPropSet        = new PropertySetClass();
            IWorkspace   pTagetWorkspace = null;

            try
            {
                switch (comBoxType.Tag.ToString().Trim())
                {
                case "PDB":
                    AccessWorkspaceFactory pAccessFact = new AccessWorkspaceFactoryClass();
                    if (!File.Exists(WorkSpacePath + ".mdb"))
                    {
                        pAccessFact.Create(txtSavePath.Text, txtProjectName.Text, null, 0);
                    }
                    pPropSet.SetProperty("DATABASE", WorkSpacePath + ".mdb");
                    pTagetWorkspace = pAccessFact.Open(pPropSet, 0);
                    pAccessFact     = null;
                    pPropSet        = null;
                    break;

                case "GDB":
                    FileGDBWorkspaceFactoryClass pFileGDBFact = new FileGDBWorkspaceFactoryClass();
                    if (!Directory.Exists(WorkSpacePath + ".gdb"))
                    {
                        pFileGDBFact.Create(txtSavePath.Text, txtProjectName.Text, null, 0);
                    }
                    pPropSet.SetProperty("DATABASE", WorkSpacePath + ".gdb");
                    pTagetWorkspace = pFileGDBFact.Open(pPropSet, 0);
                    pFileGDBFact    = null;
                    pPropSet        = null;
                    break;

                default:
                    break;
                }
            }
            catch (Exception er)
            {
                //*******************************************************************
                //guozheng added
                if (ModData.SysLog != null)
                {
                    ModData.SysLog.Write(er, null, DateTime.Now);
                }
                else
                {
                    ModData.SysLog = new SysCommon.Log.clsWriteSystemFunctionLog();
                    ModData.SysLog.Write(er, null, DateTime.Now);
                }
                //********************************************************************
            }

            if (pTagetWorkspace == null)
            {
                SysCommon.Error.ErrorHandle.ShowFrmErrorHandle("提示", "未设置输出库连接或连接失败,请检查!");
                return;
            }

            this.Cursor = System.Windows.Forms.Cursors.AppStarting;

            Exception            err        = null;
            bool                 res        = true;
            List <IFeatureLayer> lstFeatLay = new List <IFeatureLayer>();

            //根据历史图层创建输出数据图层结构
            for (int i = 0; i < Map.LayerCount; i++)
            {
                //cyf 20110706 add
                ILayer mLayer = Map.get_Layer(i);
                if (mLayer is IGroupLayer)
                {
                    ICompositeLayer pComLayer = mLayer as ICompositeLayer;
                    for (int j = 0; j < pComLayer.Count; j++)
                    {
                        IFeatureLayer featLay = pComLayer.get_Layer(j) as IFeatureLayer;
                        if (featLay == null)
                        {
                            continue;
                        }
                        if (!(featLay.FeatureClass as IDataset).Name.EndsWith("_GOH"))
                        {
                            continue;
                        }
                        lstFeatLay.Add(featLay);
                        if (!(pTagetWorkspace as IWorkspace2).get_NameExists(esriDatasetType.esriDTFeatureClass, featLay.Name))//(featLay.FeatureClass as IDataset).Name
                        {
                            if (!CreateFeatCls(pTagetWorkspace as IFeatureWorkspace, featLay.FeatureClass, featLay.Name, out err))
                            {
                                res       = false;
                                featLay   = null;
                                pComLayer = null;
                                mLayer    = null;
                                break;
                            }
                        }
                    }
                }//end
                else
                {
                    IFeatureLayer featLay = Map.get_Layer(i) as IFeatureLayer;
                    if (featLay == null)
                    {
                        continue;
                    }
                    if (!(featLay.FeatureClass as IDataset).Name.EndsWith("_GOH"))
                    {
                        continue;
                    }
                    lstFeatLay.Add(featLay);
                    if (!(pTagetWorkspace as IWorkspace2).get_NameExists(esriDatasetType.esriDTFeatureClass, featLay.Name))//(featLay.FeatureClass as IDataset).Name
                    {
                        if (!CreateFeatCls(pTagetWorkspace as IFeatureWorkspace, featLay.FeatureClass, featLay.Name, out err))
                        {
                            res     = false;
                            featLay = null;
                            mLayer  = null;
                            break;
                        }
                    }
                }
            }

            if (res == false)
            {
                this.Cursor = System.Windows.Forms.Cursors.Default;
                SysCommon.Error.ErrorHandle.ShowFrmErrorHandle("提示", "创建输出库结构失败!");
                System.Runtime.InteropServices.Marshal.ReleaseComObject(pTagetWorkspace);
                pTagetWorkspace = null;
                lstFeatLay.Clear();
                lstFeatLay = null;
                try
                {
                    Directory.Delete(txtSavePath.Text + "\\" + txtProjectName.Text + ".gdb", true);
                }
                catch (Exception err2)
                { }
                return;
            }

            StringBuilder sb = new StringBuilder();

            foreach (ListViewItem aItem in listViewEx.CheckedItems)
            {
                if (sb.Length != 0)
                {
                    sb.Append("or");
                }
                else
                {
                    sb.Append("(");
                }

                sb.Append("(FromDate<='" + aItem.Text + "' and ToDate>'" + aItem.Text + "')");
            }

            sb.Append(")");

            //遍历数据FC进行数据移植
            (pTagetWorkspace as IWorkspaceEdit).StartEditing(false);
            progressBarXLay.Maximum = lstFeatLay.Count;
            progressBarXLay.Minimum = 0;
            progressBarXLay.Value   = 0;
            foreach (IFeatureLayer aFeatLay in lstFeatLay)
            {
                labelXMemo.Text = aFeatLay.FeatureClass.AliasName + "...";
                Application.DoEvents();

                StringBuilder newSB = new StringBuilder();
                newSB.Append(sb.ToString());

                if (m_Sel)
                {
                    int fdIndex = aFeatLay.FeatureClass.Fields.FindField("SourceOID");
                    if (fdIndex == -1)
                    {
                        continue;
                    }
                    IFeatureLayerDefinition featLayDefTemp = aFeatLay as IFeatureLayerDefinition;
                    IEnumIDs      pEnumIDs = featLayDefTemp.DefinitionSelectionSet.IDs;
                    int           ID       = pEnumIDs.Next();
                    StringBuilder sbTemp   = new StringBuilder();
                    while (ID != -1)
                    {
                        IFeature pFeat = aFeatLay.FeatureClass.GetFeature(ID);
                        if (sbTemp.Length != 0)
                        {
                            sbTemp.Append(",");
                        }
                        sbTemp.Append(pFeat.get_Value(fdIndex).ToString());
                        ID = pEnumIDs.Next();
                    }
                    newSB.Append(" and SourceOID in (" + sbTemp.ToString() + ")");
                }

                IQueryFilter queryFilter = new QueryFilterClass();
                queryFilter.WhereClause = newSB.ToString();
                IFeatureCursor featureCursor = aFeatLay.FeatureClass.Search(queryFilter, false);
                progressBarXFeat.Maximum = aFeatLay.FeatureClass.FeatureCount(queryFilter);
                progressBarXFeat.Minimum = 0;
                progressBarXFeat.Value   = 0;
                try
                {
                    //cyf 20110706 modify:去掉用户名
                    string pFeaLayerName = aFeatLay.Name.Trim();
                    if (pFeaLayerName.Contains("."))
                    {
                        pFeaLayerName = pFeaLayerName.Substring(pFeaLayerName.IndexOf('.') + 1);
                    }
                    //end
                    IFeatureClass tagetFeatCls = (pTagetWorkspace as IFeatureWorkspace).OpenFeatureClass(pFeaLayerName);//(aFeatLay.FeatureClass as IDataset).Name
                    ModDBOperator.NewFeatures(tagetFeatCls, featureCursor, null, null, true, false, progressBarXFeat, out err);
                }
                catch (Exception ex)
                {
                    //*******************************************************************
                    //guozheng added
                    if (ModData.SysLog != null)
                    {
                        ModData.SysLog.Write(ex, null, DateTime.Now);
                    }
                    else
                    {
                        ModData.SysLog = new SysCommon.Log.clsWriteSystemFunctionLog();
                        ModData.SysLog.Write(ex, null, DateTime.Now);
                    }
                    //********************************************************************
                    SysCommon.Error.ErrorHandle.ShowFrmErrorHandle("提示", "入库失败!");
                    return;
                }

                Marshal.ReleaseComObject(featureCursor);
                progressBarXLay.Value++;

                labelXMemo.Text = "";
                Application.DoEvents();
                if (err != null)
                {
                    res = false;
                    break;
                }
            }

            (pTagetWorkspace as IWorkspaceEdit).StopEditing(res);

            this.Cursor = System.Windows.Forms.Cursors.Default;
            if (res)
            {
                SysCommon.Error.ErrorHandle.ShowFrmErrorHandle("提示", "提取成功!");
                this.Close();
            }
            else
            {
                SysCommon.Error.ErrorHandle.ShowFrmErrorHandle("提示", "提取失败!");
            }
        }
Esempio n. 12
0
        private static IWorkspace Connect()
        {
            IPropertySet propertySet = null;
            IWorkspaceFactory workspaceFactory = null;

            try
            {
                propertySet = new PropertySet();
                propertySet.SetProperty(Connection.DATABASE, Connection.DATABASE_PATH);

                workspaceFactory = new FileGDBWorkspaceFactoryClass();

                return workspaceFactory.Open(propertySet, IntPtr.Zero.ToInt32());
            }
            catch(Exception e)
            {
                Console.WriteLine("Error establishing connection to DB. Error: " + e.Message);
            }
            finally
            {
                if (workspaceFactory != null)
                    Marshal.ReleaseComObject(workspaceFactory);
                if (propertySet != null)
                    Marshal.ReleaseComObject(propertySet);
            }

            return null;
        }
        /*
         * A utility function to open the File Geodatabase workspace
         */
        public static IWorkspace openFileGeodatabaseWorkspace(ref log4net.ILog log, string workspaceDirectory, string workspaceFolderName)
        {
            // attempting to open the file geodatabase
            log.Debug("Attempting to open the File Geodatabase.");
            log.Debug("Workspace: " + workspaceDirectory);
            log.Debug("Directory: " + workspaceFolderName);
            try
            {
                // verify the workspace does already exist
                log.Debug("Checking to see if the referenced File Geodatabase exists or not.");
                if (System.IO.Directory.Exists(workspaceDirectory + "\\" + workspaceFolderName))
                {
                    // File Geodatabase exist, create/configure a properties set
                    log.Debug("Creating a property set object.");
                    IPropertySet propertySet = new PropertySetClass();

                    // set the database value
                    log.Debug("Setting the DATABASE value of the property set.");
                    propertySet.SetProperty("DATABASE", workspaceDirectory + "\\" + workspaceFolderName);

                    // create a new workspace factory to open the FDGB
                    log.Debug("Creating a new workspace factory to open the file geodatabase.");
                    IWorkspaceFactory workspaceFactory = new FileGDBWorkspaceFactoryClass();

                    // Open the FDGB and return it
                    log.Debug("Opening and returning the workspace.");
                    return workspaceFactory.Open(propertySet, 0);
                }
                else
                {
                    // Referenced File Geodatabase (FGDB) does ont exist
                    log.Warn("Referenced File Geodatabase (FGDB) does not exist");
                    throw new System.Exception(Properties.Resources.fgdb_GeodatabaseUtilities_openFileGeodatabaseWorkspace_notfound);
                }
            }
            catch (System.Exception ex)
            {
                // an error occured, log and throw
                log.Error(ex);
                throw new System.Exception(Properties.Resources.fgdb_GeodatabaseUtilities_openFileGeodatabaseWorkspace_unknown);
            }
        }