1.首先定義一個用於操作的SDE空間數據庫的工作空間。
- public IFeatureWorkspace pWorkspaceSDE;//SDE數據庫的工作空間
2.加載需要檢查的MDB數據庫文件。
- //設置默認DBM文件爲第一個,並加載它的表到DataGridView
- filenameComboBox.SelectedIndex = 0;
- AddDataToDataGridView(filenameComboBox.SelectedItem.ToString());
第一次加載時默認解析第一個MDB文件裏面的數據表,並且把這些表的信息加載到控件中顯示。加載對應MDB文件的數據表信息的函數如下所示:
- /// <summary>
- /// 將DBM文件中的表加載到DataGridView中
- /// </summary>
- /// <param name="filename"></param>
- private void AddDataToDataGridView(string filename)
- {
- dataGridViewX1.Rows.Clear();
- //打開mdb文件所在的工作空間
- IWorkspaceFactory wf = new AccessWorkspaceFactory();
- IFeatureWorkspace pFeatureWorkspaceMDB = wf.OpenFromFile(filename, 0) as IFeatureWorkspace;
- IWorkspace pWorkspaceMDB = pFeatureWorkspaceMDB as IWorkspace;
- //1.遍歷mdb的每一個要素集
- IEnumDataset enumDataset = pWorkspaceMDB.get_Datasets(esriDatasetType.esriDTFeatureDataset);
- IFeatureDataset featureDs = enumDataset.Next() as IFeatureDataset;
- object[] obj = new object[2];
- while (featureDs != null)
- {
- IFeatureClass pFeatureClass;
- IFeatureClassContainer fcContainer = featureDs as IFeatureClassContainer;
- for (int i = 0; i < fcContainer.ClassCount; i++)
- {
- pFeatureClass = fcContainer.get_Class(i);
- obj[0] = CheckFields(pFeatureClass.Fields, pFeatureClass.AliasName);
- obj[1] = pFeatureClass.AliasName;
- dataGridViewX1.Rows.Add(obj);
- }
- featureDs = enumDataset.Next() as IFeatureDataset;
- }
- //2.遍歷mdb的每一個獨立要素類
- enumDataset = pWorkspaceMDB.get_Datasets(esriDatasetType.esriDTFeatureClass);
- IDataset dataset = enumDataset.Next();
- while (dataset != null)
- {
- IFeatureClass pFeatureClass = dataset as IFeatureClass;
- obj[0] = CheckFields(pFeatureClass.Fields, pFeatureClass.AliasName);
- obj[1] = pFeatureClass.AliasName;
- dataGridViewX1.Rows.Add(obj);
- dataset = enumDataset.Next();
- }
- //3.遍歷mdb的每一個屬性表
- enumDataset = pWorkspaceMDB.get_Datasets(esriDatasetType.esriDTTable);
- dataset = enumDataset.Next();
- while (dataset != null)
- {
- ITable pT = dataset as ITable;
- obj[0] = CheckFields(pT.Fields, dataset.Name);
- obj[1] = dataset.Name;
- dataGridViewX1.Rows.Add(obj);
- dataset = enumDataset.Next();
- }
- }
3.動態增加MDB文件到ComboBox,在打開檢查字段的界面以前可以先添加需要檢查的文件。
- /// <summary>
- /// 增加文件名到ComboBox中
- /// </summary>
- /// <param name="filename">文件名</param>
- public void AddDataToComboBox(string filename)
- {
- filenameComboBox.Items.Add(filename);
- }
4.查字段是否符合數據標準定義,所有數據結構都是預先定義好了的並且存在數據庫中作爲數據字典管理。
- /// <summary>
- /// 檢查字段是否符合數據標準定義
- /// </summary>
- /// <param name="pSourceFileds">字段集</param>
- /// <param name="strTableName">對應的標準名</param>
- /// <returns>true:成功;false:失敗</returns>
- private bool CheckFields(IFields pSourceFileds, string strTableName)
- {
- SqlHelper sh = new SqlHelper();
- //從數據庫定義標準表中查找出字段的定義
- string sql = "select * from jcsjk_fielddefine where table_name ='"
- + strTableName.ToUpper() + "' or table_name='" + strTableName.ToLower() + "'";
- //如果字段數目不同肯定就不能通過檢查了
- if (pSourceFileds.FieldCount != sh.GetRecordCount(sql))
- {
- return false;
- }
- bool result = true;
- OracleDataReader odr = sh.ReturnDataReader(sql);
- object[] obj1 = new object[4];
- object[] obj2 = new object[4];
- //分別檢查字段名稱,字段類型,字段長度和字段是否可以爲空
- for (int i = 0; i < pSourceFileds.FieldCount; i++)
- {
- odr.Read();
- //根據標準中定義的名稱查找檢查的字段名稱
- if (pSourceFileds.FindField(odr["NAME"].ToString()) >= 0)
- {
- IField pSourceField = pSourceFileds.get_Field(pSourceFileds.FindField(odr["NAME"].ToString()));
- obj1[0] = pSourceField.Name;
- obj1[1] = pSourceField.Type;
- obj1[2] = pSourceField.Length;
- obj1[3] = pSourceField.IsNullable;
- obj2[0] = odr["NAME"].ToString();
- if (odr["TYPE"].ToString() == "RowID")
- {
- obj2[1] = esriFieldType.esriFieldTypeOID;
- }
- else if (odr["type"].ToString() == "整數型")
- {
- obj2[1] = esriFieldType.esriFieldTypeInteger;
- }
- else if (odr["type"].ToString() == "浮點型")
- {
- obj2[1] = esriFieldType.esriFieldTypeDouble;
- }
- else if (odr["type"].ToString() == "字符型")
- {
- obj2[1] = esriFieldType.esriFieldTypeString;
- }
- else if (odr["type"].ToString() == "圖元")
- {
- obj2[1] = esriFieldType.esriFieldTypeGeometry;
- }
- obj2[2] = odr["LENGTH"].ToString();
- if (odr["ISNULL"].ToString() == "是")
- {
- obj2[3] = true;
- }
- else
- {
- obj2[3] = false;
- }
- if (obj1[1].ToString() != obj2[1].ToString()
- || obj1[2].ToString() != obj2[2].ToString()
- || obj1[3].ToString() != obj2[3].ToString())
- {
- result = false;
- break;
- }
- }
- else
- {
- result = false;
- break;
- }
- }
- return result;
- }
5.點擊一個具體的表名之後在另一個控件中顯示這個表的字段的檢查結果,主要顯示這個是否成功,如果錯誤提示哪些方面的錯誤,例如字段數量不對(少了還是多了)、字段類型不對、數據的長度不匹配等。
- /// <summary>
- /// 點擊一個具體的表名之後在另一個控件中顯示這個表的字段的檢查結果
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void dataGridViewX1_Click(object sender, EventArgs e)
- {
- if (dataGridViewX1.CurrentRow == null)
- {
- return;
- }
- string strTableName = dataGridViewX1.CurrentRow.Cells[1].Value.ToString();
- IWorkspaceFactory wf = new AccessWorkspaceFactory();
- IFeatureWorkspace pFeatureWorkspaceMDB = wf.OpenFromFile(filenameComboBox.SelectedItem.ToString(), 0) as IFeatureWorkspace;
- IWorkspace pWorkspaceMDB = pFeatureWorkspaceMDB as IWorkspace;
- //1.遍歷mdb的每一個要素集
- IEnumDataset enumDataset = pWorkspaceMDB.get_Datasets(esriDatasetType.esriDTFeatureDataset);
- IFeatureDataset featureDs = enumDataset.Next() as IFeatureDataset;
- IFields pFs = null;
- while (featureDs != null)
- {
- IFeatureClass pFC = null;
- IFeatureClassContainer fcContainer = featureDs as IFeatureClassContainer;
- for (int i = 0; i < fcContainer.ClassCount; i++)
- {
- if (fcContainer.get_Class(i).AliasName.ToLower() == strTableName.ToLower())
- {
- pFC = fcContainer.get_Class(i);
- pFs = pFC.Fields;
- break;
- }
- }
- featureDs = enumDataset.Next() as IFeatureDataset;
- }
- //2.遍歷mdb的每一個獨立要素類
- if (pFs == null)
- {
- IFeatureClass pFC = null;
- enumDataset = pWorkspaceMDB.get_Datasets(esriDatasetType.esriDTFeatureClass);
- IDataset dataset = enumDataset.Next();
- while (dataset != null)
- {
- pFC = dataset as IFeatureClass;
- if (pFC.AliasName.ToLower() == strTableName.ToLower())
- {
- pFs = pFC.Fields;
- break;
- }
- dataset = enumDataset.Next();
- }
- }
- //3.遍歷mdb的每一個屬性表
- if (pFs == null)
- {
- ITable tc = null;
- enumDataset = pWorkspaceMDB.get_Datasets(esriDatasetType.esriDTTable);
- IDataset dataset = enumDataset.Next();
- while (dataset != null)
- {
- tc = dataset as ITable;
- if (dataset.Name.ToLower() == strTableName.ToLower())
- {
- pFs = tc.Fields;
- break;
- }
- dataset = enumDataset.Next();
- }
- }
- SqlHelper sh = new SqlHelper();
- string sql = "select * from jcsjk_fielddefine where table_name ='"
- + strTableName.ToUpper() + "' or table_name='" + strTableName.ToLower() + "'";
- if (sh.GetRecordCount(sql) <= 0)
- {
- errTxt.Text = "標準不存在,不能檢查!";
- return;
- }
- if (pFs.FieldCount != sh.GetRecordCount(sql))
- {
- errTxt.Text = "標準檢查未通過,字段數量不匹配!";
- return ;
- }
- OracleDataReader odr = sh.ReturnDataReader(sql);
- int count = 0;
- object[] obj = new object[2];
- for (int i = 0; i < pFs.FieldCount; i++)
- {
- odr.Read();
- //字段是否存在
- if (pFs.FindField(odr["NAME"].ToString()) >= 0)
- {
- IField pSourceField = pFs.get_Field(pFs.FindField(odr["NAME"].ToString()));
- //1.檢查字段類型
- if (odr["TYPE"].ToString() == "RowID")
- {
- if (pSourceField.Type != esriFieldType.esriFieldTypeOID)
- {
- count++;
- obj[0] = pSourceField.AliasName;
- obj[1] = "字段類型錯誤";
- dataGridViewX2.Rows.Add(obj);
- continue;
- }
- }
- else if (odr["type"].ToString() == "整數型")
- {
- if (pSourceField.Type != esriFieldType.esriFieldTypeInteger)
- {
- count++;
- obj[0] = pSourceField.AliasName;
- obj[1] = "字段類型錯誤";
- dataGridViewX2.Rows.Add(obj);
- continue;
- }
- }
- else if (odr["type"].ToString() == "浮點型")
- {
- if (pSourceField.Type != esriFieldType.esriFieldTypeDouble)
- {
- count++;
- obj[0] = pSourceField.AliasName;
- obj[1] = "字段類型錯誤";
- dataGridViewX2.Rows.Add(obj);
- continue;
- }
- }
- else if (odr["type"].ToString() == "字符型")
- {
- if (pSourceField.Type != esriFieldType.esriFieldTypeString)
- {
- count++;
- obj[0] = pSourceField.AliasName;
- obj[1] = "字段類型錯誤";
- dataGridViewX2.Rows.Add(obj);
- continue;
- }
- }
- else if (odr["type"].ToString() == "圖元")
- {
- if (pSourceField.Type != esriFieldType.esriFieldTypeGeometry)
- {
- count++;
- obj[0] = pSourceField.AliasName;
- obj[1] = "字段類型錯誤";
- dataGridViewX2.Rows.Add(obj);
- continue;
- }
- }
- //2.檢查字段長度
- if (pSourceField.Length != int.Parse(odr["LENGTH"].ToString()))
- {
- count++;
- obj[0] = pSourceField.AliasName;
- obj[1] = "字段長度錯誤";
- dataGridViewX2.Rows.Add(obj);
- continue;
- }
- //3.檢查字段是否爲空
- if (odr["ISNULL"].ToString() == "是")
- {
- if (pSourceField.IsNullable != true)
- {
- count++;
- obj[0] = pSourceField.AliasName;
- obj[1] = "字段爲空錯誤";
- dataGridViewX2.Rows.Add(obj);
- }
- }
- else
- {
- if (pSourceField.IsNullable != false)
- {
- count++;
- obj[0] = pSourceField.AliasName;
- obj[1] = "字段爲空錯誤";
- dataGridViewX2.Rows.Add(obj);
- }
- }
- }
- else
- {
- errTxt.Text = "標準檢查未通過,沒有找到標準裏面的" + odr["NAME"].ToString() + "字段!";
- return;
- }
- }
- if (count > 0)
- {
- errTxt.Text = "標準檢查未通過,失敗字段數:" + count.ToString();
- }
- else
- {
- errTxt.Text = "標準檢查通過!";
- }
- }
6.總結:本篇只介紹了一個功能就是檢查對應的空間數據結構是否匹配,這是導入數據到數據庫中的必備步驟,不然可能造成數據庫的損壞,也浪費了一些不必要的操作。本篇的代碼中還有datagridView和combobox等控件的使用,例如怎樣在datagridview控件的固定單元增加值等。