寫windows服務(wù)日志.net4.5.2定時修改數(shù)據(jù)庫中某些參數(shù)的步驟
環(huán)境:
windows 11
Visual Studio 2015
.net 4.5.2
SQL Server
目的:
定時修改數(shù)據(jù)庫中某些參數(shù)的值
- 定時修改24小時內(nèi),SQL數(shù)據(jù)庫中,表JD_Reports 內(nèi),如果部門是‘體檢科',設(shè)置打印類型為 1
- 可以打印。
步驟:
1、新建項目,創(chuàng)建windows 服務(wù)
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=【自己數(shù)據(jù)庫】;User Id=sa;Password=【自己的密碼】;"; // 創(chuàng)建一個靜態(tài)日志記錄器實例,用于在服務(wù)中記錄日志 private static readonly Logger Log = LogManager.GetCurrentClassLogger(); public Report_Print() { InitializeComponent(); // 設(shè)置服務(wù)每5分鐘檢查一次 _timer = new Timer(5 * 60 * 1000); // 5分鐘 _timer.Elapsed += TimerElapsed; } protected override void OnStart(string[] args) { // 服務(wù)啟動時記錄日志 Log.Debug("開始執(zhí)行"); _timer.Start(); // 啟動時立即執(zhí)行一次 UpdatePrintType(); } protected override void OnStop() { _timer.Stop(); // 服務(wù)啟動時記錄日志 Log.Debug("服務(wù)停止執(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()); // 這里可以添加日志記錄受影響的行數(shù) } } } catch (Exception ex) { // 這里應(yīng)該添加適當(dāng)?shù)腻e誤日志記錄 // 例如使用 EventLog 或其他日志框架 } } } }
5、在 Report_Print.cs 界面內(nèi),右鍵"添加安裝程序"
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、安裝成功
到此這篇關(guān)于寫windows服務(wù)日志.net4.5.2定時修改數(shù)據(jù)庫中某些參數(shù)的步驟的文章就介紹到這了,更多相關(guān).net4.5.2定時修改數(shù)據(jù)庫參數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
.NET?Core(.NET6)中g(shù)RPC使用實踐
gRPC是高性能的遠(yuǎn)程過程調(diào)用(RPC)框架,本文主要介紹了.NET?Core(.NET6)中g(shù)RPC使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04asp.net 頁面版文本框智能提示JSCode (升級版)
模擬百度,Google智能提示,非與服務(wù)器端交互的,數(shù)據(jù)源來自已經(jīng)綁定好的下拉列表。純客戶端腳本 升級版2009-12-12ASP.NET MVC5使用MiniProfiler監(jiān)控MVC性能
這篇文章主要為大家詳細(xì)介紹了ASP.NET MVC5使用MiniProfiler監(jiān)控MVC性能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07IP地址與整數(shù)之間的轉(zhuǎn)換實現(xiàn)代碼(asp.net)
把這個整數(shù)轉(zhuǎn)換成一個32位二進(jìn)制數(shù)。從左到右,每8位進(jìn)行一下分割,得到4段8位的二進(jìn)制數(shù),把這些二進(jìn)制數(shù)轉(zhuǎn)換成整數(shù)然后加上”?!本褪沁@個ip地址了2012-09-09ASP.NET對HTML頁面元素進(jìn)行權(quán)限控制(一)
界面每個元素的權(quán)限也是需要控制的。比如一個查詢用戶的界面里面有查詢用戶按鈕,添加用戶按鈕,刪除用戶按鈕,不同的角色我們得分配不同的權(quán)限2013-12-12Asp.net FileUpload上傳文件夾并檢測所有子文件的實現(xiàn)代碼
這篇文章主要介紹了Asp.net FileUpload上傳文件夾并檢測所有子文件的實現(xiàn)代碼,需要的朋友可以參考下2017-05-05