基于ArcGIS10.0和Oracle10g的空間數(shù)據(jù)管理平臺十二(C#開發(fā))-日志管理
我的獨(dú)立博客網(wǎng)址是:http://wuyouqiang.sinaapp.com/。
我的新浪微博:http://weibo.com/freshairbrucewoo。
歡迎大家相互交流,共同提高技術(shù)。
本篇文章主要介紹本系統(tǒng)中的日志管理功能,由前面介紹的內(nèi)容可以知道日志分為三類:分別是用戶操作日志、數(shù)據(jù)表更新日志以及數(shù)據(jù)庫監(jiān)控日志,這些日志的寫入可能遍布整個(gè)系統(tǒng)中的各個(gè)地方。這里提供一個(gè)統(tǒng)一的日志管理界面,可以對日志進(jìn)行查詢和刪除,并沒有提供修改的功能,因?yàn)槿罩臼窍到y(tǒng)自動生成的,修改日志沒有任何意義,而且還可能造成系統(tǒng)的漏洞,比如誰刪除了數(shù)據(jù)然后把刪除的日志記錄修改了,就不能正確發(fā)現(xiàn)誰刪除了數(shù)據(jù)!
1.定義操作數(shù)據(jù)集的相關(guān)成員變量,對于所有日志都采用數(shù)據(jù)集來維護(hù),這就要求每一個(gè)表都必須要有一個(gè)主鍵,這樣做的目的簡化操作,采用一致性的模型處理所有日志數(shù)據(jù)。
1 private OracleCommandBuilder builder;//數(shù)據(jù)適配器的命令
2 private OracleDataAdapter da;//數(shù)據(jù)適配器
3 private DataSet ds;
4 protected OracleConnection Connection;
2.初始化數(shù)據(jù)庫連接字符串
1 public LogManager()
2 {
3 InitializeComponent();
4 Connection = new OracleConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
5 }
3.初始化數(shù)據(jù)集適配器,并默認(rèn)并對操作日志表,修改datagridView的列頭部顯示
1 /// <summary>
2 /// 初始化數(shù)據(jù)集適配器,并默認(rèn)并對操作日志表,修改datagridView的列頭部顯示
3 /// </summary>
4 /// <param name="sender"></param>
5 /// <param name="e"></param>
6 private void LogManager_Load(object sender, EventArgs e)
7 {
8 SqlHelper sh = new SqlHelper();
9 string sql = "select opttime,type,username,description from optlog";
10 if (Connection.State != ConnectionState.Open)
11 {
12 Connection.Open();
13 }
14 //構(gòu)建數(shù)據(jù)適配器為了修改數(shù)據(jù),綁定的數(shù)據(jù)表必須有主鍵才能修改
15 da = new OracleDataAdapter(sql, Connection);
16 builder = new OracleCommandBuilder(da);
17 ds = new DataSet();
18 da.Fill(ds, "jcsjk_optlog");
19 dataGridViewX1.DataSource = ds.Tables[0];
20
21 dataGridViewX1.Columns[0].HeaderText = "時(shí)間";
22 dataGridViewX1.Columns[1].HeaderText = "類型";
23 dataGridViewX1.Columns[2].HeaderText = "用戶名";
24 dataGridViewX1.Columns[3].HeaderText = "日志內(nèi)容";
25 dataGridViewX1.Columns[3].Width = 300;
26
27 comboBoxEx1.SelectedIndex = 0;
28 }
4.導(dǎo)出數(shù)據(jù)到word或excel中,具體的導(dǎo)出過程在前面的工具類中已經(jīng)介紹過了。
1 /// <summary>
2 /// 導(dǎo)出數(shù)據(jù)到word或excel
3 /// </summary>
4 /// <param name="sender"></param>
5 /// <param name="e"></param>
6 private void expLogBtn_Click(object sender, EventArgs e)
7 {
8 FrmSelectExpType fset = new FrmSelectExpType();
9 fset.ShowDialog();
10
11 if (fset.isGon)
12 {
13 if (fset.type == 0)
14 {
15 CommonTools.ExportDataGridViewToWord(dataGridViewX1);
16 }
17 else
18 {
19 CommonTools.DataToExcel(dataGridViewX1);
20 MessageBox.Show("導(dǎo)出數(shù)據(jù)完成!");
21 }
22 }
23 }
5.刪除一條日志記錄的實(shí)現(xiàn)
1 /// <summary>
2 /// 刪除一條日志記錄
3 /// </summary>
4 /// <param name="sender"></param>
5 /// <param name="e"></param>
6 private void delOneBtn_Click(object sender, EventArgs e)
7 {
8 if (dataGridViewX1.CurrentRow.Index < 0)
9 {
10 MessageBox.Show("請選擇需要?jiǎng)h除的一行");
11 return;
12 }
13
14 string strTime = dataGridViewX1.Rows[dataGridViewX1.CurrentRow.Index].Cells[0].Value.ToString();
15 dataGridViewX1.Rows.Remove(dataGridViewX1.CurrentRow);
16
17 if (da.Update(ds, "optlog") > 0)
18 {
19 errLabel.Text = "刪除一條日志記錄成功!";
20 }
21 else
22 {
23 errLabel.Text = "刪除一條日志記錄失敗!";
24 }
25 }
6.根據(jù)選擇字段類型來查詢?nèi)罩荆梢愿鶕?jù)操作時(shí)間、日志類型、用戶名或日志的具體內(nèi)容。
1 /// <summary>
2 /// 查詢相關(guān)日志
3 /// </summary>
4 /// <param name="sender"></param>
5 /// <param name="e"></param>
6 private void queryBtn_Click(object sender, EventArgs e)
7 {
8 //dataGridViewX1.Rows.Clear();
9 string strType = string.Empty;
10
11 switch (comboBoxEx1.SelectedIndex)
12 {
13 case 0:
14 strType = "OPTTIME";
15 break;
16 case 1:
17 strType = "TYPE";
18 break;
19 case 2:
20 strType = "USERNAME";
21 break;
22 case 3:
23 strType = "description";
24 break;
25 default:
26 break;
27 }
28 string sql = string.Empty;
29
30 SqlHelper sh = new SqlHelper();
31 sql = "select opttime,type,username,description from optlog where " +
32 strType + " like '%" + queryTxt.Text.Trim() + "%'";
33 if (Connection.State != ConnectionState.Open)
34 {
35 Connection.Open();
36 }
37 //構(gòu)建數(shù)據(jù)適配器為了修改數(shù)據(jù),綁定的數(shù)據(jù)表必須有主鍵才能修改
38 da = new OracleDataAdapter(sql, Connection);
39 builder = new OracleCommandBuilder(da);
40 ds = new DataSet();
41 da.Fill(ds, "jcsjk_optlog");
42 dataGridViewX1.DataSource = ds.Tables[0];
43
44 dataGridViewX1.Columns[0].HeaderText = "時(shí)間";
45 dataGridViewX1.Columns[1].HeaderText = "類型";
46 dataGridViewX1.Columns[2].HeaderText = "用戶名";
47 dataGridViewX1.Columns[3].HeaderText = "日志內(nèi)容";
48 dataGridViewX1.Columns[3].Width = 300;
49 }
7.刪除所有日志。
1 private void delAllBtn_Click(object sender, EventArgs e)
2 {
3 //dataGridViewX1.Rows.Clear();
4 int count = dataGridViewX1.Rows.Count;
5 for (int i = count-1; i >= 0; --i )
6 {
7 dataGridViewX1.Rows.RemoveAt(i);
8 if (da.Update(ds, "optlog") > 0)
9 {
10 continue;
11 }
12 else
13 {
14 errLabel.Text = "刪除日志記錄失敗!";
15 }
16 }
17 errLabel.Text = "刪除日志記錄成功!";
18 }
8.總結(jié)
本篇文章介紹的日志管理功能相對比較簡單,沒有什么技術(shù)難度,不過我覺得還是有幾點(diǎn)可以值得思考和學(xué)習(xí)。第一采用統(tǒng)一的數(shù)據(jù)管理模型,而不是一張數(shù)據(jù)表就需要一個(gè)單獨(dú)界面來管理;第二日志查詢采用了一種稱為全文查詢,只要涉及的字段都納入可查詢范圍,而且都是通過同一條sql語句實(shí)現(xiàn),通過用戶選擇的來配置,當(dāng)然可以增強(qiáng)一些功能就是允許用戶選擇多個(gè)字段;第三就是刪除日志也采用了靈活的方式,可以選擇一條刪除,如果要?jiǎng)h除多條(有規(guī)律的日志:例如某一天的),可以通過查詢?nèi)缓笫褂脛h除所有日志功能(這里是指查詢出來在控件中顯示的日志)。
OK!今天介紹完畢!
浙公網(wǎng)安備 33010602011771號