Dim sql As String = "RESTORE DATABASE [Northwind] " & _
"FROM DISK = N'C:SQL Server DatabasesNorthwind.bak' " & _
"WITH FILE = 1," & _
"Move N'Northwind' TO N'C:SQLNorthwind.mdf'," & _
"Move N'Northwind_log' TO N'C:SQLNorthwind_1.ldf'," & _
"NOUNLOAD, STATS = 10"
Dim cn As SqlConnection = New SqlConnection("Data Source=rrehak;Initial
Dim cmd As SqlCommand = New SqlCommand(sql, cn)
Make sure the account you use in the connection string has sufficient privileges for the RESTORE command. If the database does not exist, the user needs CREATE DATABASE privileges. If the database already exists, the user has to be a member of the sysadmin or dbcreator server roles, or be the owner (dbo) of the database.
This was first published in February 2008