前面有几篇文章专门介绍了空间数据的导入,导入的目的是为了统一管理。今天介绍空间数据导出,导出的格式支持和导入的格式一样,导出的目的是为了方便数据的迁移。其实导入和导出用到的技术基本上都是相同的,不过为了介绍的完整性还是单独拿出来,因为这一部分的功能也是很重要而且是必不可少的!
1.首先定义一个用于操作SDE数据库的工作空间并且在构造函数中初始化(调用工具类里面提供的静态方法初始化):
- private IFeatureWorkspace pWorkspaceSDE;//定义SDE工作空间
- public FrmDataExport()
- {
- InitializeComponent();
- if (pWorkspaceSDE == null)
- {
- pWorkspaceSDE = MapOperation.GetFeatrueWorkspace();
- }
- }
2.列出所有数据表:供用户选择需要导出的数据,每一个表是一个可选项,这样用户可以一次导出多个需要的数据表。
- /// <summary>
- /// 列出所有的表信息
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void FrmDataExport_Load(object sender, EventArgs e)
- {
- SqlHelper sh = new SqlHelper();
- string sql = string.Empty;
- sql = "select table_name,table_mapname,type from layer l,element e where "
- + "e.id=l.pid and e.category='矢量数据'";
- OracleDataReader odr = sh.ReturnDataReader(sql);
- object[] obj = new object[4];
- while (odr.Read())
- {
- obj[0] = false;
- obj[1] = odr[0].ToString();
- obj[2] = odr[2].ToString();
- obj[3] = odr[1].ToString();
- dataGridViewX1.Rows.Add(obj);
- }
- comboBoxEx1.SelectedIndex = 0;
- }
3.根据选择的导出数据格式打开相应的文件
- /// <summary>
- /// 根据选择的导出数据格式打开相应的文件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void selectPathBtn_Click(object sender, EventArgs e)
- {
- //根据导出数据格式打开相应的文件
- switch (comboBoxEx1.SelectedIndex)
- {
- case 0:
- {
- FolderBrowserDialog folder = new FolderBrowserDialog();
- if (folder.ShowDialog() == DialogResult.OK)
- {
- if (folder.SelectedPath != "")
- {
- selectPathTxt.Text = folder.SelectedPath;
- }
- }
- }
- break;
- case 1:
- {
- OpenFileDialog ofd = new OpenFileDialog();
- ofd.Filter = "MDB文件(.mdb) | *.mdb";
- ofd.CheckFileExists = false;
- if (ofd.ShowDialog() == DialogResult.OK)
- {
- if (ofd.FileName != "")
- {
- selectPathTxt.Text = ofd.FileName;
- }
- }
- }
- break;
- default:
- break;
- }
- }
4.执行具体的导出功能:一起准备工作都做好了就开始执行具体的导出功能了,根据不同的格式执行相应导出格式的功能。
- /// <summary>
- /// 执行具体的导出功能
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void exportBtn_Click(object sender, EventArgs e)
- {
- if (selectPathTxt.Text == "")
- {
- MessageBox.Show("请选择导出路劲");
- return;
- }
- IWorkspaceFactory pWF = null;
- switch (comboBoxEx1.SelectedIndex)
- {
- case 0:
- {
- if (!File.Exists(selectPathTxt.Text))
- {
- }
- //创建一个输出shp文件的工作空间
- pWF = new ShapefileWorkspaceFactoryClass();
- IFeatureWorkspace pFW = pWF.OpenFromFile(selectPathTxt.Text, 0) as IFeatureWorkspace;
- IWorkspace pW = pFW as IWorkspace;
- for (int i = 0; i < dataGridViewX1.Rows.Count; ++i)
- {
- if (bool.Parse(dataGridViewX1.Rows[i].Cells[0].Value.ToString()))
- {
- if (dataGridViewX1.Rows[i].Cells[2].Value.ToString() != "PA")
- {
- string str = dataGridViewX1.Rows[i].Cells[1].Value.ToString();
- MapOperation.ConvertFeatureClass(pWorkspaceSDE as IWorkspace,
- pW, str, str, 4326);
- }
- else
- {
- MessageBox.Show("属性表不能够导出为Shape文件");
- }
- }
- }
- MessageBox.Show("导出数据完成!");
- }
- break;
- case 1:
- {
- // Instantiate an Access workspace factory and create a new personal geodatabase.
- pWF = new AccessWorkspaceFactoryClass();
- IWorkspaceName pWN = pWF.Create(Path.GetDirectoryName(selectPathTxt.Text),
- Path.GetFileName(selectPathTxt.Text),null, 0);
- // Cast the workspace name object to the IName interface and open the workspace.
- IName pN = (IName)pWN;
- IWorkspace pW = (IWorkspace)pN.Open();
- for (int i = 0; i < dataGridViewX1.Rows.Count; ++i)
- {
- if (bool.Parse(dataGridViewX1.Rows[i].Cells[0].Value.ToString()))
- {
- string str = dataGridViewX1.Rows[i].Cells[1].Value.ToString();
- if (dataGridViewX1.Rows[i].Cells[2].Value.ToString() != "PA")
- {
- MapOperation.ConvertFeatureClass(pWorkspaceSDE as IWorkspace,
- pW, str, str, 4326);
- }
- else
- {
- ITable pSourceT = pWorkspaceSDE.OpenTable(str);
- IFeatureWorkspace pFW = pW as IFeatureWorkspace;
- ITable pTargetT = pFW.CreateTable(str, pSourceT.Fields, null, null, "");
- FusedIndexTable(ref pSourceT, ref pTargetT);
- }
- }
- }
- MessageBox.Show("导出数据完成!");
- }
- break;
- default:
- break;
- }
- }
5.如果导出的数据表或文件已经存在就以追加的方式导出数据
- /// <summary>
- /// 如果目的数据库中已经有表,则将新的记录追加进去
- /// </summary>
- /// <param name="FromTable">导出表</param>
- /// <param name="ToTable">导入表</param>
- private void FusedIndexTable(ref ITable FromTable, ref ITable ToTable)
- {
- if (FromTable == null || ToTable == null)
- {
- return;
- }
- IRow pFromRow;
- ICursor pToCursor, pFromCursor;
- IRowBuffer pToRowBuffer;
- int pIndex;
- pToRowBuffer = ToTable.CreateRowBuffer();
- pToCursor = ToTable.Insert(true);
- pFromCursor = FromTable.Search(null, false);
- pFromRow = pFromCursor.NextRow();
- while (pFromRow != null)
- {
- for (int i = 0; i < pFromRow.Fields.FieldCount; i++)
- {
- pIndex = pToRowBuffer.Fields.FindField(pFromRow.Fields.get_Field(i).Name.Trim());
- if (pFromRow.Fields.get_Field(i).Editable && pIndex > -1)
- {
- pToRowBuffer.set_Value(pIndex, pFromRow.get_Value(i));
- }
- }
- pToCursor.InsertRow(pToRowBuffer);
- pFromRow = pFromCursor.NextRow();
- }
- System.Runtime.InteropServices.Marshal.ReleaseComObject(pToCursor);
- pFromRow = null;
- pFromCursor = null;
- pToRowBuffer = null;
- }
6.总结:这里用到的大部分技术在前面都介绍过了,这里不过是不同的业务逻辑而已,其实很多的时候高深的技术并不会用到很多,主要是处理好各个功能的业务逻辑,至于用什么样的技术实现都是可以的!