寫windows服務日志.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),如果部門是‘體檢科',設置打印類型為 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=【自己數(shù)據(jù)庫】;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()); // 這里可以添加日志記錄受影響的行數(shù) } } } catch (Exception ex) { // 這里應該添加適當?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、安裝成功
到此這篇關于寫windows服務日志.net4.5.2定時修改數(shù)據(jù)庫中某些參數(shù)的步驟的文章就介紹到這了,更多相關.net4.5.2定時修改數(shù)據(jù)庫參數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
asp.net 頁面版文本框智能提示JSCode (升級版)
模擬百度,Google智能提示,非與服務器端交互的,數(shù)據(jù)源來自已經(jīng)綁定好的下拉列表。純客戶端腳本 升級版2009-12-12ASP.NET MVC5使用MiniProfiler監(jiān)控MVC性能
這篇文章主要為大家詳細介紹了ASP.NET MVC5使用MiniProfiler監(jiān)控MVC性能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07IP地址與整數(shù)之間的轉換實現(xiàn)代碼(asp.net)
把這個整數(shù)轉換成一個32位二進制數(shù)。從左到右,每8位進行一下分割,得到4段8位的二進制數(shù),把這些二進制數(shù)轉換成整數(shù)然后加上”。”就是這個ip地址了2012-09-09Asp.net FileUpload上傳文件夾并檢測所有子文件的實現(xiàn)代碼
這篇文章主要介紹了Asp.net FileUpload上傳文件夾并檢測所有子文件的實現(xiàn)代碼,需要的朋友可以參考下2017-05-05