zoukankan      html  css  js  c++  java
  • 基于ArcGIS10.0和Oracle10g的空间数据管理平台十(C#开发)空间数据导入RDBMS上MDB格式

    我的独立博客网址是:http://wuyouqiang.sinaapp.com/

    我的新浪微博:http://weibo.com/freshairbrucewoo

    欢迎大家相互交流,共同提高技术。

        前面第九篇把Shape格式导入已经完成了,在这个以后我又陆续加入其他几篇这个项目相关知识介绍的博文,加入那些知识的目的是为了让博友们能够更加清楚这个项目的一切相关知识,以便更好的理解这个项目。现在开始完成MDB格式导入Oracle数据库。

        和Shape相同的部分就不在讲解了,只讲解与Shape不同的实现部分!

    1.添加一个MDB格式的空间数据文件到导入列表,并显示其相关信息。

     1         ///<summary>
    2 /// 添加一个MDB格式的文件到导入控件列表
    3 ///</summary>
    4 ///<param name="sender"></param>
    5 ///<param name="e"></param>
    6 private void addFileBtn_Click(object sender, EventArgs e)
    7 {
    8 OpenFileDialog ofd = new OpenFileDialog();
    9
    10 //打开MDB文件
    11 string StrFilter = "MDB文件(.mdb) | *.mdb";
    12 ofd.Filter = StrFilter;
    13 string ImportMDBName;
    14 if (ofd.ShowDialog() == DialogResult.OK)
    15 {
    16 ImportMDBName = ofd.FileName;
    17 }
    18 else
    19 {
    20 return;
    21 }
    22 //得到文件名和目录名
    23 string ImportFileName = System.IO.Path.GetFileName(ImportMDBName);
    24 string ImportFilePath = System.IO.Path.GetDirectoryName(ImportMDBName);
    25 //检查文件是否已经存在DataGridView中,没有就加载到其中
    26 if (!fileIsExist(ImportFileName))
    27 {
    28 object[] obj = new object[2];
    29 obj[0] = ImportFileName;
    30 obj[1] = ImportFilePath;
    31 dataGridViewX1.Rows.Add(obj);
    32 addTableNameToDataGridView(ImportMDBName);
    33 }
    34 }

       这个函数又调用了一个专门用于检测文件是否已经被导入了的函数,实现如下:

     1         ///<summary>
    2 /// 判断文件名是否已经存在在DataGridView中了
    3 ///</summary>
    4 ///<param name="filename"></param>
    5 ///<returns></returns>
    6 private bool fileIsExist(string filename)
    7 {
    8 bool result = false;
    9
    10 for (int i = 0; i < dataGridViewX1.Rows.Count; ++i)
    11 {
    12 if (filename == dataGridViewX1.Rows[i].Cells[0].Value.ToString())
    13 {
    14 result = true;
    15 break;
    16 }
    17 }
    18 return result;
    19 }

    2.读出一个MDB格式文件里所有的表相关信息并加载到控件中显示。

      1         ///<summary>
    2 /// 读出一个MDB格式文件里所有的表相关信息并加载到控件中显示
    3 ///</summary>
    4 ///<param name="filename"></param>
    5 private void addTableNameToDataGridView(string filename)
    6 {
    7 //打开mdb文件所在的工作空间
    8 IWorkspaceFactory wf = new AccessWorkspaceFactory();
    9 IFeatureWorkspace pAccessFW = wf.OpenFromFile(filename, 0) as IFeatureWorkspace;
    10 IWorkspace pAccessW = pAccessFW as IWorkspace;
    11
    12 object[] obj = new object[5];
    13 SqlHelper sh = new SqlHelper();
    14 string sql = string.Empty;
    15
    16 //1.遍历mdb的每一个要素集,并加载表信息到DataGridViewX2中
    17 IEnumDataset enumDataset = pAccessW.get_Datasets(esriDatasetType.esriDTFeatureDataset);
    18 IFeatureDataset featureDs = enumDataset.Next() as IFeatureDataset;
    19
    20 while (featureDs != null)
    21 {
    22 IFeatureClass pFeatureClass;
    23 IFeatureClassContainer fcContainer = featureDs as IFeatureClassContainer;
    24 for (int i = 0; i < fcContainer.ClassCount; i++)
    25 {
    26 pFeatureClass = fcContainer.get_Class(i);
    27 obj[0] = true;
    28 obj[1] = pFeatureClass.AliasName;
    29 obj[2] = pFeatureClass.AliasName;
    30 sql = "select table_name from layer l,element e where table_name='"
    31 + pFeatureClass.AliasName.ToLower()
    32 + "' and e.id=l.pid and e.category='矢量数据'";
    33 if (sh.GetRecordCount(sql) > 0)
    34 {
    35 obj[3] = "";
    36 }
    37 else
    38 {
    39 obj[3] = "";
    40 }
    41 //检查此表结构是否符合标准
    42 if (CheckFields(pFeatureClass.Fields, pFeatureClass.AliasName))
    43 {
    44 obj[4] = true;
    45 }
    46 else
    47 {
    48 obj[4] = false;
    49 }
    50 dataGridViewX2.Rows.Add(obj);
    51 }
    52
    53 featureDs = enumDataset.Next() as IFeatureDataset;
    54 }
    55
    56 //2.遍历mdb的每一个独立要素类,,并加载表信息到DataGridViewX2中
    57 enumDataset = pAccessW.get_Datasets(esriDatasetType.esriDTFeatureClass);
    58 IDataset dataset = enumDataset.Next();
    59
    60 while (dataset != null)
    61 {
    62 IFeatureClass pFeatureClass = dataset as IFeatureClass;
    63
    64 obj[0] = true;
    65 obj[1] = pFeatureClass.AliasName;
    66 obj[2] = pFeatureClass.AliasName;
    67 sql = "select table_name from layer l,element e where table_name='"
    68 + pFeatureClass.AliasName.ToLower()
    69 + "' and e.id=l.pid and e.category='矢量数据'";
    70 if (sh.GetRecordCount(sql) > 0)
    71 {
    72 obj[3] = "";
    73 }
    74 else
    75 {
    76 obj[3] = "";
    77 }
    78 //检查此表结构是否符合标准
    79 if (CheckFields(pFeatureClass.Fields, pFeatureClass.AliasName))
    80 {
    81 obj[4] = true;
    82 }
    83 else
    84 {
    85 obj[4] = false;
    86 }
    87 dataGridViewX2.Rows.Add(obj);
    88 dataset = enumDataset.Next();
    89 }
    90
    91 //3.遍历mdb的每一个属性表,,并加载表信息到DataGridViewX2中
    92 enumDataset = pAccessW.get_Datasets(esriDatasetType.esriDTTable);
    93 dataset = enumDataset.Next();
    94
    95 while (dataset != null)
    96 {
    97 ITable t = dataset as ITable;
    98 obj[0] = true;
    99 obj[1] = dataset.Name;
    100 obj[2] = dataset.Name;
    101 sql = "select table_name from layer l,element e where table_name='"
    102 + dataset.Name.ToLower()
    103 + "' and e.id=l.pid and e.category='矢量数据'";
    104 if (sh.GetRecordCount(sql) > 0)
    105 {
    106 obj[3] = "";
    107 }
    108 else
    109 {
    110 obj[3] = "";
    111 }
    112 //检查此表结构是否符合标准
    113 if (CheckFields(t.Fields, dataset.Name))
    114 {
    115 obj[4] = true;
    116 }
    117 else
    118 {
    119 obj[4] = false;
    120 }
    121 dataGridViewX2.Rows.Add(obj);
    122 dataset = enumDataset.Next();
    123 }
    124
    125 //判断所有的是否通过了检查,可以是人为的,也可能是自动检查,通过则使能完成按钮
    126 //否则禁止完成按钮
    127 bool isAll = true;
    128 for (int i = 0; i < dataGridViewX2.Rows.Count; ++i )
    129 {
    130 if (!bool.Parse(dataGridViewX2.Rows[i].Cells[4].Value.ToString()))
    131 {
    132 isAll = false;
    133 break;
    134 }
    135 }
    136 if (isAll)
    137 {
    138 finishBtn.Enabled = true;
    139 }
    140 else
    141 {
    142 finishBtn.Enabled = false;
    143 }
    144 }

        由上面的代码可以看出要读取出MDB文件中所有的表结构信息需要分三种情况,一、所有在数据集里面的表;二、独立(游离)的表;三、属性表。每读取一个表结构的信息就检测这个表的字段是否符合数据库中定义好的数据结构,完成这个功能是调用函数CheckFields,这个函数定义和实现如下所示:

     1         ///<summary>
    2 /// 检查字段是否符合数据标准定义
    3 ///</summary>
    4 ///<param name="pSourceFileds">字段集</param>
    5 ///<param name="strTableName">对应的标准名</param>
    6 ///<returns></returns>
    7 private bool CheckFields(IFields pSourceFileds, string strTableName)
    8 {
    9 SqlHelper sh = new SqlHelper();
    10 string sql = "select * from fielddefine where table_name ='"
    11 + strTableName.ToUpper() + "' or table_name='" + strTableName.ToLower() + "'";
    12
    13 if (pSourceFileds.FieldCount != sh.GetRecordCount(sql))
    14 {
    15 return false;
    16 }
    17 bool result = true;
    18
    19 OracleDataReader odr = sh.ReturnDataReader(sql);
    20 object[] obj1 = new object[4];
    21 object[] obj2 = new object[4];
    22 for (int i = 0; i < pSourceFileds.FieldCount; i++)
    23 {
    24 odr.Read();
    25 if (pSourceFileds.FindField(odr["NAME"].ToString()) >= 0)
    26 {
    27 IField pSourceField = pSourceFileds.get_Field(pSourceFileds.FindField(odr["NAME"].ToString()));
    28
    29 obj1[0] = pSourceField.Name;
    30 obj1[1] = pSourceField.Type;
    31 obj1[2] = pSourceField.Length;
    32 obj1[3] = pSourceField.IsNullable;
    33
    34 obj2[0] = odr["NAME"].ToString();
    35 if (odr["TYPE"].ToString() == "RowID")
    36 {
    37 obj2[1] = esriFieldType.esriFieldTypeOID;
    38 }
    39 else if (odr["type"].ToString() == "整数型")
    40 {
    41 obj2[1] = esriFieldType.esriFieldTypeInteger;
    42 }
    43 else if (odr["type"].ToString() == "浮点型")
    44 {
    45 obj2[1] = esriFieldType.esriFieldTypeDouble;
    46 }
    47 else if (odr["type"].ToString() == "字符型")
    48 {
    49 obj2[1] = esriFieldType.esriFieldTypeString;
    50 }
    51 else if (odr["type"].ToString() == "图元")
    52 {
    53 obj2[1] = esriFieldType.esriFieldTypeGeometry;
    54 }
    55 obj2[2] = odr["LENGTH"].ToString();
    56 if (odr["ISNULL"].ToString() == "")
    57 {
    58 obj2[3] = true;
    59 }
    60 else
    61 {
    62 obj2[3] = false;
    63 }
    64
    65 if (obj1[1].ToString() != obj2[1].ToString()
    66 || obj1[2].ToString() != obj2[2].ToString()
    67 || obj1[3].ToString() != obj2[3].ToString())
    68 {
    69 result = false;
    70 break;
    71 }
    72 }
    73 else
    74 {
    75 result = false;
    76 break;
    77 }
    78 }
    79 return result;
    80 }

    3.增加目录,把目录下的所有MDB文件加载进来,在控件中显示这些文件的相关信息。

     1         ///<summary>
    2 /// 增加目录,把目录下的所有MDB文件加载进来
    3 ///</summary>
    4 ///<param name="sender"></param>
    5 ///<param name="e"></param>
    6 private void addDirectoryBtn_Click(object sender, EventArgs e)
    7 {
    8 FolderBrowserDialog folder = new FolderBrowserDialog();
    9 if (folder.ShowDialog() == DialogResult.OK)
    10 {
    11 string strPath = folder.SelectedPath;
    12 if (strPath == "")
    13 {
    14 return;
    15 }
    16
    17 string[] fileNames = Directory.GetFiles(strPath);
    18 string strTemp;
    19 foreach (string str in fileNames)
    20 {
    21 strTemp = str.Remove(0, str.LastIndexOf('\\') + 1);
    22 if (strTemp.Substring(strTemp.IndexOf('.')) == ".mdb")
    23 {
    24 if (!fileIsExist(strTemp))
    25 {
    26 object[] obj = new object[2];
    27 //strTemp = strTemp.Remove(strTemp.IndexOf('.'));
    28 obj[0] = strTemp;
    29 obj[1] = strPath;
    30 dataGridViewX1.Rows.Add(obj);
    31 addTableNameToDataGridView(str);
    32 }
    33 }
    34 }
    35 }
    36 }

        这个函数依次变量目录下的所有MDB文件,然后调用函数fileIsExist判断文件是否已经加载了,最后调用前面介绍的函数addTableNameToDataGridView把具体的文件相关信息加载到控件中显示。

    4.从MDB文件信息显示控件中删除一个文件,并联动删除在另一个控件中的相应的表结构信息。

     1         ///<summary>
    2 /// 从DataGridViewX1中删除MDB文件,并删除DataGridViewX2中属于这个MDB文件的表
    3 ///</summary>
    4 ///<param name="sender"></param>
    5 ///<param name="e"></param>
    6 private void delFileBtn_Click(object sender, EventArgs e)
    7 {
    8 if (dataGridViewX1.CurrentRow != null)
    9 {
    10 string strFileName = dataGridViewX1.Rows[dataGridViewX1.CurrentRow.Index].Cells[1].Value.ToString() + "\\" +
    11 dataGridViewX1.Rows[dataGridViewX1.CurrentRow.Index].Cells[0].Value.ToString();
    12 //打开mdb文件所在的工作空间
    13 IWorkspaceFactory wf = new AccessWorkspaceFactory();
    14 IWorkspace2 pAccessW = wf.OpenFromFile(strFileName, 0) as IWorkspace2;
    15
    16 int count = dataGridViewX2.Rows.Count;
    17 for (int i = count-1; i >=0; --i)
    18 {
    19 string strName = dataGridViewX2.Rows[i].Cells[1].Value.ToString();
    20 if (pAccessW.get_NameExists(esriDatasetType.esriDTFeatureClass, strName)
    21 || pAccessW.get_NameExists(esriDatasetType.esriDTTable,strName))
    22 {
    23 dataGridViewX2.Rows.Remove(dataGridViewX2.Rows[i]);
    24 }
    25 }
    26 dataGridViewX1.Rows.Remove(dataGridViewX1.CurrentRow);
    27 }
    28 else
    29 {
    30 MessageBox.Show("请选择一个要删除的行!");
    31 }
    32 }

    5.通过MDB文件找到要素类或者属性表,查找的方法就是依次遍历然后对比给定的表名称。

     1         ///<summary>
    2 /// 通过MDB文件找到要素类或值属性表
    3 ///</summary>
    4 ///<param name="fileName">MDB文件名</param>
    5 ///<param name="strTableName">要素类名称或属性表名</param>
    6 ///<returns></returns>
    7 private object FindTableInMDB(string fileName, string strTableName)
    8 {
    9 //打开mdb文件所在的工作空间
    10 IWorkspaceFactory wf = new AccessWorkspaceFactory();
    11 IFeatureWorkspace pFeatureWorkspaceMDB = wf.OpenFromFile(fileName, 0) as IFeatureWorkspace;
    12 IWorkspace pWorkspaceMDB = pFeatureWorkspaceMDB as IWorkspace;
    13
    14 //1.遍历mdb的每一个要素集
    15 IEnumDataset enumDataset = pWorkspaceMDB.get_Datasets(esriDatasetType.esriDTFeatureDataset);
    16 IFeatureDataset featureDs = enumDataset.Next() as IFeatureDataset;
    17
    18 while (featureDs != null)
    19 {
    20 IFeatureClass pFeatureClass;
    21 IFeatureClassContainer fcContainer = featureDs as IFeatureClassContainer;
    22 for (int i = 0; i < fcContainer.ClassCount; i++)
    23 {
    24 pFeatureClass = fcContainer.get_Class(i);
    25 if (pFeatureClass.AliasName.ToLower() == strTableName.ToLower())
    26 {
    27 return pFeatureClass;
    28 }
    29 }
    30 featureDs = enumDataset.Next() as IFeatureDataset;
    31 }
    32
    33 //2.遍历mdb的每一个独立要素类
    34 enumDataset = pWorkspaceMDB.get_Datasets(esriDatasetType.esriDTFeatureClass);
    35 IDataset dataset = enumDataset.Next();
    36
    37 while (dataset != null)
    38 {
    39 IFeatureClass pFeatureClass = dataset as IFeatureClass;
    40 if (pFeatureClass.AliasName.ToLower() == strTableName.ToLower())
    41 {
    42 return pFeatureClass;
    43 }
    44 dataset = enumDataset.Next();
    45 }
    46
    47 //3.遍历mdb的每一个属性表
    48 enumDataset = pWorkspaceMDB.get_Datasets(esriDatasetType.esriDTTable);
    49 dataset = enumDataset.Next();
    50
    51 while (dataset != null)
    52 {
    53 ITable t = dataset as ITable;
    54 if (dataset.Name.ToLower() == strTableName.ToLower())
    55 {
    56 return t;
    57 }
    58 dataset = enumDataset.Next();
    59 }
    60 return null;
    61 }

    6.转换所有导入的MDB文件中的数据到SDE数据库中。

      1         ///<summary>
    2 /// 转换MDB文件中的数据到SDE数据库中
    3 ///</summary>
    4 ///<param name="fileName">MDB文件名</param>
    5 private void mdbfileToSDE(string fileName)
    6 {
    7 //打开mdb文件所在的工作空间
    8 IWorkspaceFactory wf = new AccessWorkspaceFactory();
    9 IWorkspace pWorkspaceMDB = wf.OpenFromFile(fileName, 0);
    10
    11 IWorkspace2 pW2 = pWorkspaceMDB as IWorkspace2;
    12 //查找属于本文件中的表并导入
    13 for (int i=0; i<dataGridViewX2.Rows.Count; ++i)
    14 {
    15 string strSourceTableName = dataGridViewX2.Rows[i].Cells[1].Value.ToString();
    16 string strTargetTableName = dataGridViewX2.Rows[i].Cells[2].Value.ToString();
    17 //此表是否存在此MDB文件IFeatureClass
    18 if (pW2.get_NameExists(esriDatasetType.esriDTFeatureClass, strSourceTableName))
    19 {
    20 //是否选择需要导入
    21 if (bool.Parse(dataGridViewX2.Rows[i].Cells[0].Value.ToString()))
    22 {
    23 //SDE中是否存在此表了,是就添加记录,否则新建游离图层
    24 if (dataGridViewX2.Rows[i].Cells[3].Value.ToString().Trim() == "")
    25 {
    26 MapOperation.ConvertFeatureClass(pWorkspaceMDB, pWorkspaceSDE as IWorkspace,
    27 strSourceTableName, strTargetTableName, 0);
    28 //插入一条记录到layer中去
    29 IFeatureWorkspace pFW = pWorkspaceMDB as IFeatureWorkspace;
    30 IFeatureClass pFC = pFW.OpenFeatureClass(strTargetTableName);
    31 SqlHelper sh = new SqlHelper();
    32 string sql = "select max(ID) from layer";
    33 OracleDataReader odr = sh.ReturnDataReader(sql);
    34 int result = 0;
    35 if (odr.Read())
    36 {
    37 result = int.Parse(odr[0].ToString());
    38 result += 1;
    39 }
    40 Hashtable ht = new Hashtable();
    41 ht.Add("ID", result);
    42 ht.Add("PID", 2025);
    43 ht.Add("TABLE_NAME", strTargetTableName);
    44 ht.Add("TABLE_MAPNAME", strTargetTableName);
    45 ht.Add("DESCRIPTION", "无辅助信息!");
    46 if (pFC.ShapeType == esriGeometryType.esriGeometryPoint
    47 || pFC.ShapeType == esriGeometryType.esriGeometryMultipoint)
    48 {
    49 ht.Add("TYPE", "PT");
    50 }
    51 else if (pFC.ShapeType == esriGeometryType.esriGeometryLine
    52 || pFC.ShapeType == esriGeometryType.esriGeometryPolyline)
    53 {
    54 ht.Add("TYPE", "PL");
    55 }
    56 else if (pFC.ShapeType == esriGeometryType.esriGeometryPolygon)
    57 {
    58 ht.Add("TYPE", "PY");
    59 }
    60 else
    61 {
    62 ht.Add("TYPE", "PY");
    63 }
    64 sh.Insert("layer", ht);
    65
    66 }
    67 else
    68 {
    69 AddMDBFeatureClassToSDE(pWorkspaceMDB, pWorkspaceSDE as IWorkspace,
    70 strSourceTableName, strTargetTableName);
    71 }
    72 }
    73 }
    74 //ITable
    75 else if (pW2.get_NameExists(esriDatasetType.esriDTTable, strSourceTableName))
    76 {
    77 //是否选择需要导入
    78 if (bool.Parse(dataGridViewX2.Rows[i].Cells[0].Value.ToString()))
    79 {
    80 //SDE中是否存在此表了,是就添加记录,否则新建游离图层
    81 if (dataGridViewX2.Rows[i].Cells[3].Value.ToString().Trim() == "")
    82 {
    83 ITable pSourceT = (ITable)FindTableInMDB(fileName, strSourceTableName);
    84 ITable pTargetT = pWorkspaceSDE.CreateTable(strSourceTableName, pSourceT.Fields,
    85 null, null, "");
    86 FusedIndexTable(ref pSourceT, ref pTargetT);
    87
    88 //插入一条记录到layer中去
    89 SqlHelper sh = new SqlHelper();
    90 string sql = "select max(ID) from layer";
    91 OracleDataReader odr = sh.ReturnDataReader(sql);
    92 int result = 0;
    93 if (odr.Read())
    94 {
    95 result = int.Parse(odr[0].ToString());
    96 result += 1;
    97 }
    98 Hashtable ht = new Hashtable();
    99 ht.Add("ID", result);
    100 ht.Add("PID", 2024);
    101 ht.Add("TABLE_NAME", strTargetTableName);
    102 ht.Add("TABLE_MAPNAME", strTargetTableName);
    103 ht.Add("DESCRIPTION", "无辅助信息!");
    104 ht.Add("TYPE", "PT");
    105 sh.Insert("layer", ht);
    106 }
    107 else
    108 {
    109 ITable pTargetT = pWorkspaceSDE.OpenTable(strTargetTableName);
    110 ITable pSourceT = (ITable)FindTableInMDB(fileName, strSourceTableName);
    111 FusedIndexTable(ref pSourceT, ref pTargetT);
    112 }
    113 }
    114 }
    115 }
    116 }

       从上面代码可以看出,在导入的时候分为属性表和空间表,因为空间表和属性表导入方法不同。在导入的时候又要首先判断是否在Oracle数据库中是否已经存在对应的表,如果没有就需要创建一个新表来保持导入的数据,这个功能在函数MapOperation类ConvertFeatureClass实现,这个函数已经讲解,完成导入以后因为是新建的表结构,就需要在数据字典中添加一条记录,记录的内容就是表的相关信息。如果空间表已经存在那么就调用函数AddMDBFeatureClassToSDE完成数据的追加导入,这个函数实现如下:

     1         ///<summary>
    2 /// 将mdb中要素类转换追加到sde数据库已有的要素类中
    3 ///</summary>
    4 ///<param name="sourceWorkspace"></param>
    5 ///<param name="targetWorkspace"></param>
    6 ///<param name="nameOfSourceFeatureClass"></param>
    7 ///<param name="nameOfTargetFeatureClass"></param>
    8 public void AddMDBFeatureClassToSDE(IWorkspace sourceWorkspace, IWorkspace targetWorkspace,
    9 string nameOfSourceFeatureClass, string nameOfTargetFeatureClass)
    10 {
    11 IFeatureWorkspace pSourceFW = sourceWorkspace as IFeatureWorkspace;
    12 IFeatureClass pSourceFc = pSourceFW.OpenFeatureClass(nameOfSourceFeatureClass);
    13
    14 IFeatureWorkspace pTargetFW = targetWorkspace as IFeatureWorkspace;
    15 IFeatureClass pTargetFc = pTargetFW.OpenFeatureClass(nameOfTargetFeatureClass);
    16
    17 mdb2fc(pSourceFc, pTargetFc);
    18
    19 /*IFeatureWorkspace pSourceFW = sourceWorkspace as IFeatureWorkspace;
    20 ITable pSourceTable = pSourceFW.OpenTable(nameOfSourceFeatureClass);
    21
    22 IFeatureWorkspace pTargerFW = targetWorkspace as IFeatureWorkspace;
    23 ITable pTargetTable = pTargerFW.OpenTable(nameOfTargetFeatureClass);
    24 FusedIndexTable(ref pSourceTable, ref pTargetTable);*/
    25 }

       这个函数又调用mdb2fc函数一个一个字段和值的完成数据导入,继续看这个函数的实现:

     1         ///<summary>
    2 /// 追加要素到SDE的featureclass中
    3 ///</summary>
    4 ///<param name="pSourceFc"></param>
    5 ///<param name="pTargetFc"></param>
    6 private void mdb2fc(IFeatureClass pSourceFc, IFeatureClass pTargetFc)
    7 {
    8 IFeatureCursor pFeaCursor = pSourceFc.Search(null, false);
    9 IFeature pFeature = pFeaCursor.NextFeature();
    10
    11 IField pField = new FieldClass();
    12 int iIndex = 0;
    13
    14 while (pFeature != null)
    15 {
    16 IFeature tempFeature = pTargetFc.CreateFeature();
    17 tempFeature.Shape = pFeature.Shape;
    18
    19 try
    20 {
    21 //添加字段值
    22 for (int j = 0; j < pSourceFc.Fields.FieldCount; j++)
    23 {
    24 pField = pSourceFc.Fields.get_Field(j);
    25 iIndex = tempFeature.Fields.FindField(pField.Name);
    26 if (pField.Editable && iIndex != -1)
    27 {
    28 tempFeature.set_Value(iIndex, pFeature.get_Value(j));
    29 }
    30 }
    31 tempFeature.Store();
    32 }
    33 catch (System.Exception ex)
    34 {
    35 MessageBox.Show("单要素写入异常!" + ex.Message, "提示");
    36 return;
    37 }
    38 pFeature = pFeaCursor.NextFeature();
    39 }
    40 }

        如果是属性表也要首先考虑是否在Oracle数据库存在表结构了,不存在同样需要先要创建一个新的表结构,并且在数据字典中添加一条表的相关信息的记录。创建表以后调用FusedIndexTable函数完成数据的具体导入,如果已经存在表结构那么就直接调用FusedIndexTable函数导入数据,这个函数实现如下所示:

     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 }

    7.点击完成按钮完成导入数据的功能,这种导入数据上上面介绍的那个函数mdbfileToSDE。

     1         ///<summary>
    2 /// 完成按钮事件开始导入数据
    3 ///</summary>
    4 ///<param name="sender"></param>
    5 ///<param name="e"></param>
    6 private void finishBtn_Click(object sender, EventArgs e)
    7 {
    8 if (checkBoxX1.Checked)
    9 {
    10 MessageBox.Show("请选择空间参考系!");
    11 return;
    12 }
    13
    14 if (pWorkspaceSDE != null)
    15 {
    16 int fileCount = dataGridViewX1.Rows.Count;
    17
    18 for (int i = 0; i < fileCount; ++i)
    19 {
    20 string strFileName = dataGridViewX1.Rows[i].Cells[1].Value.ToString() + "\\" +
    21 dataGridViewX1.Rows[i].Cells[0].Value.ToString();
    22 //清空原表数据
    23 if (checkBoxX2.Checked)
    24 {
    25 if (dataGridViewX2.Rows[i].Cells[3].Value.ToString().Trim() == "")
    26 {
    27 string strTableName = dataGridViewX2.Rows[i].Cells[2].Value.ToString();
    28 SqlHelper sh = new SqlHelper();
    29 string sql = "delete from " + strTableName;
    30 sh.ExecuteSQL(sql);
    31 }
    32 }
    33 mdbfileToSDE(strFileName);
    34 }
    35 LogHelp.writeLog(FrmMain.username, "空间数据管理", "空间数据MDB格式转换导入成功");
    36 MessageBox.Show("数据导入完成!");
    37 }
    38 Close();
    39 }

        在导入的时候根据导入的选项来决定具体怎样导入,例如是否检查控件参考系或者时候清除原表中已有的数据。

    8.将mdb中的要素数据集转换到sde数据库中

     1         ///<summary>
    2 /// 将mdb中的要素数据集转换到sde数据库中
    3 ///</summary>
    4 ///<param name="sourceWorkspace"></param>
    5 ///<param name="targetWorkspace"></param>
    6 ///<param name="nameOfSourceFeatureDataset"></param>
    7 ///<param name="nameOfTargetFeatureDataset"></param>
    8 public void ConvertMDBFeatureDatasetToSDE(IWorkspace sourceWorkspace, IWorkspace targetWorkspace,
    9 string nameOfSourceFeatureDataset, string nameOfTargetFeatureDataset)
    10 {
    11 if (sourceWorkspace == null || targetWorkspace == null)
    12 {
    13 return;
    14 }
    15 //创建源工作空间名
    16 IDataset sourceWorkspaceDataset = (IDataset)sourceWorkspace;
    17 IWorkspaceName sourceWorkspaceName = (IWorkspaceName)sourceWorkspaceDataset.FullName;
    18 //创建源数据集名
    19 IFeatureDatasetName sourceFeatureDatasetName = new FeatureDatasetNameClass();
    20 IDatasetName sourceDatasetName = (IDatasetName)sourceFeatureDatasetName;
    21 sourceDatasetName.WorkspaceName = sourceWorkspaceName;
    22 sourceDatasetName.Name = nameOfSourceFeatureDataset;
    23 //创建目标工作空间名
    24 IDataset targetWorkspaceDataset = (IDataset)targetWorkspace;
    25 IWorkspaceName targetWorkspaceName = (IWorkspaceName)targetWorkspaceDataset.FullName;
    26 //创建目标数据集名
    27 IFeatureDatasetName targetFeatureDatasetName = new FeatureDatasetNameClass();
    28 IDatasetName targetDatasetName = (IDatasetName)targetFeatureDatasetName;
    29 targetDatasetName.WorkspaceName = targetWorkspaceName;
    30 targetDatasetName.Name = nameOfTargetFeatureDataset;
    31 //转换(复制)源数据集到目标数据集
    32 IFeatureDataConverter featureDataConverter = new FeatureDataConverter();
    33 featureDataConverter.ConvertFeatureDataset(sourceFeatureDatasetName,
    34 targetFeatureDatasetName, null, "", 1000, 0);
    35
    36 }

    9.当在表信息的控件显示列中选择一个表的时候就在另一个控件中显示表的字段相关信息。

      1         ///<summary>
    2 /// 显示一个表中的字段信息
    3 ///</summary>
    4 ///<param name="sender"></param>
    5 ///<param name="e"></param>
    6 private void dataGridViewX2_Click(object sender, EventArgs e)
    7 {
    8 dataGridViewX3.Rows.Clear();
    9 if (dataGridViewX2.CurrentRow == null)
    10 {
    11 return;
    12 }
    13
    14 int fileCount = dataGridViewX1.Rows.Count;
    15 string strFileName;
    16
    17 for (int i = 0; i < fileCount; ++i)
    18 {
    19 strFileName = dataGridViewX1.Rows[i].Cells[1].Value.ToString() + "\\" +
    20 dataGridViewX1.Rows[i].Cells[0].Value.ToString();
    21 string strTableName = dataGridViewX2.CurrentRow.Cells[1].Value.ToString();
    22 //打开mdb文件所在的工作空间
    23 IWorkspaceFactory wf = new AccessWorkspaceFactory();
    24 IFeatureWorkspace pFeatureWorkspaceMDB = wf.OpenFromFile(strFileName, 0) as IFeatureWorkspace;
    25 IWorkspace pWorkspaceMDB = pFeatureWorkspaceMDB as IWorkspace;
    26
    27 //1.遍历mdb的每一个要素集
    28 IEnumDataset enumDataset = pWorkspaceMDB.get_Datasets(esriDatasetType.esriDTFeatureDataset);
    29 IFeatureDataset featureDs = enumDataset.Next() as IFeatureDataset;
    30 IFields pFs = null;
    31
    32 while (featureDs != null)
    33 {
    34 IFeatureClass pFC = null;
    35 IFeatureClassContainer fcContainer = featureDs as IFeatureClassContainer;
    36 for (int j = 0; j < fcContainer.ClassCount; j++)
    37 {
    38 if (fcContainer.get_Class(j).AliasName.ToLower() == strTableName.ToLower())
    39 {
    40 pFC = fcContainer.get_Class(j);
    41 pFs = pFC.Fields;
    42 break;
    43 }
    44 }
    45 featureDs = enumDataset.Next() as IFeatureDataset;
    46 }
    47 //2.遍历mdb的每一个独立要素类
    48 if (pFs == null)
    49 {
    50 IFeatureClass pFC = null;
    51 enumDataset = pWorkspaceMDB.get_Datasets(esriDatasetType.esriDTFeatureClass);
    52 IDataset dataset = enumDataset.Next();
    53 while (dataset != null)
    54 {
    55 pFC = dataset as IFeatureClass;
    56 if (pFC.AliasName.ToLower() == strTableName.ToLower())
    57 {
    58 pFs = pFC.Fields;
    59 break;
    60 }
    61 dataset = enumDataset.Next();
    62 }
    63 }
    64 //3.遍历mdb的每一个属性表
    65 if (pFs == null)
    66 {
    67 ITable tc = null;
    68 enumDataset = pWorkspaceMDB.get_Datasets(esriDatasetType.esriDTTable);
    69 IDataset dataset = enumDataset.Next();
    70 while (dataset != null)
    71 {
    72 tc = dataset as ITable;
    73 if (dataset.Name.ToLower() == strTableName.ToLower())
    74 {
    75 pFs = tc.Fields;
    76 break;
    77 }
    78 dataset = enumDataset.Next();
    79 }
    80 }
    81
    82 if (pFs != null)
    83 {
    84 object[] obj = new object[4];
    85 for (int j = 0; j < pFs.FieldCount; ++j)
    86 {
    87 IField field = pFs.get_Field(j);
    88 obj[0] = field.Name;
    89 obj[1] = field.Name;
    90 obj[2] = field.Type;
    91 obj[3] = field.Length;
    92
    93 dataGridViewX3.Rows.Add(obj);
    94 }
    95 }
    96 }
    97 //判断所有的是否通过了检查,可以是人为的,也可能是自动检查,通过则使能完成按钮
    98 //否则禁止完成按钮
    99 bool isAll = true;
    100 for (int i = 0; i < dataGridViewX2.Rows.Count; ++i)
    101 {
    102 if (!bool.Parse(dataGridViewX2.Rows[i].Cells[4].Value.ToString()))
    103 {
    104 isAll = false;
    105 break;
    106 }
    107 }
    108 if (isAll)
    109 {
    110 finishBtn.Enabled = true;
    111 }
    112 else
    113 {
    114 finishBtn.Enabled = false;
    115 }
    116 }

    10.打开检查字段信息的对话框,具体检查的实现在以后单独一篇博客具体讲解。

     1         ///<summary>
    2 /// 打开检查字段信息的对话框
    3 ///</summary>
    4 ///<param name="sender"></param>
    5 ///<param name="e"></param>
    6 private void checkFieldBtn_Click(object sender, EventArgs e)
    7 {
    8 int fileCount = dataGridViewX1.Rows.Count;
    9
    10 FrmCheckFiled fcf = new FrmCheckFiled();
    11
    12 for (int i = 0; i < fileCount; ++i)
    13 {
    14 string strFileName = dataGridViewX1.Rows[i].Cells[1].Value.ToString() + "\\" +
    15 dataGridViewX1.Rows[i].Cells[0].Value.ToString();
    16 fcf.AddDataToComboBox(strFileName);
    17 }
    18 fcf.pWorkspaceSDE = pWorkspaceSDE;
    19 fcf.ShowDialog();
    20 }

    11.当表的名称信息改变时,对于是否在Oracle数据库中已存在的信息从新检查并更新显示信息

     1         ///<summary>
    2 /// 当表的名称信息改变时,对于是否在Oracle数据库中已存在的信息从新检查并更新显示信息
    3 ///</summary>
    4 ///<param name="sender"></param>
    5 ///<param name="e"></param>
    6 private void dataGridViewX2_CellValueChanged(object sender, DataGridViewCellEventArgs e)
    7 {
    8 if (dataGridViewX2.Rows.Count > 0)
    9 {
    10 //改变导入的目的表的名称后检查是否存在在数据库中
    11 if (dataGridViewX2.Columns[e.ColumnIndex].HeaderText == "导入表名称")
    12 {
    13 SqlHelper sh = new SqlHelper();
    14 string sql = "select table_name from jcsjk_layer l,jcsjk_element e where table_name='"
    15 + dataGridViewX2.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString().ToLower()
    16 + "' and e.id=l.pid and e.category='地震矢量数据'";
    17 if (sh.GetRecordCount(sql) > 0)
    18 {
    19 dataGridViewX2.Rows[e.RowIndex].Cells[3].Value = "";
    20 }
    21 else
    22 {
    23 dataGridViewX2.Rows[e.RowIndex].Cells[3].Value = "";
    24 }
    25 }
    26 }
    27 }

    12.总结

        MDB空间数据格式的导入所有功能基本介绍完毕。还是检查字段相关信息最复杂。



  • 相关阅读:
    面向对象
    tomcat启动时的java_home和jre_home错误
    C#获取当前程序运行路径的方法集合(转)
    Windows Media Player 打不开怎么办
    CGI/MIME/servlet术语解释
    HTTP 无法注册 URL http://+:8000/。进程不具有此命名空间的访问权限
    使用QuartZ.net来做定时计划任务 ; 值不能为 null。 参数名: assemblyString
    cz.msebera.android.httpclient.conn.ConnectTimeoutException: Connect to /192.168.23.1:8080 timed out(Android访问后台一直说链接超时)
    Java的位运算符——&0xFF的运算与讲解
    android+myeclipse+mysql自定义控件下拉框的数据绑定
  • 原文地址:https://www.cnblogs.com/brucewoo/p/2264002.html
Copyright © 2011-2022 走看看