I have an application which uses EF and LocalDB as it\'s database, published by ClickOnce. it\'s my first time using LocalDB and I don\'t know how can i add a feature to my
This is what I did for backup and restore of my localDb
public void BackupDatabase(string filePath)
{
using (TVend2014Entities dbEntities = new TVend2014Entities(BaseData.ConnectionString))
{
string backupQuery = @"BACKUP DATABASE ""{0}"" TO DISK = N'{1}'";
backupQuery = string.Format(backupQuery, "full databsase file path like C:\tempDb.mdf", filePath);
dbEntities.Database.SqlQuery<object>(backupQuery).ToList().FirstOrDefault();
}
}
public void RestoreDatabase(string filePath)
{
using (TVend2014Entities dbEntities = new TVend2014Entities(BaseData.ConnectionString))
{
string restoreQuery = @"USE [Master];
ALTER DATABASE ""{0}"" SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
RESTORE DATABASE ""{0}"" FROM DISK='{1}' WITH REPLACE;
ALTER DATABASE ""{0}"" SET MULTI_USER;";
restoreQuery = string.Format(restoreQuery, "full db file path", filePath);
var list = dbEntities.Database.SqlQuery<object>(restoreQuery).ToList();
var resut = list.FirstOrDefault();
}
}
Hope this is what you want.
I had a bugger of a time getting my backup/restore to work from code in my application. I'm using LOCALDB and wanted to make sure that regardless of the state of the database or the location of the .mdf file that the backup and restore functions would work. After all - the DBMS should take care of that for you. In the end this is how I got my backup and restore functions to work: Note: code in VB - save the ";" :)
Backup:
Dim cbdfilename As String = controlPath & "\Backup\Temp\cbdb.bak"
Dim connString As String = (server + ";Initial Catalog=master;Integrated Security=True;")
Dim conn As New SqlConnection(connString)
Dim sql As String
sql = "Backup database @DBNAME " _
& " to Disk = @FILENAME" _
& " with Format"
SqlConnection.ClearAllPools()
'execute backup
Dim dbcmd As New SqlCommand(sql, conn)
dbcmd.Parameters.AddWithValue("@DBNAME", database)
dbcmd.Parameters.AddWithValue("@FILENAME", cbdfilename)
conn.Open()
Try
dbcmd.ExecuteNonQuery()
Catch ex As Exception
MsgBox("Backup DB failed" + ex.ToString)
Finally
conn.Close()
conn.Dispose()
End Try
A key thing to note above is the SqlConnection.ClearAllPools() statement. Even though I was sure that all connections had been properly closed and disposed of in other parts of my app - somehow the DBMS was still showing an open thread.
And now the Restore:
SqlConnection.ClearAllPools()
Dim connString As String = (server + ";Initial Catalog=master;Integrated Security=True;")
Dim conn As New SqlConnection(connString)
Dim sql As String
sql = "Use master;" _
& "Alter Database " & database & " Set Single_User With Rollback Immediate;" _
& "Restore Database " & database & " From Disk = @FILENAME" _
& " With Replace;" _
& "Alter Database " & database & " Set Multi_User;"
'execute restore
Dim dbcmd As New SqlCommand(sql, conn)
dbcmd.Parameters.AddWithValue("@FILENAME", cbdfilename)
conn.Open()
Try
dbcmd.ExecuteNonQuery()
Catch ex As Exception
MsgBox("Restore DB failed" + ex.ToString)
Finally
conn.Close()
conn.Dispose()
End Try
What was really weird in the SQL above is that I initially tried to use @Parms for the database name but the ALTER statements would not accept them. Kept kicking back with exceptions.
The biggest difference between my restore and the one from the earlier solution is that I only use the database name ie. "MyDB_TEST" and not the .mdf file name in my Alter and Restore statements.