寫windows服務日志.net4.5.2定時修改數據庫中某些參數的步驟
更新時間:2025年04月30日 11:25:01 作者:九鼎科技-Leo
這篇文章主要介紹了寫windows服務日志.net4.5.2定時修改數據庫中某些參數的步驟,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧
環(huán)境:
windows 11
Visual Studio 2015
.net 4.5.2
SQL Server
目的:
定時修改數據庫中某些參數的值
- 定時修改24小時內,SQL數據庫中,表JD_Reports 內,如果部門是‘體檢科',設置打印類型為 1
- 可以打印。
步驟:
1、新建項目,創(chuàng)建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=【自己的密碼】;";
// 創(chuàng)建一個靜態(tài)日志記錄器實例,用于在服務中記錄日志
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("開始執(zhí)行");
_timer.Start();
// 啟動時立即執(zhí)行一次
UpdatePrintType();
}
protected override void OnStop()
{
_timer.Stop();
// 服務啟動時記錄日志
Log.Debug("服務停止執(zhí)行");
}
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、安裝成功

到此這篇關于寫windows服務日志.net4.5.2定時修改數據庫中某些參數的步驟的文章就介紹到這了,更多相關.net4.5.2定時修改數據庫參數內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
AspNetCore&MassTransit?Courier實現(xiàn)分布式事務的詳細過程
MassTransit?Courier是一種用于創(chuàng)建和執(zhí)行帶有故障補償的分布式事務的機制,它可以用于滿足本地事務的需求,也可以在分布式系統(tǒng)中實現(xiàn)分布式事務,這篇文章主要介紹了AspNetCore&MassTransit?Courier實現(xiàn)分布式事務,需要的朋友可以參考下2022-10-10
.net開發(fā)中批量刪除記錄時實現(xiàn)全選功能的具體方法
這篇文章介紹了.net開發(fā)中批量刪除記錄時實現(xiàn)全選功能的具體方法,有需要的朋友可以參考一下2013-11-11
SignalR中豐富多彩的消息推送方式的實現(xiàn)代碼
這篇文章主要介紹了SignalR中豐富多彩的消息推送方式的實現(xiàn)代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-04-04
探究ASP.NET Core Middleware實現(xiàn)方法
這篇文章主要介紹了探究ASP.NET Core Middleware實現(xiàn)方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-02-02
淺談Asp.net Mvc之Action如何傳多個參數的方法
本篇文章主要介紹了Asp.net Mvc之Action如何傳多個參數的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-08-08

