当前位置: 首页 > news >正文

写windows服务日志-.net4.5.2-定时修改数据库中某些参数

环境:

windows 11
Visual Studio 2015
.net 4.5.2
SQL Server

目的:

定时修改数据库中某些参数的值

定时修改24小时内,SQL数据库中,表JD_Reports 内,如果部门是‘体检科',设置打印类型为 1
可以打印。

步骤:

1、新建项目,创建windows 服务

在这里插入图片描述

2、下载日志程序包 NLog

在这里插入图片描述

3、在App.config中配置日志包NLog

<?xml version="1.0" encoding="utf-8" ?>
<configuration><configSections><section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/></configSections><nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><targets><target name="file" xsi:type="File" fileName="${basedir}/Logs/${date:format=yyyy-MM-dd}/${date:format=yyyy-MM-dd}.txt" layout="[${date:format=yyyy-MM-dd HH\:mm\:ss}][${level}] ${message} ${exception}"/></targets><rules><logger name="*" minlevel="Debug" writeTo="file"/></rules></nlog><startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" /></startup>
</configuration>

4、Report_Print.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Timers;
using NLog;  // 引入 NLog 命名空间,用于日志记录namespace Report_print
{public partial class Report_Print : ServiceBase{private Timer _timer;private readonly string _connectionString = "Data Source=.;Initial Catalog=【自己数据库】;User Id=sa;Password=【自己的密码】;";// 创建一个静态日志记录器实例,用于在服务中记录日志private static readonly Logger Log = LogManager.GetCurrentClassLogger();public Report_Print(){InitializeComponent();// 设置服务每5分钟检查一次_timer = new Timer(5 * 60 * 1000); // 5分钟_timer.Elapsed += TimerElapsed;}protected override void OnStart(string[] args){// 服务启动时记录日志Log.Debug("开始执行");_timer.Start();// 启动时立即执行一次UpdatePrintType();}protected override void OnStop(){_timer.Stop();// 服务启动时记录日志Log.Debug("服务停止执行");}private void TimerElapsed(object sender, ElapsedEventArgs e){UpdatePrintType();}private void UpdatePrintType(){try{using (SqlConnection conn = new SqlConnection(_connectionString)){conn.Open();string sql = @"UPDATE JD_Reports SET PrintType = 1 WHERE Depart = '体检科' AND CheckTime >= DATEADD(HOUR, -24, GETDATE())AND (PrintType IS NULL OR PrintType != 1)";using (SqlCommand cmd = new SqlCommand(sql, conn)){int rowsAffected = cmd.ExecuteNonQuery();Log.Debug("写入日志成功: " + rowsAffected.ToString());// 这里可以添加日志记录受影响的行数}}}catch (Exception ex){// 这里应该添加适当的错误日志记录// 例如使用 EventLog 或其他日志框架}}}
}

5、在 Report_Print.cs 界面内,右键"添加安装程序"

在这里插入图片描述

6、配置ServiceInstaller1

在这里插入图片描述

7、配置serviceProcessInstaller1

在这里插入图片描述

8、右键,编译程序

完成
在这里插入图片描述

9、安装程序

sc create Report_Print binPath= "E:\vs2015\study\Report_print\Report_print\bin\Debug\Report_print.exe"
sc start Report_Print 

9.1卸载程序

sc stop Report_Print
sc delete Report_Print

10、安装成功

在这里插入图片描述

相关文章:

  • 批量级负载均衡(Batch-Wise Load Balance)和顺序级负载均衡(Sequence-Wise Load Balance)
  • 【如何使用solidwork编辑结构导入到simscope】
  • FastAPI中的依赖注入详解与示例
  • MLLM之Bench:LEGO-Puzzles的简介、安装和使用方法、案例应用之详细攻略
  • 语音合成之八-情感化语音合成的演进路线
  • HTTP header Cookie 和 Set-Cookie
  • DIFY教程第一集:安装Dify配置环境
  • 泰迪杯实战案例超深度解析:旅游景点游客流量预测与资源优化
  • 英文中日期读法
  • 记录学习记录学习《手动学习深度学习》这本书的笔记(九)
  • Python中的Walrus运算符分析
  • 第35课 常用快捷操作——用“鼠标左键”拖动图元
  • 产品经理面经(1)
  • 在winform中使用chromiumWebBrowser显示Echarts图表
  • 前端高频面试题day2
  • 大模型工程师基础之学会使用openai
  • 51单片机所有寄存器介绍
  • leetcode0103. 二叉树的锯齿形层序遍历-medium
  • JAVA手写题-精通 Java 单例模式:三种线程安全的实现方式详解
  • JAVA:单例模式
  • 美乌总统梵蒂冈会谈,外交部:望有关各方继续通过对话谈判解决危机
  • 央行副行长谈美债和美元波动:单一市场、单一资产的变动,对外储影响总体有限
  • 李在明当选韩国共同民主党总统候选人
  • 因高颜值走红的女通缉犯出狱后当主播自称“改邪归正”,账号已被封
  • 可实时追踪血液中单个细胞的穿戴医疗设备问世
  • 陈平评《艺术科学的目的与界限》|现代艺术史学的奠基时代