在实体框架中备份数据库

时间:2013-05-19 14:15:05

标签: asp.net

我使用实体框架我需要传输该代码

RESTORE DATABASE [showing8-5-2013] FROM  DISK = N'C:\Program Files (x86)\Microsoft SQL Server\MSSQL10_50.SQLEXPRESS\MSSQL\Backup\Company.bak' WITH  FILE = 1,  NOUNLOAD,  REPLACE,  STATS = 10

编码实体框架工作

任何帮助谢谢

1 个答案:

答案 0 :(得分:1)

EF是数据库中立的提供商概念。这些命令本质上是DB特有的。 EF公开了一种执行SQL命令的方法:

MyContext.Database.ExecuteSqlCommand();

但你也可以直接这样做。 将SQL命令传递给自定义例程,例如:

private static bool ExecuteSqlStatement(string connectionString, string statement) {
        int rowsAffected;
        using (var sqlConnection = new SqlConnection(connectionString)) {
            using (var sqlCommand = new SqlCommand(statement, sqlConnection)) {
                try {
                    sqlConnection.Open();
                    rowsAffected = sqlCommand.ExecuteNonQuery();
                }
                catch (Exception ex) {
                    // your handler or re-throw.... 
                    return false;
                }
            }
        }

        return rowsAffected == -1;
        // see  http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executenonquery.aspx
    }