基於ArcGIS10.0和Oracle10g的空間資料管理平台十八(C#開發)-資料字典編輯

來源:互聯網
上載者:User

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

歡迎大家相互交流,共同提高技術。

    今天繼續接著前面介紹的開發通用空間資料管理平台這個項目進行講解,今天介紹的內容比較簡單,就是通過介面來實現資料字典的編輯。至於資料字典的概念大家可以網上尋找,以前我有一篇部落格專門針對這個項目中用到的專業術語進行過講解,也可以去查看一下。

    介紹的思路還是按照當時自己在實現這個功能的思路,基本上還原當時怎樣一步一步把這個相對簡單的功能實現的。

1.定義用到的成員變數。

  1. private OracleCommandBuilder builder;//命令構建  
  2. private OracleDataAdapter da;//資料配接器  
  3. private DataSet ds;//資料集  
  4. private string selectedNodeText;//儲存選擇樹形節點的常值內容  
  5. private string tableName;//表的名稱  
  6. protected OracleConnection Connection;//串連資料庫  
  7. private bool isChanged = false;//控制項中的內容是否改變  

2.在form的裝載函數中初始化介面的相關控制項,讓這些控制項都處於一個正常的顯示狀態並且為後面的操作做好準備。

 

  1. private void DataDictionaryEdit_Load(object sender, EventArgs e)  
  2. {  
  3.     //載入相應的資料字典表到Tree  
  4.     Connection = new OracleConnection(ConfigurationSettings.AppSettings["ConnectionString"]);  
  5.   
  6.     Node tn = new Node();  
  7.     tn.Text = "資料字典表";  
  8.   
  9.     advTree1.Nodes.Add(tn);  
  10.     Node n1 = new Node();  
  11.     n1.Text = "資料分類表";  
  12.     Node n2 = new Node();  
  13.     n2.Text = "要素類別表";  
  14.     Node n3 = new Node();  
  15.     n3.Text = "圖層表";  
  16.     Node n4 = new Node();  
  17.     n4.Text = "欄位定義表";  
  18.     tn.Nodes.Add(n1);  
  19.     tn.Nodes.Add(n2);  
  20.     tn.Nodes.Add(n3);  
  21.     tn.Nodes.Add(n4);  
  22. }  

3.通過樹形控制項選擇相應的節點以後,通過樹形節點的文字(代表表名稱)選擇一個表的內容載入到datagridView控制項中,並且實現分頁顯示功能。

  1. //選擇相應的表以後載入資料到DataGridView中  
  2. private void advTree1_AfterNodeSelect(object sender, AdvTreeNodeEventArgs e)  
  3. {  
  4.     Node tn = new Node();  
  5.   
  6.     tn = e.Node;  
  7.     selectedNodeText = tn.Text;  
  8.   
  9.     switch (tn.Level)  
  10.     {  
  11.         case 1:  
  12.             labelX1.Text = "";  
  13.             labelX1.Text = "當前資料表: " + tn.Text;  
  14.   
  15.             if (Connection.State != ConnectionState.Open)  
  16.             {  
  17.                 Connection.Open();  
  18.             }  
  19.             if (tn.Text == "資料分類表")  
  20.             {  
  21.                 tableName = "jcsjk_category";  
  22.             }  
  23.             else if (tn.Text == "要素類別表")  
  24.             {  
  25.                 tableName = "jcsjk_element";  
  26.             }  
  27.             else if (tn.Text == "圖層表")  
  28.             {  
  29.                 tableName = "jcsjk_layer";  
  30.             }  
  31.             else if (tn.Text == "欄位定義表")  
  32.             {  
  33.                 tableName = "jcsjk_fielddefine";  
  34.             }  
  35.   
  36.             string sql = "select * from " + tableName;  
  37.             da = new OracleDataAdapter(sql, Connection);  
  38.             builder = new OracleCommandBuilder(da);  
  39.             ds = new DataSet();  
  40.   
  41.             da.Fill(ds, tableName);  
  42.             dataGridViewX1.DataSource = ds.Tables[0];  
  43.   
  44.             int intMod, dgr;  
  45.             //先讓垂直捲軸消失   
  46.             dataGridViewX1.ScrollBars = ScrollBars.Horizontal;  
  47.             //取出DGV的行數,為什麼要減一是因為它總是多出一行給你編輯的所以那行也佔用一行的空間   
  48.             dgr = dataGridViewX1.RowCount - 1;  
  49.             //進行模數   
  50.             if (dgr % 10 == 0)  
  51.             {  
  52.                 intMod = 0;  
  53.             }  
  54.             else  
  55.             {  
  56.                 intMod = 1;  
  57.             }  
  58.             //主要時這個for迴圈將表一共分為幾頁添加到comboBox  
  59.             comboBoxEx1.Items.Clear();  
  60.             for (int i = 1; i <= dgr / 10 + intMod; i++)  
  61.             {  
  62.                 comboBoxEx1.Items.Add("第" + i + "頁");  
  63.             }  
  64.             //預設選中第一個  
  65.             if (comboBoxEx1.Items.Count > 0)  
  66.             {  
  67.                 comboBoxEx1.SelectedIndex = 0;  
  68.             }  
  69.             break;  
  70.         default:  
  71.             break;  
  72.     }  
  73. }  

4.在控制項增加和刪除一條記錄,此時還沒有更新到資料庫,需要保持以後才真正進入資料庫中,後面一個功能實現。

  1. //刪除一條記錄  
  2. private void delBtn_Click(object sender, EventArgs e)  
  3. {  
  4.     if (!dataGridViewX1.AllowUserToDeleteRows)  
  5.     {  
  6.         dataGridViewX1.AllowUserToDeleteRows = true;  
  7.     }  
  8.     if (dataGridViewX1.CurrentRow.Index < 0)  
  9.     {  
  10.         MessageBox.Show("請選擇需要刪除的一行");  
  11.         return;  
  12.     }  
  13.     isChanged = true;  
  14.     ds.Tables[0].Rows[dataGridViewX1.CurrentRow.Index].Delete();  
  15. }  
  16.   
  17. //增加一條記錄  
  18. private void addBtn_Click(object sender, EventArgs e)  
  19. {  
  20.     if (!dataGridViewX1.AllowUserToAddRows)  
  21.     {  
  22.         dataGridViewX1.AllowUserToAddRows = true;  
  23.     }  
  24.   
  25.     if (dataGridViewX1.ReadOnly)  
  26.     {  
  27.         dataGridViewX1.ReadOnly = false;  
  28.     }  
  29.     isChanged = true;  
  30.     dataGridViewX1.FirstDisplayedScrollingRowIndex =  
  31.         dataGridViewX1.Rows[dataGridViewX1.RowCount - 1].Index;  
  32. }  

5.把控制項中改動、增加或刪除的內容更新到資料庫中去,可以同時對控制項中的內容進行增加、刪除和修改,然後一次性更新到資料庫,但是每次只能操作一個資料字典表。

  1. //修改後的資料儲存到資料庫中去  
  2. private void saveBtn_Click(object sender, EventArgs e)  
  3. {  
  4.     int result = 0;  
  5.     if (isChanged)  
  6.     {  
  7.         try  
  8.         {  
  9.             result = da.Update(ds, tableName);  
  10.         }  
  11.         catch (Exception ex)  
  12.         {  
  13.             MessageBox.Show(ex.Message);  
  14.         }  
  15.     }  
  16.     isChanged = false;  
  17.     dataGridViewX1.ReadOnly = true;  
  18.     dataGridViewX1.AllowUserToAddRows = false;  
  19.     dataGridViewX1.AllowUserToDeleteRows = false;  
  20.   
  21.     if (result > 0)  
  22.     {  
  23.         LogHelp.writeLog(FrmMain.username, "資料字典編輯",  
  24.             "資料字典" + selectedNodeText + "編輯成功");  
  25.         MessageBox.Show("更新資料庫成功");  
  26.     }  
  27.     else  
  28.     {  
  29.         LogHelp.writeLog(FrmMain.username, "資料字典編輯",  
  30.             "資料字典" + selectedNodeText + "編輯失敗");  
  31.         MessageBox.Show("資料庫沒有更新");  
  32.     }  
  33. }  

6.其他輔助小功能。

  1. //使能編輯  
  2.  private void editBtn_Click(object sender, EventArgs e)  
  3.  {  
  4.      if (dataGridViewX1.ReadOnly)  
  5.      {  
  6.          dataGridViewX1.ReadOnly = false;  
  7.      }  
  8.  }  
  9.   
  10.  //檢查資料錄入格式  
  11.  private void dataGridViewX1_DataError(object sender, DataGridViewDataErrorEventArgs e)  
  12.  {  
  13.      labelX2.Text = "資料錄入格式不正確";  
  14.  }  
  15.   
  16.  //記錄值是否發生改變  
  17.  private void dataGridViewX1_CellValueChanged(object sender, DataGridViewCellEventArgs e)  
  18.  {  
  19.      isChanged = true;  
  20.  }  
  21.   
  22.  //DataGridView全部可見  
  23.  private void showAllBtn_Click(object sender, EventArgs e)  
  24.  {  
  25.      dataGridViewX1.ScrollBars = ScrollBars.Both;  
  26.      try  
  27.      {  
  28.          dataGridViewX1.FirstDisplayedScrollingRowIndex = 0;  
  29.      }  
  30.      catch (ArgumentOutOfRangeException)  
  31.      {  
  32.   
  33.      }  
  34.  }  
  35.   
  36.  //顯示第幾頁資料  
  37.  private void comboBoxEx1_SelectedIndexChanged(object sender, EventArgs e)  
  38.  {  
  39.      try  
  40.      {  
  41.          dataGridViewX1.FirstDisplayedScrollingRowIndex = comboBoxEx1.SelectedIndex * 10;  
  42.      }  
  43.      catch (ArgumentOutOfRangeException)  
  44.      {  
  45.   
  46.      }  
  47.  }  

    其他功能主要包括:定位顯示第幾頁的資料、使能控制項可以編輯、輸入控制項值的有效性檢查等。

 

7、總結。

    這裡所謂的資料字典編輯其實也就是針對幾個表進行操作,和普通的資料庫表操作完全一樣,只是根據功能記錄的資料類型不一樣而已,資料字典可能聽起來更加專業一些,它的作用更加突出明顯一些。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.