Click here to Skip to main content
15,905,508 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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.
C#
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();
        }
    }
}
Posted
Updated 17-Jul-13 5:13am
v2
Comments
[no name] 17-Jul-13 10:26am    
Not it is not at all urgent. I do not see where you are using any kind of 2 minute timer here.

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:

C#
void timer_Elapsed(object sender, ElapsedEventArgs e)
{
    // your code here.
}


Valery.
 
Share this answer
 
Comments
shunuman 17-Jul-13 11:00am    
Thank you for reply....
But it gives a error....
timer_Elapsed
The type or namespace name 'ElapsedEventArgs'.
shunuman 17-Jul-13 11:01am    
I am using windows application..
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.
 
Share this answer
 
v3
Comments
shunuman 17-Jul-13 11:02am    
Yes, I am using windows application.
can you please describe how i can use this code in timer.
Arpit Shrivastava 17-Jul-13 11:19am    
You have to create a Event by clicking your timer property and set a Tick event. Now you can put your code or call your constructor in this method timer1_Tick(...)
Mike Meinz 17-Jul-13 11:17am    
Arpit, 2000 is 2 seconds not 2 minutes. I suggest setting the Timer interval to 60000 and then in the Tick event count the number of ticks and on the second tick, execute the SQL code and reset the tick count to zero.
Arpit Shrivastava 17-Jul-13 11:23am    
Yeah, you are right Mike, but the time should be 120000 = 2 minutes. and Is there any need to reset the timer???
Mike Meinz 17-Jul-13 13:07pm    
Yes, 120000 can be used.

I am "old school" so I used 60000 and counted the minutes. The original Timer control had a max Interval value of 65535. I keep forgetting that the shift to a 32-bit version changed that.

I would usually disable the Timer during the time that I was executing the SQL and then re-enable the Timer when the SQL was finished. That way, I didn't risk having the SQL still running when the Timer caused the next execution of the SQL....
I have used

while (true)
{
// code

}
in instead of timer and it's working well.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900