大赵传奇

GIS解决方案,webgis , 桌面、数据、服务--QQ276605216

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

 

using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

namespace AEApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            var connStr = "Instance=sde:oracle11g:1727/orcl;USER=testcgk;PASSWORD=testcgk";

            IWorkspace workspace = null;
            string[] properties = connStr.Split(';');
            //直连空间数据库
            IPropertySet pPropset = new PropertySetClass();
            foreach (string item in properties)
            {
                string[] keyVal = item.Split('=');
                if (keyVal.Length == 2)
                {
                    pPropset.SetProperty(keyVal[0].Trim(), keyVal[1].Trim());
                }
            }
            try
            {
                workspace =  new SdeWorkspaceFactoryClass().Open(pPropset, 0);
            }
            catch (Exception ex)
            {
            }


            Debug.WriteLine(workspace.ConnectionProperties.GetProperty("INSTANCE"));



            IFeatureWorkspace targetFeatureWorkspace = null;
            targetFeatureWorkspace = workspace as IFeatureWorkspace;
            //测试删除
            //var desTable = targetFeatureWorkspace.OpenTable("YW_XMXX");
            //(desTable as IDataset).Workspace.ExecuteSQL($"delete from YW_XMXX where id='F0F01BAC-7110-4294-8FF2-18DA32F320BF'");

            //这句不用开始编辑也能删除。
            //workspace.ExecuteSQL($"delete from YW_XMXX where id='7F699720-1BBD-41BC-AD7F-51CA35995FEE'");



            //如果是编辑一个字段值呢,要不要开始编辑 ?

            IQueryFilter query = new QueryFilterClass();
            query.WhereClause = "id='06808D43-85B2-4D44-ABC8-86D0D13D1C9A'";
            var desTable = targetFeatureWorkspace.OpenTable("YW_XMXX");
            ICursor cursor = desTable.Search(query, false);
            IRow pRow = null;
            while (true)
            {
                pRow = cursor.NextRow();
                if (pRow == null) break;

                //操作更新
                var idx_sqr = pRow.Fields.FindField("SQR");
                pRow.set_Value(idx_sqr, "TEST");
                pRow.Store();

                break;//不更新其他行了。
            }
            Marshal.ReleaseComObject(cursor);
            //以上代码更新成功。
            //结论:更新字段值的话,不用注册版本。

            //pEngineEditor = new EngineEditorClass();
            //pEngineEditor.StartEditing(pFtWs as IWorkspace, axMapControl1.Map);
            //pEngineEditor.StartOperation();
            //IFeatureLayer pFeatureLayer = pLayer as IFeatureLayer;
            //bool apk = (pEngineEditor as IEngineEditLayers).IsEditable(pFeatureLayer);


            var workspaceEdit = (IWorkspaceEdit)workspace;
            workspaceEdit.StartEditing(true);
            workspaceEdit.StartEditOperation();

            var djq = targetFeatureWorkspace.OpenFeatureClass("DJQ");
            var query2 = new QueryFilterClass();
            query2.WhereClause = "DJQDM ='321203004'";
            IFeatureCursor pCursor = djq.Search(query2,false);
            IFeature pFea = null;
            while (true)
            {
                pFea = pCursor.NextFeature();

                pFea.Delete();  //因为图层所在数据集没有注册版本,这行报错为: 未更新任何行 [No rows were updated]
                pFea.Store();
                break;
            }

            Marshal.ReleaseComObject(pCursor);

            if (workspaceEdit.IsBeingEdited())
            {
                //结束一个编辑会话,保存数据数据库内
                workspaceEdit.StopEditOperation();
                workspaceEdit.StopEditing(true);
            }

            //结论:如果没有注册版本,可以开始编辑,也可以结束编辑,可在具体操作图层时,会报未更新任何行。
        }
    }
}

 

        [STAThread]
        static void Main()
        {

            ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.Desktop);
            ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.EngineOrDesktop);
            IAoInitialize aoInitialize = new AoInitialize();
            esriLicenseStatus licenseStatus = esriLicenseStatus.esriLicenseUnavailable;
            licenseStatus = aoInitialize.Initialize(esriLicenseProductCode.esriLicenseProductCodeAdvanced);


            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }

 

posted on 2024-11-21 12:31  赵长青  阅读(2)  评论(0编辑  收藏  举报