写彦 發表於 2025-4-30 11:25:01

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

<div id="navCategory"><h5 class="catalogue">目录</h5><ul class="first_class_ul"><li><a href="#_label0">步骤:</a></li><ul class="second_class_ul"><li><a href="#_lab2_0_0">1、新建项目,创建windows 服务</a></li><li><a href="#_lab2_0_1">2、下载日志程序包 NLog</a></li><li><a href="#_lab2_0_2">3、在App.config中配置日志包NLog</a></li><li><a href="#_lab2_0_3">4、Report_Print.cs</a></li><li><a href="#_lab2_0_4">5、在 Report_Print.cs 界面内,右键&quot;添加安装程序&quot;</a></li><li><a href="#_lab2_0_5">6、配置ServiceInstaller1</a></li><li><a href="#_lab2_0_6">7、配置serviceProcessInstaller1</a></li><li><a href="#_lab2_0_7">8、右键,编译程序</a></li><li><a href="#_lab2_0_8">9、安装程序</a></li><ul class="third_class_ul"><li><a href="#_label3_0_8_0">9.1卸载程序</a></li></ul><li><a href="#_lab2_0_9">10、安装成功</a></li><ul class="third_class_ul"></ul></ul></ul></div><p>环境:</p>
<blockquote><p>windows 11<br />Visual Studio 2015<br />.net 4.5.2<br />SQL Server</p></blockquote>
<p>目的:</p>
<blockquote><p>定时修改数据库中某些参数的值</p></blockquote>
<ul><li>定时修改24小时内,SQL数据库中,表JD_Reports 内,如果部门是&lsquo;体检科&#39;,设置打印类型为 1</li><li>可以打印。</li></ul>
<p class="maodian"><a name="_label0"></a></p><h2>步骤:</h2>
<p class="maodian"><a name="_lab2_0_0"></a></p><h3>1、新建项目,创建windows 服务</h3>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202504/2025043011195939.png" /></p>
<p class="maodian"><a name="_lab2_0_1"></a></p><h3>2、下载日志程序包 NLog</h3>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202504/2025043011195940.png" /></p>
<p class="maodian"><a name="_lab2_0_2"></a></p><h3>3、在App.config中配置日志包NLog</h3>
<div class="jb51code"><pre class="brush:xml;">&lt;?xml version="1.0" encoding="utf-8" ?&gt;
&lt;configuration&gt;
&lt;configSections&gt;
    &lt;section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/&gt;
&lt;/configSections&gt;
&lt;nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;
    &lt;targets&gt;
      &lt;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}"/&gt;
    &lt;/targets&gt;
    &lt;rules&gt;
      &lt;logger name="*" minlevel="Debug" writeTo="file"/&gt;
    &lt;/rules&gt;
&lt;/nlog&gt;
&lt;startup&gt;
    &lt;supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" /&gt;
&lt;/startup&gt;
&lt;/configuration&gt;</pre></div>
<p class="maodian"><a name="_lab2_0_3"></a></p><h3>4、Report_Print.cs</h3>
<div class="jb51code"><pre class="brush:csharp;">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 &gt;= 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 或其他日志框架
            }
      }
    }
}</pre></div>
<p class="maodian"><a name="_lab2_0_4"></a></p><h3>5、在 Report_Print.cs 界面内,右键&quot;添加安装程序&quot;</h3>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202504/2025043011195941.png" /></p>
<p class="maodian"><a name="_lab2_0_5"></a></p><h3>6、配置ServiceInstaller1</h3>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202504/2025043011195942.png" /></p>
<p class="maodian"><a name="_lab2_0_6"></a></p><h3>7、配置serviceProcessInstaller1</h3>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202504/2025043011195943.png" /></p>
<p class="maodian"><a name="_lab2_0_7"></a></p><h3>8、右键,编译程序</h3>
<p>完成</p>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202504/2025043011195944.png" /></p>
<p class="maodian"><a name="_lab2_0_8"></a></p><h3>9、安装程序</h3>
<div class="jb51code"><pre class="brush:plain;">sc create Report_Print binPath= "E:\vs2015\study\Report_print\Report_print\bin\Debug\Report_print.exe"
sc start Report_Print </pre></div>
<p class="maodian"><a name="_label3_0_8_0"></a></p><h4>9.1卸载程序</h4>
<div class="jb51code"><pre class="brush:plain;">sc stop Report_Print
sc delete Report_Print</pre></div>
<p class="maodian"><a name="_lab2_0_9"></a></p><h3>10、安装成功</h3>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202504/2025043011195945.png" /></p>
頁: [1]
查看完整版本: 写windows服务日志.net4.5.2定时修改数据库中某些参数的步骤