日志管理完成版:.NET练习篇

王朝c#·作者佚名  2006-11-24
窄屏简体版  字體: |||超大  

好在系统没有最终使用这个版本,这下可以放心的在网上发文了。其实就是简单的数据库连接啊,添加啊,删除啊什么的。

首先在数据库中写一个表Log (logdatetime,loguser,logtype,logmodule,logdescribe)后来发现这个表名跟关键字重了,不过也懒得改了。单独写了一个类用来添加数据到数据库中。代码如下:

using System;

using System.Data;

using System.Data.SqlClient;

namespace LogModule

{

/// <summary>

/// LogModule 的摘要说明。

/// </summary>

public class LogModule

{

public LogModule()

{

//

// TODO: 在此处添加构造函数逻辑

//

}

/// <summary>

/// 日志添加

/// </summary>

public static void log(string lu,string lt,string lm, string ld)

{

if (lu!=null && lt!=null && ld!=null)

{

try

{

SqlConnection logConn = new SqlConnection("Data Source=localhost;Integrated Security=SSPI;Initial Catalog=xxxx");

logConn.Open();

string logInsert ="insert into Log(logdatetime,loguser,logtype,logmodule,logdescribe) values(getdate(), '" +

lu+"', '"+lt+"','"+lm+"','"+ld+"')";

SqlCommand logCmd = new SqlCommand(logInsert,logConn);

logCmd.ExecuteNonQuery();

//释放资源

logCmd.Dispose();

logConn.Close();

}

catch (System.Exception e)

{

Console.WriteLine(e.Message);

}

}

}

}

}

这样其他类就能够引用这个方法了,而且不用new一个对象,用起来还算方便。而对于日志的管理,修改是没有必要的,删除和查询则是在于用户交互的过程中完成的,所以做了个form来实现,如上例,我还是粘出全文:

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

using System.Data.SqlClient;

namespace LogModule

{

/// <summary>

/// Form1 的摘要说明。

/// </summary>

public class Form1 : System.Windows.Forms.Form

{

private System.Windows.Forms.Button button1;

private System.Windows.Forms.Button button2;

private System.Windows.Forms.Button button3;

private System.Windows.Forms.Button button4;

private System.Windows.Forms.Button button5;

private System.Windows.Forms.ListView listView1;

private System.Windows.Forms.ColumnHeader columnHeader1;

private System.Windows.Forms.ColumnHeader columnHeader2;

private System.Windows.Forms.ColumnHeader columnHeader3;

private System.Windows.Forms.ColumnHeader columnHeader4;

private System.Windows.Forms.ColumnHeader columnHeader5;

private System.Windows.Forms.ColumnHeader columnHeader6;

private System.Windows.Forms.ComboBox comboBox1;

private System.Windows.Forms.ComboBox comboBox2;

private System.Windows.Forms.ComboBox comboBox3;

private System.Windows.Forms.ComboBox comboBox4;

private System.Windows.Forms.ComboBox comboBox5;

private System.Windows.Forms.Label label1;

private System.Windows.Forms.Label label2;

private System.Windows.Forms.Label label3;

private System.Windows.Forms.Label label4;

private System.Windows.Forms.Label label5;

private System.Windows.Forms.TextBox textBox1;

static SqlConnection conn = new SqlConnection ("Data Source=localhost;Integrated Security=SSPI;Initial Catalog=xxxx");

/// <summary>

/// 必需的设计器变量。

/// </summary>

private System.ComponentModel.Container components = null;

public Form1()

{

//

// Windows 窗体设计器支持所必需的

//

InitializeComponent();

//

// TODO: 在 InitializeComponent 调用后添加任何构造函数代码

//

}

/// <summary>

/// 清理所有正在使用的资源。

/// </summary>

protected override void Dispose( bool disposing )

{

if( disposing )

{

if (components != null)

{

components.Dispose();

}

}

base.Dispose( disposing );

}

#region Windows 窗体设计器生成的代码

/// <summary>

/// 设计器支持所需的方法 - 不要使用代码编辑器修改

/// 此方法的内容。

/// </summary>

private void InitializeComponent()

{

this.button1 = new System.Windows.Forms.Button();

this.button2 = new System.Windows.Forms.Button();

this.button3 = new System.Windows.Forms.Button();

this.button4 = new System.Windows.Forms.Button();

this.button5 = new System.Windows.Forms.Button();

this.listView1 = new System.Windows.Forms.ListView();

this.columnHeader5 = new System.Windows.Forms.ColumnHeader();

this.columnHeader1 = new System.Windows.Forms.ColumnHeader();

this.columnHeader2 = new System.Windows.Forms.ColumnHeader();

this.columnHeader3 = new System.Windows.Forms.ColumnHeader();

this.columnHeader6 = new System.Windows.Forms.ColumnHeader();

this.columnHeader4 = new System.Windows.Forms.ColumnHeader();

this.comboBox1 = new System.Windows.Forms.ComboBox();

this.comboBox2 = new System.Windows.Forms.ComboBox();

this.comboBox3 = new System.Windows.Forms.ComboBox();

this.comboBox4 = new System.Windows.Forms.ComboBox();

this.label1 = new System.Windows.Forms.Label();

this.label2 = new System.Windows.Forms.Label();

this.label3 = new System.Windows.Forms.Label();

this.label4 = new System.Windows.Forms.Label();

this.label5 = new System.Windows.Forms.Label();

this.comboBox5 = new System.Windows.Forms.ComboBox();

this.textBox1 = new System.Windows.Forms.TextBox();

this.SuspendLayout();

//

// button1

//

this.button1.Location = new System.Drawing.Point(552, 16);

this.button1.Name = "button1";

this.button1.Size = new System.Drawing.Size(104, 48);

this.button1.TabIndex = 20;

this.button1.Text = "查找日志";

this.button1.Click += new System.EventHandler(this.button1_Click);

//

// button2

//

this.button2.Location = new System.Drawing.Point(576, 336);

this.button2.Name = "button2";

this.button2.Size = new System.Drawing.Size(80, 40);

this.button2.TabIndex = 22;

this.button2.Text = "添加日志";

this.button2.Click += new System.EventHandler(this.button2_Click);

//

// button3

//

this.button3.Location = new System.Drawing.Point(552, 72);

this.button3.Name = "button3";

this.button3.Size = new System.Drawing.Size(104, 48);

this.button3.TabIndex = 21;

this.button3.Text = "整理日志";

this.button3.Click += new System.EventHandler(this.button

[1] [2] [3] [4] 下一页

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航