Restore a local mdf file using vb.net

Hi I have a app that is now using a local mdf file which works great.
I can do a backup fine to .bak file but how can I restore it?
it says the file is in use and to use the master but its not on a server its just a local server file.
LVL 3
taz8020Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ElrondCTCommented:
Assuming that you're referring to a SQL Server database (.mdf can be used for other things, but you've tagged this in the SQL Server topic area), you would execute a SQL Scalar command, like this:

        Dim connSQL As SqlClient.SqlConnection
        Dim strConn As String = "Data Source=" & Server & ";Integrated Security=SSPI;Initial Catalog=Master"
        connSQL.ConnectionString = strConn
        connSQL.Open()
        query = "RESTORE DATABASE " & strDB & " FROM DISK='" & strBackup & "' WITH REPLACE")
        Using cmd As New SqlCommand(query, connSQL)
            intReturn = cmd.ExecuteScalar()
        End Using

Open in new window


Server is the name of your SQL Server instance; strDB is the name of the SQL database; strBackup is the full path of the backup file. This code assumes Windows authentication; using SQL Server authentication requires a different connection string. If there's a problem, intReturn will give an error code.

This is almost the same as how you could do a backup; you just change the query command. Are you doing your backup in a different way?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Vitor MontalvãoMSSQL Senior EngineerCommented:
Your question isn't clear. The title is referring to .mdf file and you are describing .bak file.
Can you clarify?
0
taz8020Author Commented:
yes sorry for the delay its a sql local mdf file that I want to be able to back up to a .bak file and restore it if necessary.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Vitor MontalvãoMSSQL Senior EngineerCommented:
Backups are made in SQL Server. SQL Server engine blocks any tentative to access a database file and that's why you are getting the error.
You need to run a BACKUP command inside the SQL Server and then you'll have the .bak file necessary to restore whenever and where you pretend.
0
ElrondCTCommented:
The commands I showed work within VB.NET; they're actually run by the SQL Server instance under VB's control. I'm showing live code that I use in my software.
0
taz8020Author Commented:
perfect sorry for the delay been away
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.