我在這裏是新來的,我需要一些幫助。希望你們能解決我的問題。在C#中讀取文本文件併發送數據到mysql查詢
我已成功創建一個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 = 「」 與當前的數據,我想。該代碼正在工作,並在服務器上發送數據。
謝謝你們!快樂的編碼。
Hi Mostafizur!我非常感謝你對此的快速反應,但代碼無效。謝謝。 – gcarlopadua
是不是它讀取文本文件或任何其他錯誤? – Mostafiz
對不起,我沒有提到,代碼沒有讀取文本文件裏面的文本。文本文件中的文本是0099.謝謝! – gcarlopadua