redned
redned

Reputation: 301

Making a back up of A SQL Server Compact Edition Corrupts the database

On close of an application written in VB, I need to copy the .sdf file, this is the local SQL Server CE database that syncs with the server. Each time we close the app we require a backup to be made.

I am using the file.copy method. First I close the connection to the database then copy the file to a specific location.

Dim conn As SqlCeConnection = New SqlCeConnection(My.Settings.MyClientConnectionString)
conn.Open()
conn.Close()

Dim tpath As String = My.Settings.CertPath 
Dim path As String = tpath.Replace("db\", "MyDatabase.sdf")
Dim fs As FileStream = File.Create(path)
fs.Close()
File.Delete("C:\db\backup\MyDatabase.sdf")
File.Copy(path, "C:\db\backup\MyDatabase.sdf")

The problem arises on the line of code:

Dim fs As FileStream = File.Create(path)

The database becomes corrupted and has a size of 0 kb.

Has anyone seen this before? Thanks

Upvotes: 0

Views: 1012

Answers (1)

Chris Dunaway
Chris Dunaway

Reputation: 11216

Are you calling Create on an existing database file? According to the docs, Create creates or overwrites a file in the specified path. In the code you show, calling Create isn't necessary anyway. Just call File.Copy.

Upvotes: 1

Related Questions