在C#中读取文本文件并将数据发送到mysql查询

时间:2016-05-02 03:45:30

标签: c# mysql

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

我已经成功创建了一个Windows服务,它通过使用计时器从服务器发送数据(时间和日期戳)。唯一阻止我这样做的是文本文件。我想将文本文件中的数据用作我的" where命令"在mysql命令下。

请参阅以下代码:

Img=ones(128,128);
Img(20:end-20,20:end-20)=2;
Img(30:end-30,30:end-30)=3;
Img(45:end-45,45:end-45)=4;
Img(50:end-65,68:end-48)=2; %% Add one more rectangular
Img(68:end-48,50:end-65)=3; %% Add one more rectangular
subplot(121);imagesc(Img);colormap(gray);axis off;axis equal;
L = bwlabel(Img==2);
% imshow(L==1);colormap(gray);hold on; axis off;axis equal;
subplot(122);imagesc(Img);colormap(gray);hold on; axis off;axis equal;
hold on
for label_index=1:max(L(:))
    im = imfill(L==label_index, 'holes');
    [c2,h2] = contour(im,[0 1],'r','LineWidth',2);
end


L = bwlabel(Img==3);
for label_index=1:max(L(:))
    im = imfill(L==label_index, 'holes');
    [c3,h3] = contour(im,[0 1],'g','LineWidth',2);
end

L = bwlabel(Img==4);
for label_index=1:max(L(:))
    im = imfill(L==label_index, 'holes');
    [c3,h3] = contour(im,[0 1],'b','LineWidth',2);
end
hold off

如果我更换WHERE =""用我想要的当前数据。代码正在运行,它在服务器上发送数据。

谢谢你们!快乐的编码。

2 个答案:

答案 0 :(得分:0)

试试这个

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();

        }

    }
}

答案 1 :(得分:0)

问题:

  1. 您的源文件有多行吗?即。

    0099
    0100
    0101
    
  2. 或者它是2行,第二行是空的吗?如果是这样,你实际上会得到

    0099\n
    
  3. 解决方案:

    1. 多行

      foreach (string line in File.ReadAllLines(@"C:\brcode.txt"))
      {
          [your sql connection code]
          "... WHERE branch_code='" + line + "';"
      }
      
    2. 如果您只想要源文件中的一行,可能只需要修剪换行符:

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