应用界面
按下“button1”按钮,保存操作日志到数据库
日志类
using MySqlConnector;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Log2
{
public class MyLog
{
public static void test1(string oper1)
{
DateTime currentTime = DateTime.Now;
String dateStr = currentTime.ToString();
//插入数据库
MySqlConnection connection;
connection = new MySqlConnection("server=localhost;user id=root;password=***;port=3306;database=test;");
if (connection.State != ConnectionState.Open)
{
connection.Open();
}
string sta = "insert into logs(time_str,oper) values('"+currentTime+"','"+oper1+"')";//执行一个简单的语句
MySqlCommand comm = new MySqlCommand(sta, connection);
comm.ExecuteNonQuery();
connection.Close();
}
}
}
Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Log2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string operation1 = "点击了button1按钮";
MyLog.test1(operation1);
}
}
}
标签:string,currentTime,Windows,System,connection,窗体,using,日志,public
From: https://blog.csdn.net/zhourongxiang1/article/details/137395969