我是新来的,需要帮助。希望你们能解决我的问题。

我已经成功创建了Windows服务,该服务通过使用计时器从服务器发送数据(时间和日期戳)。唯一阻止我执行此操作的是文本文件。我想将文本文件上的数据用作mysql命令下的“ where命令”。

参见下面的代码:

using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Timers;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;

namespace bicmwinservice
{
    public partial class Service1 : ServiceBase
    {
        public Service1()
        {
            InitializeComponent();
        }

        public void OnDebug()
        {
            OnStart(null);
        }

        private Timer count;

        protected override void OnStart(string[] args)
        {

            count = new Timer(1 * 60 * 1000);  // 5 minutes expressed as milliseconds
            count.Elapsed += new ElapsedEventHandler(OnTimerElapsed);
            count.AutoReset = true;
            count.Start();

        }

        protected override void OnStop()
        {
            System.IO.File.Create(AppDomain.CurrentDomain.BaseDirectory + "OnStop.txt");
        }

        private void OnTimerElapsed(object sender, ElapsedEventArgs e)
        {


                DateTime time = DateTime.Now;
                DateTime date = DateTime.Now;


                string connString = "Server=mysql1003.mochahost.com;Database=snowphil_tester;Uid=snowphil_test;password=snowphil_test;";
                MySqlConnection conn = new MySqlConnection(connString);
                MySqlCommand command = conn.CreateCommand();


                StreamReader read = new StreamReader(@"C:\brcode.txt", Encoding.Default);

                command.CommandText = "Update branch_monitor SET `date`='" + date.ToString("yyyy'-'MM'-'dd") + "', `time`= '" + time.ToString("HH':'mm':'ss") + "' WHERE branch_code='" + read + "'";
                conn.Open();
                command.ExecuteNonQuery();

                conn.Close();

        }

    }
}


如果我将WHERE =“”替换为所需的当前数据。代码正在工作,并且它在服务器上发送数据。

谢谢你们!快乐的编码。

最佳答案

尝试这个

using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Timers;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;

namespace bicmwinservice
{
    public partial class Service1 : ServiceBase
    {
        public Service1()
        {
            InitializeComponent();
        }

        public void OnDebug()
        {
            OnStart(null);
        }

        private Timer count;

        protected override void OnStart(string[] args)
        {

            count = new Timer(1 * 60 * 1000);  // 5 minutes expressed as milliseconds
            count.Elapsed += new ElapsedEventHandler(OnTimerElapsed);
            count.AutoReset = true;
            count.Start();

        }

        protected override void OnStop()
        {
            System.IO.File.Create(AppDomain.CurrentDomain.BaseDirectory + "OnStop.txt");
        }

        private void OnTimerElapsed(object sender, ElapsedEventArgs e)
        {


                DateTime time = DateTime.Now;
                DateTime date = DateTime.Now;


                string connString = "Server=mysql1003.mochahost.com;Database=snowphil_tester;Uid=snowphil_test;password=snowphil_test;";
                MySqlConnection conn = new MySqlConnection(connString);
                MySqlCommand command = conn.CreateCommand();

                string read = File.ReadAllText(@"C:\brcode.txt");


                command.CommandText = "Update branch_monitor SET `date`='" + date.ToString("yyyy'-'MM'-'dd") + "', `time`= '" + time.ToString("HH':'mm':'ss") + "' WHERE branch_code='" + read + "'";
                conn.Open();
                command.ExecuteNonQuery();

                conn.Close();

        }

    }
}

10-02 01:05
查看更多