C# 如何维护access db中谁更新了字段以及何时在windows窗体中更新了字段的日志

C# 如何维护access db中谁更新了字段以及何时在windows窗体中更新了字段的日志,c#,asp.net,.net,C#,Asp.net,.net,我创建了一个windows窗体,其中添加了许多字段,如插入、更新等。它正在更新access DB中的字段,但我想实现另外一个功能,即在windows窗体中维护和显示日志,如谁在DB中更新了字段以及何时(时间) 我想让它像一个历史的功能,将保持记录的所有条目,并显示在相同的windows窗体时,我点击按钮,我怎么能做到这一点 下面是我的表单预览&您可以看到,有一个网格视图,用于显示字段和特定按钮,以执行不同的任务。我使用Access DB存储和查看数据 下面是我的代码- using System

我创建了一个windows窗体,其中添加了许多字段,如插入、更新等。它正在更新access DB中的字段,但我想实现另外一个功能,即在windows窗体中维护和显示日志,如谁在DB中更新了字段以及何时(时间)

我想让它像一个历史的功能,将保持记录的所有条目,并显示在相同的windows窗体时,我点击按钮,我怎么能做到这一点

下面是我的表单预览&您可以看到,有一个网格视图,用于显示字段和特定按钮,以执行不同的任务。我使用Access DB存储和查看数据

下面是我的代码-

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\saurabh.ad.sharma\Documents\test2\Database21.accdb");
        int count = 0;
        public Form1()
        {
            InitializeComponent();
        }    

        private void button1_Click(object sender, EventArgs e)
        {    
            con.Open();
            OleDbCommand cmd = con.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "insert into table1 values('"+textBox1.Text+"','"+textBox2.Text+"')";
            cmd.ExecuteNonQuery();
            con.Close();
            textBox1.Text = "";
            textBox2.Text = "";
            MessageBox.Show("record inserted successfully");    
        }

        private void button4_Click(object sender, EventArgs e)
        {
            con.Open();
            OleDbCommand cmd = con.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "select * from table1";
            cmd.ExecuteNonQuery();
            DataTable dt = new DataTable();
            OleDbDataAdapter da = new OleDbDataAdapter(cmd);
            da.Fill(dt);
            dataGridView1.DataSource = dt;
            con.Close();    
        }

        private void button2_Click(object sender, EventArgs e)
        {
            con.Open();
            OleDbCommand cmd = con.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "delete from table1 where name='"+textBox1.Text+"'";
            cmd.ExecuteNonQuery();
            con.Close();
            MessageBox.Show("record deleted successfully");
        }

        private void button3_Click(object sender, EventArgs e)
        {
            con.Open();
            OleDbCommand cmd = con.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "update table1 set name='"+textBox2.Text+"' where name='"+textBox1.Text+"'";
            cmd.ExecuteNonQuery();
            con.Close();
            MessageBox.Show("record updates successfully");
        }

        private void button5_Click(object sender, EventArgs e)
        {
            count = 0;
            con.Open();
            OleDbCommand cmd = con.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "select * from table1 where name='"+textBox1.Text+"' ";
            cmd.ExecuteNonQuery();
            DataTable dt = new DataTable();
            OleDbDataAdapter da = new OleDbDataAdapter(cmd);
            da.Fill(dt);
            count = Convert.ToInt32(dt.Rows.Count.ToString());
            dataGridView1.DataSource = dt;
            con.Close();    

            if (count == 0)
            {
                MessageBox.Show("record not found");
            }    
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Windows.Forms;
使用System.Data.OleDb;
命名空间Windows窗体应用程序1
{
公共部分类Form1:Form
{
OleDbConnection con=新OleDbConnection(@“Provider=Microsoft.ACE.OLEDB.12.0;数据源=C:\Users\saurabh.ad.sharma\Documents\test2\Database21.accdb”);
整数计数=0;
公共表格1()
{
初始化组件();
}    
私有无效按钮1\u单击(对象发送者,事件参数e)
{    
con.Open();
OleDbCommand cmd=con.CreateCommand();
cmd.CommandType=CommandType.Text;
cmd.CommandText=“插入到表1中的值(““+textBox1.Text+”,“+textBox2.Text+”)”;
cmd.ExecuteNonQuery();
con.Close();
textBox1.Text=“”;
textBox2.Text=“”;
MessageBox.Show(“记录插入成功”);
}
私有无效按钮4_单击(对象发送者,事件参数e)
{
con.Open();
OleDbCommand cmd=con.CreateCommand();
cmd.CommandType=CommandType.Text;
cmd.CommandText=“从表1中选择*”;
cmd.ExecuteNonQuery();
DataTable dt=新的DataTable();
OleDbDataAdapter da=新的OleDbDataAdapter(cmd);
da.填充(dt);
dataGridView1.DataSource=dt;
con.Close();
}
私有无效按钮2\u单击(对象发送者,事件参数e)
{
con.Open();
OleDbCommand cmd=con.CreateCommand();
cmd.CommandType=CommandType.Text;
cmd.CommandText=“从表1中删除,其中name=”+textBox1.Text+”;
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show(“记录删除成功”);
}
私有无效按钮3\u单击(对象发送者,事件参数e)
{
con.Open();
OleDbCommand cmd=con.CreateCommand();
cmd.CommandType=CommandType.Text;
cmd.CommandText=“更新表1集合名称=”+textBox2.Text+“,其中名称=”+textBox1.Text+”;
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show(“记录更新成功”);
}
私有无效按钮5_单击(对象发送者,事件参数e)
{
计数=0;
con.Open();
OleDbCommand cmd=con.CreateCommand();
cmd.CommandType=CommandType.Text;
cmd.CommandText=“从表1中选择*,其中name=”+textBox1.Text+”;
cmd.ExecuteNonQuery();
DataTable dt=新的DataTable();
OleDbDataAdapter da=新的OleDbDataAdapter(cmd);
da.填充(dt);
count=Convert.ToInt32(dt.Rows.count.ToString());
dataGridView1.DataSource=dt;
con.Close();
如果(计数=0)
{
MessageBox.Show(“未找到记录”);
}    
}
}
}
使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用System.Windows.Forms;
命名空间Windows窗体应用程序1
{
静态类程序
{
/// 
///应用程序的主要入口点。
/// 
[状态线程]
静态void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(新Form1());
}
}
}

您可以自己记录。每次写入数据库时,也要写入为此目的创建的另一个表。大概是这样的:

INSERT INTO logtable VALUES([timestamp], [current username], [changed field], [...])
然后,您可以用任何喜欢的方式显示日志,也可以使用gridview。如果日志的增长速度快于可用磁盘空间,您可能需要不时删除旧的日志条目

此外,您当前正在从未检查的用户提供的字符串创建SQL语句。这是危险的-您必须确保字符串不能包含SQL命令,否则您可能会发现您的数据库被操纵或破坏。经典例子:


您可能应该阅读SQL注入和/或输入消毒。如果您赶时间,请查看以下内容:

什么是“asp.net windows窗体”?使用sql中的触发器,您可以为每个操作添加审核日志,请参阅