I am attaching My DB through vb.net code.
Now I request help for the following:
How to detach it using code (uid=sa, pwd=abc123)?
How to take backup?
How to restore backup?
Please note I want to do it all using vb.net code
Thanks a lot.
This article series is a great place to get started and see samples - also in VB.NET.
Getting started with SMO in SQL Server 2005
And even if you encounter C# samples - there's always the online Convert C# to VB.NET site to convert those to VB.NET
Backup:
USE AdventureWorks2008R2;
GO
BACKUP DATABASE AdventureWorks2008R2
TO DISK = 'Z:\SQLServerBackups\AdventureWorks2008R2.Bak'
WITH FORMAT,
MEDIANAME = 'Z_SQLServerBackups',
NAME = 'Full Backup of AdventureWorks2008R2';
GO
Restore:
----Put database into single user mode (terminates open connections - else restore fails)
ALTER DATABASE YourDB
SET SINGLE_USER WITH
ROLLBACK IMMEDIATE
RESTORE DATABASE YourDB
FROM DISK = 'D:\temp\YourDB.bak'
WITH REPLACE
,MOVE 'YourDB_Data' TO 'C:\Program Files\Microsoft SQL Server\MSSQL\Data\YourDB_Data.mdf'
,MOVE 'YourDB_Log' TO 'C:\Program Files\Microsoft SQL Server\MSSQL\Data\YourDB_Data.ldf'
ALTER DATABASE YourDB SET MULTI_USER
GO
Detach:
EXEC master.dbo.sp_detach_db #dbname = N'AdventureWorks',
#keepfulltextindexfile = N'true'
GO
Attach:
EXEC master.dbo.sp_attach_db #dbname = N'AdventureWorks2008R2',
#filename1 = N'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\Data\AdventureWorks2008R2_Data.mdf',
#filename2 = N'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\Data\AdventureWorks2008R2_log.ldf';
Related
I have created a small scrip to restore databases from backups. Once the script is run it displays
RESTORE DATABASE successfully processed 28818 pages in 1.568 seconds
(143.584 MB/sec).
I have more code to alter the database, alter a few views and sp too but I am getting the following error; User does not have permission to alter database 'GreyGoo', the database does not exist, or the database is not in a state that allows access checks.
I have noticed too that I cannot see the database in the object explorer
this is what I use to restore the DB from a backup
if DB exists set to a single user
if DB exist drop database
Ran the below script
RESTORE DATABASE GreyGoo FROM DISK = 'C:\Bkp\GreyGoo_backup_2020_03_02_180002_5403592.bak'
WITH
MOVE 'GreyGoo' TO 'C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQLSERVER\MSSQL\DATA\GreyGoo.mdf',
MOVE 'GreyGoo_log' TO 'C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQLSERVER\MSSQL\DATA\GreyGoo.ldf',
REPLACE;
set DB to multi-users and other properties
So what is the problem and how can I solve it, I am currently testing my code on SQL Server 2008
Thanks
During your restore process, you will need to ensure your
login / permissions are correct.
if another previous database with same name is not there, if so delete it. Perhaps from a previous attempt
You can also turn off recovery option like below, recovery is default
RESTORE FILELISTONLY
Sample -- MSDN
USE master;
GO
-- First determine the number and names of the files in the backup.
-- AdventureWorks2012_Backup is the name of the backup device.
RESTORE FILELISTONLY
FROM AdventureWorks2012_Backup;
-- Restore the files for MyAdvWorks.
RESTORE DATABASE MyAdvWorks
FROM AdventureWorks2012_Backup
WITH RECOVERY,
MOVE 'AdventureWorks2012_Data' TO 'D:\MyData\MyAdvWorks_Data.mdf',
MOVE 'AdventureWorks2012_Log' TO 'F:\MyLog\MyAdvWorks_Log.ldf';
GO
EXEC msdb.dbo.sp_delete_database_backuphistory #database_name = N'MYDB'
GO
USE [master]
GO
/* Database [MYDB] */
DROP DATABASE [MYDB]
GO
USE [master]
RESTORE DATABASE [MYDB] FROM DISK = N'E:\FTP\local_MYDB_01.bak' WITH FILE = 1, MOVE N'blank' TO N'E:\DBs\2019\MYDB.mdf', MOVE N'blank_log' TO N'E:\DBs\2019\MYDB.ldf', NOUNLOAD, STATS = 5
GO
Compared to SSMS, Datagrip doesn't really seem to have the "Restore Database" option, so apparently the only way is through script.
I tried the following:
RESTORE FILELISTONLY
FROM DISK = 'C:\Users\Marcelo\Downloads\Northwind.bak'
GO
RESTORE DATABASE Northwind
FROM DISK = 'C:\Users\Marcelo\Downloads\Northwind.bak'
WITH
MOVE 'Northwind' TO 'C:\Program Files\Microsoft SQL Server\MSSQL14.LENOBRAC\MSSQL\DATA\Northwind.mdf',
MOVE 'Northwind_log' TO 'C:\Program Files\Microsoft SQL Server\MSSQL14.LENOBRAC\MSSQL\DATA\Northwind_log.ldf',
GO
[2019-02-22 15:29:41] completed in 1 s 117 ms
The console shows that the task has been completed with no problems, however the database does not appear on the side explorer, I also cannot select any tables or do anything, I can only drop the database.
What could be the reason for this?
Just try,
USE [master]
RESTORE DATABASE Northwind
FROM DISK = N'C:\Users\Marcelo\Downloads\Northwind.bak'
WITH FILE = 1, NOUNLOAD, REPLACE, STATS = 5
GO
Right click on the hostname / datasource name in the explorer panel
Go to database tools
you'll find an option called manage shown schemas , just tick the
mark next to databases you want to see
We have an issue where one of our team didn't do backup to our database because he told us that when he tried to backup he received the error "database the transaction log for database is full due to 'checkpoint'", so what he did was Detach. Now, I'm try to attach the database again but I received also the error atabase the transaction log for database is full due to 'checkpoint' using UI and SQL Query.
USE [master]
GO
CREATE DATABASE [CXSCSNSVR] ON
( FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL11.CSSVR\MSSQL\DATA\backup\CXSCSNSVR_Data.MDF' ),
( FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL11.CSSVR\MSSQL\DATA\backup\CXSCSNSVR_Data_log.LDF' )
FOR ATTACH
GO
Our drives have sufficient space 1TB to be exact.
I've tried also to changed the location of the LOG file but still the same error.
RESTORE DATABASE Tes
FROM DISK = '{7522204E-0256-47B3-9864-137D6D1FD449}6'
WITH MOVE 'Test' TO 'C:\Program Files (x86)\Microsoft SQL Server\MSSQL12.SQLEXPRESS\MSSQL\DATA\Tes.mdf',
MOVE 'Test_log' TO 'C:\Program Files (x86)\Microsoft SQL Server\MSSQL12.SQLEXPRESS\MSSQL\DATA\Tes.ldf'
The above restore command is working fine for new dbs, But few old databases it showing a errors like
The file 'C:\Program Files (x86)\Microsoft SQL Server\MSSQL12.SQLEXPRESS\MSSQL\DATA\db1.mdf' cannot be overwritten. It is being used by database 'windb'.
Msg 3156, Level 16, State 4, Line 1
File 'windb' cannot be restored to 'C:\Program Files (x86)\Microsoft SQL Server\MSSQL12.SQLEXPRESS\MSSQL\DATA\db1.mdf'. Use WITH MOVE to identify a valid location for the file
I found that only dbs having more than one ndf files having this issues ..
Any solutions this issue ?
Try to restore the database from the Restore Database Wizard. For that, in the Object Explorer, right-click on Databases -> select Restore Databases.... In the window that appears, set the necessary details for the Source and the Destination. And in the left pane, either select Options or Files, depending on the SSMS version you have.
In the table that appears, find the column Restore As. In each row, give a new file name, instead of the name that shows. For example, in your case it might be showing the file names as something similar to <Folder Path>\DATA\Tes.mdf and <Folder Path>\DATA\Tes.ldf. Give a different name other than Tes. (A name that does not already exist in the DATA folder).
Note: The file names do not have anything to do with the database name, but it will easy referencing later on to give a meaningful name, in case you have to backup or look for the files.
If you are trying to move your database to a new location, you should better detach the source database or take it offline first
I faced this issue when working on an MS SQL Server 2012.
I was trying to restore a database using the script below
USE master;
GO
ALTER DATABASE my_db SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
GO
RESTORE DATABASE my_db.dev
FROM DISK = 'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\Backup\com.mydb.dev.bak'
WITH REPLACE,
STATS = 10,
RESTART,
MOVE 'com.mydb.dev' TO 'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\com.mydb.dev.mdf',
MOVE 'com.mydb.dev_log' TO 'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\com.mydb.dev_log.ldf'
GO
ALTER DATABASE com.mydb.dev SET MULTI_USER;
GO
And then this was throwing the error below when I tried executing the restore task:
The file 'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\com.mydb.dev.mdf' cannot be overwritten. It is being used by database restdb
The file 'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\com.mydb.dev_log.ldf' cannot be overwritten. It is being used by database restdb
Here's how I fixed it:
The issue was that the database backup file that I was using for this database restore operation had been previously used to restore data to a different database on the same server.
All I had to do was to change the Physical names of the Logical files to that of the database that I was to restore to. That is from com.mydb.dev.mdf to my_db.mdf and from com.mydb.dev_log.ldf to my_db_log.ldf:
USE master;
GO
ALTER DATABASE my_db SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
GO
RESTORE DATABASE my_db
FROM DISK = 'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\Backup\com.mydb.dev.bak'
WITH REPLACE,
STATS = 10,
RESTART,
MOVE 'com.my_db.dev' TO 'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\my_db.mdf',
MOVE 'com.mydb.dev_log' TO 'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\my_db_log.ldf'
GO
ALTER DATABASE my_db SET MULTI_USER;
GO
And this time, the database restore task ran successfully.
That's all.
I hope this helps
I have a SQL server data backup size is above 20 GB. My database is in E drive have enough space but my SQL software installed in C drive which has low memory. In this scenario how can i restore my backup to E drive.
You can restore database via SQL Server Management Studio.
While restoring , you can specify MDF and LDF file location to your E: drive.
To specify the new location of the database files, select the Files
page, and then click Relocate all files to folder. Provide a new
location for the Data file folder and Log file folder
THIS MIGHT HELP
steps :
ALTER DATABASE database_name SET OFFLINE;
Move the file or files to the new location.
For each file moved, run the following statement.
ALTER DATABASE database_name MODIFY FILE ( NAME = logical_name, FILENAME = 'new_path\os_file_name' );
Run the following statement.
ALTER DATABASE database_name SET ONLINE;
Verify the file change by running the following query.
SELECT name, physical_name AS CurrentLocation, state_desc
FROM sys.master_files
WHERE database_id = DB_ID(N'<database_name>');
for more details go through url
USE MASTER;
GO
ALTER DATABASE DBName
SET SINGLE_USER
WITH ROLLBACK IMMEDIATE;
GO
-- Detach DB
EXEC MASTER.dbo.sp_detach_db #dbname = N'DBName'
GO
Step 1:Detach DB
USE MASTER;
GO
ALTER DATABASE DBName
SET SINGLE_USER
WITH ROLLBACK IMMEDIATE;
GO
-- Detach DB
EXEC MASTER.dbo.sp_detach_db #dbname = N'DBName'
GO
Step 2:
Now move the files from C drive to E drive. You can now reattach the files with E drive.