using System;
using System.Collections.Generic;
using System.Text;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.DataSourcesGDB;
namespace AddSDE
{
public class SDEADD
{
//--------------------------------------------建立SDE的连接------------------------------------------------
private IPropertySet SdeConn(string Server, string Instance, string DataBase, string user, string password)
{
try
{
IPropertySet pProSet = new PropertySetClass();
//-----------------------SDE连接属性的设置------------------------
pProSet.SetProperty("Server", Server);
pProSet.SetProperty("Instance", Instance);
pProSet.SetProperty("Database", DataBase);
pProSet.SetProperty("user", user);
pProSet.SetProperty("password", password);
pProSet.SetProperty("version", "SDE.DEFAULT");
return pProSet;
}
catch
{
return null;
}
}
//-------------------------向空间库添加要素----------------------------
public bool Add_Fea(string Server, string Instance, string DataBase, string user, string password,string FeaName,string FieldValue)
{
IPropertySet pProSet = new PropertySetClass();
IWorkspaceFactory pWorkSpFac = new SdeWorkspaceFactoryClass();
IFeatureWorkspace pFeaWorkSp = null;
IFeatureClass FeaCls = null;
IFeatureBuffer FeaBuffer = null;
IFeatureCursor FeaCursor = null;
try
{
pProSet = SdeConn(Server, Instance, DataBase, user, FeaName);
if (pProSet != null)
{
pFeaWorkSp = (IFeatureWorkspace)(pWorkSpFac.Open(pProSet, 0));//打开要素空间
FeaCls = pFeaWorkSp.OpenFeatureClass(FeaName);//取得要素集
FeaCursor = FeaCls.Insert(true);
FeaBuffer = FeaCls.CreateFeatureBuffer();
IField Fld = new FieldClass();
IFields Flds = new FieldsClass();
//IPoint pPoint = new PointClass();
Flds = FeaCls.Fields;
int i;
for (i = 1; i < Flds.FieldCount - 1; i++)
{
Fld = Flds.get_Field(i);
if (Fld.Type == esriFieldType.esriFieldTypeGeometry)
{
//IGeometry pGeo = pPoint;
//FeaBuffer.set_Value(i, pPoint);
//MessageBox.Show("地理字段!");
}
else
{
if (Fld.Type == esriFieldType.esriFieldTypeInteger)
FeaBuffer.set_Value(i, 0);
else if (Fld.Type == esriFieldType.esriFieldTypeDouble)
FeaBuffer.set_Value(i, 0);
else if (Fld.Type == esriFieldType.esriFieldTypeSmallInteger)
FeaBuffer.set_Value(i, 0);
else if (Fld.Type == esriFieldType.esriFieldTypeString)
FeaBuffer.set_Value(i, FieldValue);
else
{
return false;
}
}
}
FeaCursor.InsertFeature(FeaBuffer);
pFeaWorkSp = null;
pProSet = null;
}
return true;
}
catch
{
return false;
}
}
}
}