none
Error Path in C#

    Question

  • dear friends,

    I just got an error when I try to create form for Backup SQL server


    here's 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(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"];
                label1.Text = dir;

                string a = label1.Text;

                //string location = System.IO.Path.Combine(dir, database);

                string connectionString = "Data Source= SVR-01; Initial Catalog=test; User Id=sa; Password=123456";
                SqlConnection con = new SqlConnection(connectionString);
                try
                {

                    string cmd = "BACKUP DATABASE test TO DISK='" + @a + "test_"+ DateTime.Now.ToString("yyyyMMdd--HH-mm-ss") + ".bak'";

                    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 ?

    Thank You

    Regards,

    Steve Henry

    Wednesday, May 3, 2017 8:26 AM

Answers

  • dear friends,

    I just got an error when I try to create form for Backup SQL server


    Seems like you have nested directory in your path value.

    C:\Program Files\Microsoft SQL Server\MSSQL12\MSSQL\Backup\C:Backup\test......

    Could please put a debug pointer where you write code for path location and see what output you get.


    Thanks,
    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]

    Wednesday, May 3, 2017 3:54 PM
    Moderator

All replies

  • dear friends,

    I just got an error when I try to create form for Backup SQL server


    Seems like you have nested directory in your path value.

    C:\Program Files\Microsoft SQL Server\MSSQL12\MSSQL\Backup\C:Backup\test......

    Could please put a debug pointer where you write code for path location and see what output you get.


    Thanks,
    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]

    Wednesday, May 3, 2017 3:54 PM
    Moderator
  • Hi Steve Henry,

    If your issue is solved please Mark as answer or Vote as helpful post to the appropriate answer so that it will help other members to find solution if they faces similar issue.

    Your understanding and cooperation will be grateful.

    Thanks,
    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]

    Friday, May 12, 2017 12:55 PM
    Moderator