【问题标题】:Log user activities in C# form application在 C# 表单应用程序中记录用户活动
【发布时间】:2013-10-28 19:02:49
【问题描述】:

在我的表单应用程序中,我有两个按钮。当用户点击按钮时,它会在 SQL 中更新。

所以我想在单独的 txt 文件中记录用户在文本框中输入的数字(记录)用户在表中输入的数字。

请检查以下代码以了解我的应用程序在做什么

private void button_StatusChange_Click(object sender, EventArgs e)
{
    SqlConnection sqlcon = new SqlConnection("Data source = Test; Initial Catalog = TableName; user id = sa;pwd = 12345678 ");
    if (MessageBox.Show("Do you really need to change", "Success", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.No)
        this.Close();

    SqlCommand updateCommand = sqlcon.CreateCommand();
    sqlcon.Open();
    updateCommand.CommandText = " update dbo.TableTBL set ColumName = '" + comboBox_statusChange.Text + "' where TId = '" + textBox_cNumber.Text + "'";

    string resultStatus = ((string)updateCommand.ExecuteScalar());
    updateCommand.ExecuteScalar();
    MessageBox.Show(" Status Changed Successfully");
    sqlcon.Close();

}

【问题讨论】:

  • 没有注意到你的问题
  • 我想启用我的用户活动登录,例如他在文本框中输入的数字
  • 你的意思是“textBox_cNumber”中的值吗?
  • 客户在文本框中输入的内容应记录在文本文件中
  • 你真的没有一个叫TableTBL的表吗?

标签: c# logging


【解决方案1】:
//very simple log to file
File.AppendAllText("filepath_here.txt", DateTime.Now.ToString(CultureInfo.InvariantCulture) + " " + textBox_cNumber.Text + "\r\n");

【讨论】:

  • 这行不通。 File.Create 将创建文件并返回一个 FileStream 你可以用来对文件做一些事情,但你没有关闭。所以你会在下一行得到一个 IOException。您只需要 File.AppendAllText 行 -> 它会为您创建文件,您无需调用 File.Create
  • 非常感谢,阿兰!没有正确测试它。固定。
猜你喜欢
  • 2011-09-01
  • 2014-02-28
  • 2013-09-23
  • 2010-12-21
  • 1970-01-01
  • 1970-01-01
  • 2012-06-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多