如何在定时器的Tick事件中的特定时间间隔后重复调用方法? [英] How Do I Call Method Repeatedly after specific Time Interval In Tick Event Of Timer?

查看:239
本文介绍了如何在定时器的Tick事件中的特定时间间隔后重复调用方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种从文件夹导入文件的方法,但我没有文件夹中的所有文件文件从ftp到该文件夹​​。我有一个字段作为主键的表,当我成功运行程序文件在程序执行时出现的数据库中导入但当新文件从服务器进入文件夹时,这些文件未导入数据库。并且在所有间隔错误出现违反主键约束但我无法删除主键,否则重复保存的文件一次又一次。

帮我解决这个问题。





这是我的代码

I have 1 method for importing files from folder but i don't have all files in that folder the files are coming from ftp to that folder.and i have table with 1 field as primary key , when i run program files successfully imported in database which are present at the time of execution of program but when new files are coming into folder from server those files not imported in database. and at all interval error comes "Violation of primary key constraint" but i can't remove primary key otherwise duplicate files saved again and again.
Help me to solve this problem.


Here is my code

namespace Module1 
{
 public partial class DeviceDetails : Form 
{
 //Timer initialises// System.Windows.Forms Timer t = new System.Windows.Forms Timer();

    public DeviceDetails()
    {
        InitializeComponent();
      //  ImportAllFilesOfFolder();
    }

    //Initialize data connection componants//
    SqlConnection con = new SqlConnection(----some conn string----);
    DataTable dt;
    SqlDataAdapter dataAdap;
    SqlCommandBuilder scmd;

    private void DeviceDetails_Load_1(object sender, EventArgs e)
    {
                  this.deviceDetailsTableAdapter.Fill(this.hospitalProjectDataSet.DeviceDetails);

        t.Interval = 100000;  // interval in millisecond
        t.Tick += new EventEventHandler(t_Tick);           
        t.Enabled = true;
        t.Start();
    }

     void t_Tick(object sender, EventArgs e)
    { 
        GetData("select from DeviceDetails.csv");
        ImportAllFilesOfFolder();         
    }



这是导入方法




Here is import method

public void ImportAllFilesOfFolder() { try { SqlConnection con = new SqlConnection(----con string----); string sourceDir = @"---folder path to import from---"; var IcsvFile = Directory.EnumerateFiles(sourceDir, "*.csv");

            foreach (string currentFile in IcsvFile)
            {                   
                    StreamReader sr = new StreamReader(currentFile);
                    string line = sr.ReadLine();
                    string[] value = line.Split(',');
                    DataTable dt = new DataTable();
                    DataRow row;

                    foreach (string dc in value)
                    {
                        dt.Columns.Add(new DataColumn(dc));
                    }

                    while (!sr.EndOfStream)
                    {
                        value = sr.ReadLine().Split(',');
                        if (value.Length == dt.Columns.Count)
                        {
                            row = dt.NewRow();
                            row.ItemArray = value;
                            dt.Rows.Add(row);
                        }
                    }

                    SqlBulkCopy bc = new SqlBulkCopy(con.ConnectionString, SqlBulkCopyOptions.TableLock);
                    bc.DestinationTableName = "DeviceDetails";
                    bc.BatchSize = dt.Rows.Count;
                    con.Open();
                    bc.WriteToServer(dt);
                    bc.Close();
                    con.Close();
                }
            }





[edit]已添加代码块 - OriginalGriff [/ edit]



[edit]Code block added - OriginalGriff[/edit]

推荐答案

请参阅我以前的回答:投票数据库带计时器 [ ^ ]。



-SA
Please see my past answer: Polling Database with Timer[^].

—SA


这篇关于如何在定时器的Tick事件中的特定时间间隔后重复调用方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆