1. 程式人生 > >arcengine 連線sde,並對sde內資料進行管理(增刪改)

arcengine 連線sde,並對sde內資料進行管理(增刪改)

1、連線sde,用的是連線檔案,怎麼建立連線檔案,arcgis 工具箱,

如果service引數使用的預設的埠號5151,則必須在建立連線檔案之前必須建立和開啟sde服務,而如果使用sde:sqlserver:ip地址或計算機名,則不需要建立和開啟服務。

location:自己選資料夾路徑;

filename:自己填名字

server:192.168.106.33

service:sde:sqlserver:192.168.106.33;

database:資料庫名稱;

username:使用者名稱

password:密碼

 圖三

 private IFeatureClass GetWrokspace()
        {
            string connectionString = @"D:\123\sde1.sde";
            Type factoryType = Type.GetTypeFromProgID("esriDataSourcesGDB.SdeWorkspaceFactory");
            IWorkspaceFactory2 workspaceFactory2 = (IWorkspaceFactory2)Activator.CreateInstance(factoryType);
            IWorkspace pworkspace = workspaceFactory2.OpenFromFile(connectionString, 1);
            pFeaWS = pworkspace as IFeatureWorkspace;
            pFeatureClas = pFeaWS.OpenFeatureClass("sde.SDE.testPoint");
            IFeatureLayer pFLr = new FeatureLayerClass();
            pFLr.FeatureClass = pFeatureClas;
            axMapControl1.AddLayer(pFLr as ILayer);


            //deleteFeature(pFeatureClas, pFeaWS);


            return pFeatureClas;
        }

   private void deleteFeature(IFeatureClass pFeatureClas, IFeatureWorkspace featureWorkspace)
        {
            //IQueryFilter pQF = new QueryFilterClass();
            //pQF.WhereClause = "點號 ='N182'";
            //IFeatureCursor pFeatureCursor = featureclass.Update(pQF, false);
            //IFeature pFeature = pFeatureCursor.NextFeature();
            //if (pFeature == null) return;
            //while (pFeature != null)
            //{
            //    pFeatureCursor.DeleteFeature();
            //    pFeature = pFeatureCursor.NextFeature();
            //}


            IWorkspaceEdit edit = featureWorkspace as IWorkspaceEdit;
            edit.StartEditing(true);
            edit.StartEditOperation();


            IQueryFilter queryFilter = new QueryFilterClass
            {
                WhereClause = "點號 = 'N181'"
            };


            // Create a ComReleaser for cursor management.
           
                // Create and manage a cursor.
            IFeatureCursor searchCursor = pFeatureClas.Search(queryFilter, false);
              


                // Delete the retrieved features.
                IFeature feature = null;
                while ((feature = searchCursor.NextFeature()) != null)
                {
                    feature.Delete();
                }
              edit.StopEditOperation();
              edit.StopEditing(true);




              axMapControl1.Refresh();
 
        }

 private void addFeature(IFeatureClass pFeatureClas, IFeatureWorkspace featureWorkspace)
        {
            IWorkspaceEdit edit = featureWorkspace as IWorkspaceEdit;
            edit.StartEditing(true);
            edit.StartEditOperation();




            IFeatureBuffer featureBuffer = pFeatureClas.CreateFeatureBuffer();
            IFeatureCursor featureCursor = pFeatureClas.Insert(true);


            featureBuffer.set_Value(1, "haha");
            IPoint point = new PointClass();
            point.X=116.027;
            point.Y =39.678;
            featureBuffer.Shape = point;


            featureCursor.InsertFeature(featureBuffer);


            edit.StopEditOperation();
            edit.StopEditing(true);


            axMapControl1.Refresh();
 
        }

 private void updateFeature(IFeatureClass pFeatureClas, IFeatureWorkspace featureWorkspace)
        {
            IWorkspaceEdit edit = featureWorkspace as IWorkspaceEdit;
            edit.StartEditing(true);
            edit.StartEditOperation();


            IQueryFilter pQF = new QueryFilterClass();
            pQF.WhereClause = "點號 ='N204'";
            IFeatureCursor pFeatureCursor = pFeatureClas.Update(pQF, false);
            int index = pFeatureClas.FindField("點名");
            IFeature pFeature = pFeatureCursor.NextFeature();
            if (pFeature == null) return;
            while (pFeature != null)
            {
                pFeature.set_Value(index, "哈哈222");


                pFeatureCursor.UpdateFeature(pFeature);


                pFeature = pFeatureCursor.NextFeature();
            }




            edit.StopEditOperation();
            edit.StopEditing(true);




            axMapControl1.Refresh();
 
        }