简体   繁体   English

我正在尝试从SQL Server 2008备份和还原数据库

[英]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 我正在尝试备份和还原SQL Server 2008数据库,但出现错误提示

Incorrect Syntax near database name 数据库名称附近的语法不正确

Here is the code for back up and restore which is written c# asp.net. 这是用于备份和还原的代码,编写为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);
    }
}

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. BACKUP DATABASE和数据库名称之间没有空格,这将是第一个问题,第二个(恢复)代码也是如此。

You could attach yourself in debug mode to see what is the SQL command after you have built all your sql query string. 建立所有sql查询字符串后,可以将自己附加在调试模式下以查看SQL命令是什么。 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. 有了它之后,可以将其发布到SQL Server Management Studio中,并在那里查看错误提示,这可能会更容易。

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. 最后但并非最不重要的一点是,如果您无法轻松调试应用程序或弹出SQL查询(由于显示ex.Message显然不是这种情况),则可以运行SQL Server Profiler会话来检查针对SQL Server执行的查询。

You're missing a few spaces in your SQL statements! 您在SQL语句中缺少一些空格

Check this out: 看一下这个:

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

If you select a database called TEST , this becomes: 如果选择一个名为TEST的数据库, TEST变为:

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! 需要有之间至少有一个空格 BACKUP 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 (eg 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] .....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM