shweta
shweta

Reputation: 9

I'm trying to backup and restore database from SQL Server 2008

I'm trying to backup and restore a SQL Server 2008 database, but I'm getting an error saying

Incorrect Syntax near database name

Here is the code for back up and restore which is written c# asp.net.

Can I get a help regarding this?

private void btnbackup_Click(object sender, EventArgs e)
{
    try
    {
        if (combodatabase.Text.CompareTo("") == 0)
        {
            MessageBox.Show("Please select a Database");
            return;
        }

        con = new SqlConnection(connectionstring);
        con.Open();

        sql = "BACKUP DATABASE" + combodatabase.Text + " TO DISK='" + txtbackupfileloc.Text + "\\" + combodatabase.Text + "-" + DateTime.Now.Ticks.ToString() + ".bak'";

        cmd = new SqlCommand(sql, con);
        cmd.ExecuteNonQuery();

        con.Close();
        con.Dispose();

        MessageBox.Show("Database Backup Successfully Completed.");
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

Restore code:

private void btnrestore_Click(object sender, EventArgs e)
{
    try
    {
        if (combodatabase.Text.CompareTo("") == 0)
        {
            MessageBox.Show("Please Select a Database");
            return;
        }

        con = new SqlConnection(connectionstring);
        con.Open();

        sql = "ALTER DATABASE" + combodatabase.Text + "SET SINGLE_USR WITH ROLLBACK IMMEDIATELY ";
        sql += "RESTORE DATABASE" + combodatabase.Text + "From DISK='" + txtrestorefileloc.Text + "'With REPLACE;";

        cmd = new SqlCommand(sql, con);
        cmd.ExecuteNonQuery();

        con.Close();
        con.Dispose();

        MessageBox.Show("Database Successfully Restored");
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

Upvotes: 0

Views: 484

Answers (2)

marc_s
marc_s

Reputation: 755321

You're missing a few spaces in your SQL statements!

Check this out:

sql = "BACKUP DATABASE" + combodatabase.Text + " TO DISK='" + ....

If you select a database called TEST, this becomes:

BACKUP DATABASETEST TO DISK=.....

Or this:

sql = "ALTER DATABASE" + combodatabase.Text + "SET SINGLE_USR WITH ROLLBACK IMMEDIATELY ";
sql += "RESTORE DATABASE" + combodatabase.Text + "From DISK='" + txtrestorefileloc.Text + "'With REPLACE;";

becomes

ALTER DATABASETESTSET SINGLE_USR WITH ROLLBACK IMMEDIATELY
RESTORE DATABASETESTFrom DISK=......

There needs to be at least one space between the BACKUP DATABASE and the name of the database! Some throughout your code - you need to be more careful when stringing together keywords and placeholder - you need spaces between them at times!

Also, if your database name is "special" in any way (e.g. it contains a space itself, starts with a number etc.), you might need to put the database name into square brackets:

BACKUP DATABASE [Test DB] .....

BACKUP DATABASE [7dwarfs] .....

Upvotes: 0

Jakub Szumiato
Jakub Szumiato

Reputation: 1318

You don't have any whitespace between BACKUP DATABASE and the database name, that will be the first problem, the same goes for the second (restore) code.

You could attach yourself in debug mode to see what is the SQL command after you have built all your sql query string. It seems a lot of whitespaces are missing there. Once you have it, you could post it in SQL Server Management Studio and see tips for errors there, would be easier probably.

Last, but not least, if you can't debug your application easily or pop up the SQL query (which apparently isn't the case since you display the ex.Message), you could maybe run a SQL Server Profiler session to check the queries executed against SQL Server.

Upvotes: 3

Related Questions