好在系统没有最终使用这个版本,这下可以放心的在网上发文了。其实就是简单的数据库连接啊,添加啊,删除啊什么的。
首先在数据库中写一个表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;