简体   繁体   English

通过C#代码从备份文件还原远程计算机上的MSSQL数据库

[英]Restore MSSQL database on remote machine from backup file via C# code

I have a mssql database on remote machine and backup file for this database. 我在远程计算机上有一个mssql数据库,并且该数据库有备份文件。 I want to create a method (c#) for restoring of database from backup. 我想创建一种用于从备份还原数据库的方法(c#)。 I will execute my method on my local machine. 我将在本地计算机上执行我的方法。 Can somebody help me to create such method for restoring remote database? 有人可以帮我创建恢复远程数据库的方法吗?

Try the this: 试试这个:

    public  void RestoreDatabase(string fileName)
    {
        try 
        {           
            using (SqlConnection conn = new SqlConnection("connectionString"))
            {
                string sql = "RESTORE DATABASE YourDatabase FROM DISK = N''" + fileName;
                conn.Open();
                SqlCommand _command = new SqlCommand(sql, conn);
                _command.ExecuteNonQuery();                
            }
        }
        catch (Exception ex)
        {
             throw;
        }
    }

You call it this way : 您这样称呼它:

    RestoreDatabase(@"\\remotemachine\...\YourFile.bak");

NB: Put an actual path for where the file is located 注意:放置文件的实际路径

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

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