我们如何在C#中添加日志消息,我们可以在SQL服务器中读取它们? [英] How we can add log messages in C# that we can read them in SQL server?
本文介绍了我们如何在C#中添加日志消息,我们可以在SQL服务器中读取它们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在C#中添加日志消息,因此我可以从我的SQL Server数据库中读取它们,因为登录失败,新用户创建等等但似乎没有任何效果。有人能帮助我吗?
我尝试过的事情:
我尝试了不同的教程,但他们都没有工作
I am trying to add log messages in C# so i can read them from my SQL Server DB, for ex Login Failed, New User Created etc. but nothing seems to work. Can someone help me?
What I have tried:
I have tried different tutorials but neither of them worked
推荐答案
试试这个:
Try this:
public void LogToDB(string text)
{
using (SqlConnection con = new SqlConnection(strLogDBConnect))
{
con.Open();
using (SqlCommand cmd = new SqlCommand("INSERT INTO LogTableName (LogTextColumnName) VALUES (@LOGSTR)", con))
{
cmd.Parameters.AddWithValue("@LOGSTR", strLogDBConnect);
cmd.ExecuteNonQuery();
}
}
}
首先需要在数据库中设置一个表,并相应地设置名称和连接字符串。如果它不起作用,请向我们展示代码,并解释它是如何失败的。
You will need to set up a table in the DB first, and set the names and connection string appropriately. If it doesn't work, show us the code, and explain how it failed.
这篇关于我们如何在C#中添加日志消息,我们可以在SQL服务器中读取它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文