日期:2014-05-18  浏览次数:20837 次

请教C#,做日志
请教C#,做日志
winform

会员名称,会员编号,电话,邮编,地址。 这些都是文本框。

 做好了新增会员了,现在,再做一个修改会员资料窗口,打开可以修改资料。
现在,需要判断,那个文本框被修改了,如果发现,就把原来的和修改后的,都写到数据库。
判断没有被修改的,就不用写到数据。

就是想做这样的日志。

总体的意思,就是判断那个会员资料被修改了,都把修改前和修改后的数据写到数据库的日志表。
没有改动过的文本框里的字段,就不需要写到数据库。

请教各位,写个代码。提个思路。

------解决方案--------------------
编个类存会员数据。

xxx.membership d --- 取当前会员信息 from database

xxx.membership a = new xxx.membership();

依次比较textbox.text 和 d中数据是否相同。不同就:

a.yyy = ....text;

相同就:

a.yyy = 保持为null或空。

d.yyy = 清空.(因为不改写)

确定.

然后的处理这个 a。 a 中包含有数据的写入数据库.

现在, a中的数据部分是新的。d 中的数据是 a 更新过的数据库记录后保有的旧数据。

然后 把:

a, d 所包含的数据分别发送到数据库,写入日志。一个是更新的,一个是原有数据。


在程序上面写,这算一个思路,另一个思路使用存储过程。


发送更新数据时,查询关键字段,如id, 把旧数据取出,然后更新,然后比较存储过程参数值,然后把有值的参数也存入日志标记为更新。把没值的参数与旧数据匹配的存入日志标记入旧数据。


推荐用存储过程。程序端只发送一次,处理数据由数据库来解决。比较好。
------解决方案--------------------
可以新的数据存在原来的表中,但是多一个有效版本字段,用来表示这是第几个版本的资料.


数据 ID 版本号
。。 1 1
。。 2 1
。。 3 1
。。 1 2
------解决方案--------------------
你试试textbox.TextChanged事件中加一个状态位,记录变化情况,是不是省事点
------解决方案--------------------
C# code

/// <summary>
        /// 建立系统事件日志
        /// </summary>
        /// <param name="LogSource">事件源</param>
        /// <param name="LogName">事件名称</param>
        /// <returns>是否成功</returns>
        public bool CreateEventLog(string LogSource, string LogName)
        {
            bool rel = false;
            if (!EventLog.SourceExists(LogName))
            {
                EventLog.CreateEventSource(LogSource, LogName);
                rel = true;
            }
            return rel;
        }
        /// <summary>
        /// 写入事件日志
        /// </summary>
        /// <param name="LogSource">事件源</param>
        /// <param name="LogName">事件名称</param>
        /// <param name="Log">事件内容</param>
        public void RecordSysLog(string LogSource, string LogName, string Log)
        {
            if (!EventLog.SourceExists(LogSource))
            {
                if (this.CreateEventLog(LogSource,LogName))
                {
                    EventLog elog1 = new EventLog(LogName);
                    elog1.WriteEntry(Log);
                    elog1.Close();
                }
            }
        }
        /// <summary>
        /// 写文件
        /// </summary>
        /// <param name="Path">文件路径</param>
        /// <param name="Strings">写入内容</param>
        public static void WriteFile(string Path, string Strings)
        {
            if (!File.Exists(Path))
            {
                FileStream fs = File.Create(Path);
                fs.Close();
                fs.Dispose();
            }
            StreamWriter sw = new StreamWriter(Path, true, Encoding.UTF8);
            sw.WriteLine(Strings);
            sw.Close();
            sw.Dispose();
        }
        /// <summary>
        /// 写入文件,包括时间
        /// </summary>
        /// <param name="Path">文件路径</param>
        /// <param name="Strings">文件内容</param>
        /// <param name="dt">写入时间</param>
        public static void WriteFile(string Path, string Strings, DateTime dt)
        {
            if (!File.Exists(Path))
            {
                FileStream fs = File.Create(Path);
                fs.Close();
                fs.Dispose();
            }
            StreamWriter sw2 = new StreamWriter(Path,true,Encoding.UTF8);
            sw2.WriteLine(Strings + "|"