none
Automatic backup of a database using C#.net RRS feed

  • Question

  • Sir/Madam

    Currently I am doing a project based on Automatic Backup.i have placed five options,

    1. Hourly

    2. Daily

    3. Weekly

    4. Monthly

    5. Yearly

    I keep a xml file for storing the details of type of backup. the file looks likes

    <?xml version="1.0" encoding="UTF-8"?>
    
    <Backups>
    
    <TypeofBackups>Hourly</TypeofBackups>
    
    <Hour>02:00:00</Hour>
    
    <Time>01-01-0001 00:00:00</Time>
    
    <Day>0</Day>
    
    <Week/>
    
    <Month/>
    
    </Backups>

    if it is hourly there should be another file for storing last backup time. and i want to add a timer to the project and it should be work in some intervals and check the details with the xml file. if the condition satisfies, backup should work.

    In my project, everything is working except the timer. my project is run only once for selecting the options. and to run the program automatically according to the time interval. Kindly provide me the guidelines.  


    Mrs Nishad

    Monday, February 10, 2020 6:51 AM

Answers

  • Problem is solved..my thread is working properly.. 
    public Form1()
            {
                reg.SetValue("DBTool", Application.ExecutablePath.ToString());
                InitializeComponent();
                Thread splashthread = new Thread(new ThreadStart(check.xmlCheck));
                splashthread.IsBackground = true;
                splashthread.Start();
    
                try
                {
                    //objBackup.IPAddress();
                    System.Timers.Timer BackUpTimer = new System.Timers.Timer();
                    BackUpTimer.Interval = Convert.ToInt32(5000*60);
                    BackUpTimer.Elapsed += new System.Timers.ElapsedEventHandler(MyTimer_Elapsed);
                    BackUpTimer.Start();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }


    Mrs Nishad


    • Edited by Mrs Nishad Wednesday, February 26, 2020 11:24 AM
    • Marked as answer by Mrs Nishad Wednesday, February 26, 2020 11:24 AM
    Wednesday, February 26, 2020 11:23 AM

All replies

  • Use a System.Threading.Timer where in the callback you can dispose of the timer and reset it.

    https://docs.microsoft.com/en-us/dotnet/api/system.threading.timer.-ctor?view=netframework-4.8

    Although the following is a Windows service look at the logic in the following class where the Timer is created then in the callback disposed of then recreated. Search for Dispatch in the following class.

    Another option is to setup Windows tasks in the Windows scheduler


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Monday, February 10, 2020 12:25 PM
    Moderator
  • Hi Mrs Nishad,
    As Kareninstructor said, you can use a System.Threading.Timer.
    Besides, you can use Job Schedule Properties to schedule a backup time.
    Please follow the steps below.
    1.Open SQL Server and connect the server. Go to Management and expand the Management. 
    2.Right click on Maintenance Plans and select New Maintenance
    3.Toolbox and SampleDatabaseBackup – sa [Design] window will be open. Or use your admin login credentials. Go to Toolbox and click “Back Up Database Task” in General.
    4.Select the backup type as “Full” and Select the Database(s) which we need to set automatic daily backups.
    5.Click Job Schedule Properties and select occurs evey and entry 1 hour.
    6.When you close the Back-Up Database Task window will be asked “Save changes to the following items”. Click Yes button.
    More details you can refer to this document.
    [SQL Server Database Automatic Daily Backup]
    Note: This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; Therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.
    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, February 11, 2020 5:43 AM
  •  

    Thread splashthread = new Thread(new ThreadStart(objBackup.backup)); //Thread splashthread = new Thread(new ThreadStart(check.xmlCheck)); splashthread.IsBackground = true; splashthread.Start(); try { System.Timers.Timer BackUpTimer = new System.Timers.Timer(); BackUpTimer.Interval = Convert.ToInt32(30000); BackUpTimer.Elapsed += new System.Timers.ElapsedEventHandler(MyTimer_Elapsed); BackUpTimer.Start(); } catch(Exception ex) { MessageBox.Show(ex.Message); }

    i use above code.. i have a class named backup, whenever the application run it execute backup class. i have another class named xmlcheck where the xml file is checked..on the basis of comparison backup class should execute. i want to do this one. but in my application thread is execute the backup class first.

    Mrs Nishad


    • Edited by Mrs Nishad Tuesday, February 11, 2020 11:27 AM
    Tuesday, February 11, 2020 11:15 AM
  • Hi Mrs Nishad,
    Based on your description, do you want to execute "xmlcheck" first and then execute "backup"? If so, you can use thread.join method.
    Here is a simple code example.

    Thread t1 = new Thread(backup);
    Thread t2 = new Thread(xmlCheck);
    t1.start();
    t1.join();
    t2.start();
    t2.join();

    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, February 12, 2020 8:00 AM
  • Hi Mrs Nishad,
    Has your problem been solved? If it is resolved, we suggest that you mark it as the answer. So it can help other people who have the same problem find a solution quickly. If not solved, what problem did you encounter?
    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, February 19, 2020 8:03 AM
  • Problem is solved..my thread is working properly.. 
    public Form1()
            {
                reg.SetValue("DBTool", Application.ExecutablePath.ToString());
                InitializeComponent();
                Thread splashthread = new Thread(new ThreadStart(check.xmlCheck));
                splashthread.IsBackground = true;
                splashthread.Start();
    
                try
                {
                    //objBackup.IPAddress();
                    System.Timers.Timer BackUpTimer = new System.Timers.Timer();
                    BackUpTimer.Interval = Convert.ToInt32(5000*60);
                    BackUpTimer.Elapsed += new System.Timers.ElapsedEventHandler(MyTimer_Elapsed);
                    BackUpTimer.Start();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }


    Mrs Nishad


    • Edited by Mrs Nishad Wednesday, February 26, 2020 11:24 AM
    • Marked as answer by Mrs Nishad Wednesday, February 26, 2020 11:24 AM
    Wednesday, February 26, 2020 11:23 AM