none
Error Path Backup Directory SQL Server from C#

    Question

  • Dear Friends,

    I just try to make a backup SQL Server using C#

    but for the location I just put it into text editor

    and this is my code:

                

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Data.SqlClient;
    using System.Drawing;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;

    namespace AutoBackup
    {
        public partial class Form1 : Form
        {
            string path = Path.Combine(Environment.CurrentDirectory, @"SettingAutoBackup\AutoBackup.txt");
            public Form1()
            {
                InitializeComponent();
            }

            private void button1_Click(object sender, EventArgs e)
            {
                var data = File
                //.ReadAllLines("G:\\Project Steven\\HIS LIS automatic execute Store Procedure\\Notepad Value\\Text_To_C_Sharp\\test.txt")
                .ReadAllLines(path)
                .Select(x => x.Split('='))
                .Where(x => x.Length > 1)
                .ToDictionary(x => x[0].Trim(), x => x[1]);

                string dir = data["LocationHIS"];
                string database = data["DatabaseHIS"];

                string connectionString = "Data Source= SVR-01; Initial Catalog=test; User Id=sa; Password=xxx";
                SqlConnection con = new SqlConnection(connectionString);
                try
                {
                    //string cmd = "BACKUP DATABASE test TO DISK= 'C:\\Backup\\Test_" + DateTime.Now.ToString("yyyyMMdd--HH-mm-ss") + ".bak'"; --> It works fine

                    string cmd = "BACKUP DATABASE test TO DISK='" + Path.Combine(dir,database)+ DateTime.Now.ToString("yyyyMMdd--HH-mm-ss")+".bak'"; --> it doesn't work, nothing happen with this script

                    using (SqlCommand command = new SqlCommand(cmd, con))
                    {
                        if (con.State != ConnectionState.Open)
                        {
                            con.Open();
                        }
                        command.ExecuteNonQuery();
                        con.Close();
                        MessageBox.Show("database backup done successefully");
                    }
                }

               catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }
     }

    Does anyone could help me

    Regards,

    Steve Henry

    Wednesday, May 3, 2017 7:36 AM

All replies

  • Hi steve_henry,

    Thank you for posting here.

    For your question, please change the path in .txt file like following.

    DatabaseHIS=Test
    LocationHIS=C:\Backup\
    

    I text the your code. cmd is the code which works fine. cmd is the code which does not work.

      //string cmd = "BACKUP DATABASE test TO DISK= 'C:\\Backup\\Test_" + DateTime.Now.ToString("yyyyMMdd--HH-mm-ss") + ".bak'"; --> It works fine
    
                    string cmd2 = "BACKUP DATABASE test TO DISK='" + Path.Combine(dir,database)+ DateTime.Now.ToString("yyyyMMdd--HH-mm-ss")+".bak'"; --> it doesn't work, nothing happen with this script
    I make a messageBox to show the path. Please have a try.

    I hope this would be helpful.

    Best Regards,

    Wendy


    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, May 3, 2017 9:23 AM
    Moderator