我的獨立部落格網址是:http://wuyouqiang.sinaapp.com/。
我的新浪微網誌:http://weibo.com/freshairbrucewoo。
歡迎大家互相交流,共同提高技術。
前面有幾篇文章專門介紹了空間資料的導入,導入的目的是為了統一管理。今天介紹空間資料導出,導出的格式支援和導入的格式一樣,導出的目的是為了友善資料的遷移。其實導入和導出用到的技術基本上都是相同的,不過為了介紹的完整性還是單獨拿出來,因為這一部分的功能也是很重要而且是必不可少的!
1.首先定義一個用于操作SDE資料庫的工作空間并且在構造函數中初始化(調用工具類裡面提供的靜态方法初始化):
1 private IFeatureWorkspace pWorkspaceSDE;//定義SDE工作空間
2 public FrmDataExport()
3 {
4 InitializeComponent();
5 if (pWorkspaceSDE == null)
6 {
7 pWorkspaceSDE = MapOperation.GetFeatrueWorkspace();
8 }
9 }
2.列出所有資料表:供使用者選擇需要導出的資料,每一個表是一個可選項,這樣使用者可以一次導出多個需要的資料表。
1 /// <summary>
2 /// 列出所有的表資訊
3 /// </summary>
4 /// <param name="sender"></param>
5 /// <param name="e"></param>
6 private void FrmDataExport_Load(object sender, EventArgs e)
7 {
8 SqlHelper sh = new SqlHelper();
9 string sql = string.Empty;
10 sql = "select table_name,table_mapname,type from layer l,element e where "
11 + "e.id=l.pid and e.category='矢量資料'";
12 OracleDataReader odr = sh.ReturnDataReader(sql);
13 object[] obj = new object[4];
14 while (odr.Read())
15 {
16 obj[0] = false;
17 obj[1] = odr[0].ToString();
18 obj[2] = odr[2].ToString();
19 obj[3] = odr[1].ToString();
20 dataGridViewX1.Rows.Add(obj);
21 }
22 comboBoxEx1.SelectedIndex = 0;
23 }
3.根據選擇的導出資料格式打開相應的檔案
1 /// <summary>
2 /// 根據選擇的導出資料格式打開相應的檔案
3 /// </summary>
4 /// <param name="sender"></param>
5 /// <param name="e"></param>
6 private void selectPathBtn_Click(object sender, EventArgs e)
7 {
8 //根據導出資料格式打開相應的檔案
9 switch (comboBoxEx1.SelectedIndex)
10 {
11 case 0:
12 {
13 FolderBrowserDialog folder = new FolderBrowserDialog();
14 if (folder.ShowDialog() == DialogResult.OK)
15 {
16 if (folder.SelectedPath != "")
17 {
18 selectPathTxt.Text = folder.SelectedPath;
19 }
20 }
21 }
22 break;
23 case 1:
24 {
25 OpenFileDialog ofd = new OpenFileDialog();
26 ofd.Filter = "MDB檔案(.mdb) | *.mdb";
27 ofd.CheckFileExists = false;
28
29 if (ofd.ShowDialog() == DialogResult.OK)
30 {
31
32 if (ofd.FileName != "")
33 {
34 selectPathTxt.Text = ofd.FileName;
35 }
36 }
37 }
38 break;
39 default:
40 break;
41 }
42 }
4.執行具體的導出功能:一起準備工作都做好了就開始執行具體的導出功能了,根據不同的格式執行相應導出格式的功能。
1 /// <summary>
2 /// 執行具體的導出功能
3 /// </summary>
4 /// <param name="sender"></param>
5 /// <param name="e"></param>
6 private void exportBtn_Click(object sender, EventArgs e)
7 {
8 if (selectPathTxt.Text == "")
9 {
10 MessageBox.Show("請選擇導出路勁");
11 return;
12 }
13 IWorkspaceFactory pWF = null;
14 switch (comboBoxEx1.SelectedIndex)
15 {
16 case 0:
17 {
18 if (!File.Exists(selectPathTxt.Text))
19 {
20
21 }
22 //建立一個輸出shp檔案的工作空間
23 pWF = new ShapefileWorkspaceFactoryClass();
24 IFeatureWorkspace pFW = pWF.OpenFromFile(selectPathTxt.Text, 0) as IFeatureWorkspace;
25 IWorkspace pW = pFW as IWorkspace;
26
27 for (int i = 0; i < dataGridViewX1.Rows.Count; ++i)
28 {
29 if (bool.Parse(dataGridViewX1.Rows[i].Cells[0].Value.ToString()))
30 {
31 if (dataGridViewX1.Rows[i].Cells[2].Value.ToString() != "PA")
32 {
33 string str = dataGridViewX1.Rows[i].Cells[1].Value.ToString();
34 MapOperation.ConvertFeatureClass(pWorkspaceSDE as IWorkspace,
35 pW, str, str, 4326);
36 }
37 else
38 {
39 MessageBox.Show("屬性表不能夠導出為Shape檔案");
40 }
41 }
42 }
43 MessageBox.Show("導出資料完成!");
44 }
45 break;
46 case 1:
47 {
48 // Instantiate an Access workspace factory and create a new personal geodatabase.
49 pWF = new AccessWorkspaceFactoryClass();
50 IWorkspaceName pWN = pWF.Create(Path.GetDirectoryName(selectPathTxt.Text),
51 Path.GetFileName(selectPathTxt.Text),null, 0);
52
53 // Cast the workspace name object to the IName interface and open the workspace.
54 IName pN = (IName)pWN;
55 IWorkspace pW = (IWorkspace)pN.Open();
56
57 for (int i = 0; i < dataGridViewX1.Rows.Count; ++i)
58 {
59 if (bool.Parse(dataGridViewX1.Rows[i].Cells[0].Value.ToString()))
60 {
61 string str = dataGridViewX1.Rows[i].Cells[1].Value.ToString();
62 if (dataGridViewX1.Rows[i].Cells[2].Value.ToString() != "PA")
63 {
64 MapOperation.ConvertFeatureClass(pWorkspaceSDE as IWorkspace,
65 pW, str, str, 4326);
66 }
67 else
68 {
69 ITable pSourceT = pWorkspaceSDE.OpenTable(str);
70 IFeatureWorkspace pFW = pW as IFeatureWorkspace;
71 ITable pTargetT = pFW.CreateTable(str, pSourceT.Fields, null, null, "");
72 FusedIndexTable(ref pSourceT, ref pTargetT);
73 }
74 }
75 }
76 MessageBox.Show("導出資料完成!");
77 }
78 break;
79 default:
80 break;
81 }
82 }
5.如果導出的資料表或檔案已經存在就以追加的方式導出資料
1 /// <summary>
2 /// 如果目的資料庫中已經有表,則将新的記錄追加進去
3 /// </summary>
4 /// <param name="FromTable">導出表</param>
5 /// <param name="ToTable">導入表</param>
6 private void FusedIndexTable(ref ITable FromTable, ref ITable ToTable)
7 {
8 if (FromTable == null || ToTable == null)
9 {
10 return;
11 }
12 IRow pFromRow;
13 ICursor pToCursor, pFromCursor;
14 IRowBuffer pToRowBuffer;
15 int pIndex;
16
17 pToRowBuffer = ToTable.CreateRowBuffer();
18 pToCursor = ToTable.Insert(true);
19 pFromCursor = FromTable.Search(null, false);
20 pFromRow = pFromCursor.NextRow();
21 while (pFromRow != null)
22 {
23 for (int i = 0; i < pFromRow.Fields.FieldCount; i++)
24 {
25 pIndex = pToRowBuffer.Fields.FindField(pFromRow.Fields.get_Field(i).Name.Trim());
26 if (pFromRow.Fields.get_Field(i).Editable && pIndex > -1)
27 {
28 pToRowBuffer.set_Value(pIndex, pFromRow.get_Value(i));
29 }
30 }
31
32 pToCursor.InsertRow(pToRowBuffer);
33 pFromRow = pFromCursor.NextRow();
34 }
35 System.Runtime.InteropServices.Marshal.ReleaseComObject(pToCursor);
36 pFromRow = null;
37 pFromCursor = null;
38 pToRowBuffer = null;
39 }
40 }
6.總結:這裡用到的大部分技術在前面都介紹過了,這裡不過是不同的業務邏輯而已,其實很多的時候高深的技術并不會用到很多,主要是處理好各個功能的業務邏輯,至于用什麼樣的技術實作都是可以的!