/**
* pDataSet:要导出的数据库的数据集,如果直接导入到数据库而不是数据集,则使用强制转换IWorkspace-->IDataset
* strFeatFileDir:要导入的shape文件所在目录
* strFeatFileName:要导入的shape文件名
* isWorkspace:标识是导入数据库还是数据集
*strOutName:存储于数据库的文件名
*failInfo:导入失败文件名
*errorDetail:错误信息
*skipInfo:发现重名情况跳过文件名
**/
public void FeatureClassToFeatureClass(IDataset pDataSet, string strFeatFileDir, string strFeatFileName, string strOutName, bool isWorkspace, ref string failInfo, ref string errorDetail, ref string skipInfo)
{
try
{
IWorkspaceFactory pWSF = new ShapefileWorkspaceFactoryClass();
IFeatureWorkspace pFeatureWK = (IFeatureWorkspace)pWSF.OpenFromFile(strFeatFileDir, 0);
IFeatureClass pInFeatureClass = pFeatureWK.OpenFeatureClass(strFeatFileName);
if (pInFeatureClass == null || pDataSet == null)
{
if (failInfo == "")
{
failInfo = strFeatFileDir + "\\" + strFeatFileName;
errorDetail = "错误信息:" + strFeatFileDir + "是空的矢量工作空间。";
}
else
{
failInfo += ";" + strFeatFileDir + "\\" + strFeatFileName;
errorDetail += ";" + "错误信息:" + strFeatFileDir + "是空的矢量工作空间。";
}
return;
}
IFeatureClassName pInFeatureclassName;
IDataset pIndataset = (IDataset)pInFeatureClass;
pInFeatureclassName = (IFeatureClassName)pIndataset.FullName;
//如果名称已存在
IWorkspace2 pWS2 = null;
if (isWorkspace)
pWS2 = pDataSet as IWorkspace2;
else
pWS2 = pDataSet.Workspace as IWorkspace2;
if (pWS2.get_NameExists(esriDatasetType.esriDTFeatureClass, strOutName))
{
DialogResult result;
result = System.Windows.Forms.MessageBox.Show(null, "矢量文件名 " + strOutName + " 在数据库中已存在!" + "\r是否覆盖?", "相同文件名", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
//覆盖原矢量要素
if (result == DialogResult.Yes)
{
IFeatureWorkspace pFWS = (IFeatureWorkspace)pWS2;
IDataset pDataset = pFWS.OpenFeatureClass(strOutName) as IDataset;
pDataset.Delete();
pDataset = null;
}
else if (result == DialogResult.No)
{
//不覆盖,则退出for循环,忽略这个要素,转入下一个要素的导入
if (skipInfo == "")
skipInfo = strFeatFileDir + "\\" + strFeatFileName;
else
skipInfo += ";" + strFeatFileDir + "\\" + strFeatFileName;
return;
}
}
IFields pInFields, pOutFields;
IFieldChecker pFieldChecker = new FieldCheckerClass();
IEnumFieldError pError;
pInFields = pInFeatureClass.Fields;
pFieldChecker.Validate(pInFields, out pError, out pOutFields);
IField geoField = null;
for (int i = 0; i < pOutFields.FieldCount; i++)
{
IField pField = pOutFields.get_Field(i);
if (pField.Type == esriFieldType.esriFieldTypeOID)
{
IFieldEdit pFieldEdit = (IFieldEdit)pField;
pFieldEdit.Name_2 = pField.AliasName;
}
if (pField.Type == esriFieldType.esriFieldTypeGeometry)
{
geoField = pField;
break;
}
}
IGeometryDef geometryDef = geoField.GeometryDef;
IFeatureDataConverter one2another = new FeatureDataConverterClass();
IFeatureClassName pOutFeatureClassName = new FeatureClassNameClass();
IDatasetName pOutDatasetName = (IDatasetName)pOutFeatureClassName;
if (isWorkspace)
pOutDatasetName.WorkspaceName = (IWorkspaceName)pDataSet.FullName;
else
pOutDatasetName.WorkspaceName = (IWorkspaceName)((IDataset)pDataSet.Workspace).FullName;
pOutDatasetName.Name = strOutName;
if (isWorkspace)
{
one2another.ConvertFeatureClass(pInFeatureclassName, null, null, pOutFeatureClassName, geometryDef,
pOutFields, "", 1000, 0);
}
else
{
IFeatureDataset pFeatDS = (IFeatureDataset)pDataSet;
IFeatureDatasetName pOutFeatDSName = pFeatDS.FullName as IFeatureDatasetName;
one2another.ConvertFeatureClass(pInFeatureclassName, null, pOutFeatDSName, pOutFeatureClassName, geometryDef,
pOutFields, "", 1000, 0);
pOutFeatDSName = null;
pFeatDS = null;
}
}
catch (Exception ex)
{
if (failInfo == "")
{
failInfo = strFeatFileDir + "\\" + strFeatFileName;
errorDetail = "错误信息:" + ex.Message;
}
else
{
failInfo += ";" + strFeatFileDir + "\\" + strFeatFileName;
errorDetail += ";" + "错误信息:" + ex.Message;
}
Console.WriteLine(ex.Message);
return;
}
}
转载自:http://www.cnblogs.com/bluemaplestudio/archive/2010/04/13/1710747.html 蓝枫工作室