如何使用计时器每2分钟执行一次我的代码 [英] How I can use of timer for execute my code in every 2 min
问题描述
晚上好晚上.........
我用它来将一个服务器的表数据复制到sqlserver 2000中的另一个服务器。
我希望这个代码每2分钟执行一次,因为源数据每2分钟更新一次,我需要每2分钟复制一次新数据。我试了很多,但没有成功,请帮我急,谢谢大家。
<前lang =cs> 命名空间测试
{
public partial class Form1:Form
{
public Form1()
{
SqlConnection source = new SqlConnection( Server = Rajan; User ID = abc; Password = 123 ;数据库=测试跨度>);
SqlConnection destination = new SqlConnection( Server = santosh;用户ID =印度;密码= 12345;数据库=书);
SqlCommand cmd = new SqlCommand( 从书中删除,目的地);
source.Open();
destination.Open();
cmd.ExecuteNonQuery();
cmd = new SqlCommand( select *来自Test,source);
SqlDataReader reader = cmd.ExecuteReader();
SqlBulkCopy bulkdata = new SqlBulkCopy(destination);
bulkdata.DestinationTableName = Book;
bulkdata.WriteToServer(reader);
bulkdata.Close();
destination.Close();
source.Close();
InitializeComponent();
}
}
} 您好,
使用计时器。
计时器计时器=新计时器();
timer.Interval = 12000;
timer.Elapsed + = timer_Elapsed;
timer.Start();
并且在timer_Elapsed方法中,每2分钟需要执行一次代码:
void timer_Elapsed( object 发件人,ElapsedEventArgs e)
{
// 您的代码在这里。
}
Valery。
您正在使用Form,所以我在猜你的代码是窗口形式。
所以你可以实现 TIMER (转到 TOOLBOX >>点击 COMPONENTS >>将 TIMER 拖到表格中),你必须将你的构造函数方法调用到这个函数:
timer1_Tick(...)
现在右键单击 Form1.cs [Designer]中的计时器并选择属性。现在您必须将间隔字段设置为 120000 (请记住此间隔以毫秒为单位)并将已启用字段设置为 True 。
你已经完成了。
我用过了
while(true)
{
//代码
}
而不是计时器,它运行良好。
Good evening all.........
I am using this for copy a table data from one server to another server in sqlserver 2000.
I want this code execute in every 2 minute because source data updated in every 2 minutes and i need to copy new data in every 2 min. I try many but not sucess any, please help me its urgent, Thank you all.
namespace Test
{
public partial class Form1 : Form
{
public Form1()
{
SqlConnection source = new SqlConnection("Server = Rajan;User ID=abc;Password=123;Database=Test");
SqlConnection destination = new SqlConnection("Server = santosh;User ID=india;Password=12345;Database=Book");
SqlCommand cmd = new SqlCommand("delete from Book", destination);
source.Open();
destination.Open();
cmd.ExecuteNonQuery();
cmd = new SqlCommand("select * from Test", source);
SqlDataReader reader = cmd.ExecuteReader();
SqlBulkCopy bulkdata = new SqlBulkCopy(destination);
bulkdata.DestinationTableName = "Book";
bulkdata.WriteToServer(reader);
bulkdata.Close();
destination.Close();
source.Close();
InitializeComponent();
}
}
}
Hello,
use a Timer.
Timer timer = new Timer(); timer.Interval = 12000; timer.Elapsed += timer_Elapsed; timer.Start();
and in the timer_Elapsed method put what ever code needs to be executed every 2 minutes:
void timer_Elapsed(object sender, ElapsedEventArgs e) { // your code here. }
Valery.
You are using Form, so I am guessing your code is in Window Form.
So you can implement a TIMER (Go to TOOLBOX >> Click COMPONENTS >> Drag TIMER to the Form), You have to call your Constructor method to this function:
timer1_Tick(...)
Now right click your timer in Form1.cs [Designer] and select property. Now you have to set the Interval field to 120000 (Remember this interval is in milliseconds) and Set the Enabled field to True.
You are done.
I have used
while (true)
{
// code
}
in instead of timer and it's working well.
这篇关于如何使用计时器每2分钟执行一次我的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!