Click here to Skip to main content
16,004,574 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I want to get the backup data.I'm getting correct Location as well. But when i click the backup button, its showing an error "
Incorrect Syntax near 'SystemTO'
. I don't know where i'm going wrong. Please assist me.
Here:
Button4 = backup button
Button3 = Browse button

What I have tried:

C#
private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                connectionString = "Data Source = " + textBox1.Text + "; User Id = " + textBox2.Text + "; Password = " + textBox3.Text + "";
                con = new SqlConnection(connectionString);
                con.Open();
               // sql = "EXEC sp_databases";
                sql = "SELECT * FROM sys.databases d WHERE d.database_id>4";
                command = new SqlCommand(sql, con);
                reader = command.ExecuteReader();
                comboBox1.Items.Clear();
                while (reader.Read())
                {
                    comboBox1.Items.Add(reader[0].ToString());
                }
                    textBox1.Enabled = false;
                    textBox2.Enabled = false;
                    textBox3.Enabled = false;
                    button1.Enabled = false;
                    button2.Enabled = true;

                    comboBox1.Enabled = true;

                    button4.Enabled = true;
                    button5.Enabled = true;

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

        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Enabled = true;
            textBox2.Enabled = true;
            textBox3.Enabled = true;
            comboBox1.Enabled = false;
            button4.Enabled = false;
            button5.Enabled = false;
            button1.Enabled = true;

        }

        private void Backup_or_Restore_Data_Load(object sender, EventArgs e)
        {
            button2.Enabled = false;
            comboBox1.Enabled = false;
            button4.Enabled = false;
            button5.Enabled = false;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            try
            {
                if (comboBox1.Text.CompareTo("") == 0)
                {
                    MessageBox.Show("Please Select a Database");
                    return;
                }
                con = new SqlConnection(connectionString);
                con.Open();
                sql = "BACKUP DATABASE " + comboBox1.Text + " TO DISK = '" + textBox4.Text + "\\" + comboBox1.Text + "-" + DateTime.Now.Ticks.ToString() + ".bak'";
                command = new SqlCommand(sql, con);
                command.ExecuteNonQuery();
                MessageBox.Show("Successfully Database Backup Completed. ");
                }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dlg = new FolderBrowserDialog();
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                textBox4.Text = dlg.SelectedPath;
            }

        }
                        
    }
}
Posted
Updated 17-Mar-20 22:31pm
v2
Comments
CPallini 18-Mar-20 3:09am    
There is no 'systemTO' occurrence in the posted code.
ishuishika 18-Mar-20 3:23am    
Sir,

Yes sir, there is no SystemTo in the program. But I'm getting that error same error.
Richard MacCutchan 18-Mar-20 4:28am    
Where exactly does the error occur?
F-ES Sitecore 18-Mar-20 6:54am    
Try

sql = "BACKUP DATABASE [" + comboBox1.Text + "] TO DISK = '" + textBox4.Text + "\\" + comboBox1.Text + "-" + DateTime.Now.Ticks.ToString() + ".bak'";

I added square brackets around the database name

Try comboBox1.SelectedItem.ToString() instead of combobox1.Text

Although you should be using parameters, instead of building a string in your code.
 
Share this answer
 
Comments
ishuishika 18-Mar-20 5:40am    
now I'm getting error as Incorrect Syntax near 'System'.
I don't know where exactly the mistake is from
Richard MacCutchan 18-Mar-20 5:47am    
You have all the information, which we do not. So look at the actual SQL command that you are generating. Are all fields correct, are there any illegal characters in it, etc. ? Try to do the basic diagnostic analysis for yourself before you post here.
This is most likely the problem:
C#
sql = "BACKUP DATABASE " + comboBox1.Text + " TO DISK = '" + textBox4.Text + "\\" + comboBox1.Text + "-" + DateTime.Now.Ticks.ToString() + ".bak'";

You should not use string concatenation to create SQL statements. Capture the various data items first and validate them before using them in a command.
 
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