Copying .mdf file from app_data folder to default localhost folder - sql-server

My friend gave me a database file: record.mdf. I copied that .mdf file to my app_data folder and I can access it.
However, the connection string contains absolute path:
AttachDbFilename="C:\Users\Dell\Documents\Visual Studio 2010\Projects\WebApplication2\WebApplication2\App_Data\record.mdf"
But I want it to connect using:
Data Source=localhost\SQLEXPRESS;
How do I copy .mdf file to SQL Server's local folder, so that the connection string does not use an absolute path to the database?
I am using Visual Studio 2010. I do not have SQL Server Management Studio.

Step 1: you need to find out your SQL Server's data directory. This will be something like
C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\Data
by default (for SQL Server 2008 R2 Express) - it might be slightly different in your case, depending on how you installed your SQL Server Express (and which version you have).
Step 2: copy that record.mdf file to that directory
Step 3: attach it to your SQL Server Express instance - using sqlcmd if you don't have Mgmt Studio at hand:
c:\> sqlcmd -S .\SQLExpress
Then at the sqlcmd prompt, type in:
USE [master]
GO
CREATE DATABASE record ON
(FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\Data\record.mdf' )
FOR ATTACH_REBUILD_LOG;
GO
This will attach the .mdf file as your new "logical" database record to your SQL Server Express instance, and it will rebuild the missing transaction log file (.ldf) in the process.
From now on, you can use
server=.\SQLEXPRESS;Database=record;Integrated Security=SSPI;
as your connection string to connect to your database

Rather than copying it to the SQL server local folder, you can access it from the App_Data directory using |DataDirectory|\record.mdf
Documentation: http://msdn.microsoft.com/en-us/library/ms247257(v=vs.80).aspx

Related

SQL Server: attach database without a log file

I have uninstalled my SQL Server 2019 and installed SQL Server 2022 Express. All my databases are under:
C:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\DATA
My new location is:
C:\Program Files\Microsoft SQL Server\MSSQL16.MSSQLSERVER\MSSQL\DATA
Attaching a database directly from the old location results in:
Unable to open the physical file "C:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\DATA\DBName.mdf". Operating system error 5: "5(Access is denied.)".
I then copied the .mdf file to the new location. But trying to attach it from the new location, I get a similar error, but for the log file:
Unable to open the physical file "C:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\DATA\DBName_log.ldf". Operating system error 5: "5(Access is denied.)".
I can't copy the log file into the new location because I don't have enough space on my HD.
Any idea?
Thanks
Operating system error 5: "5(Access is denied.)".
SQL Server locks down the data folder with NTFS ACLs. So you can fix this by taking ownership of the folder and files and granting full control to the other SQL Server instance.
SQL Server disables permissions inheritance on the files, so you need to grant full control to the other SQL instance to each file. And if you're connected using Windows Integrated Auth, you'll also need to grant yourself full control over the files.
In my test, after granting full control over the mdf and ldf to 'NT Service\MSSQLSERVER' and to my windows user, I was able to run
CREATE DATABASE [Foo]
ON (FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL15.SQLEXPRESS\MSSQL\DATA\Foo.mdf' ),
(FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL15.SQLEXPRESS\MSSQL\DATA\Foo_log.ldf' )
for attach
from the default instance.
You can also move (not copy) the files without needing additional space.

How to attach a database by mdf file that has not been detached

Issue:
OS hard disk was broke before DB backing up, the original .mdf and .ldf files can be transferred but those .mdf and .ldf files can not be attached by SQL Server Management Studio directly.
Solution:
Assuming the DB name is ‘Sample’ and DB files path is ‘D:\DB’
Step 1: Open Microsoft SQL Server Management Studio to create a New Database with
Database name: Sample
Path: D:\DB
Step2: Stop the DB Server
Step3: Delete Sample.mdf and Sample.ldf files from folder D:\DB
Step4: Copy the old Sample.mdf to the folder D:\DB
Step5: Start the DB Server
Step6: execute the following sql to detach DB
exec sp_detach_db Sample,'true'
Step7: execute the following sql to attach DB and then refresh
exec sp_attach_single_file_db 'Sample','D:/DB/Sample.mdf'

Can MSSQL server be configured to create a database in a specified folder?

I use SQL Server 2008. Each time when I create a new database with it, it attempts to the database in the destination folder like this:
C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA
I understand that I can manually change it. But I am curious whether the default destination folder can be configured to another folder.
This can be configured in the Database Settings of the Server Properties:

Attach Database using Management studio points to the wrong file path for the log file

I am trying to attach a SQL server 2000 MDF and LDF which were created on a different machine and attach them to a SQL Server 2008 R2 on different machine. The SQL server files at the original machine were located at:
C:\DB\SqlServerDataBase.mdf
D:\Logs\SqlServerDatabase.ldf
The 2008 Sql Server machine also contains the same file structure and I place the MDF and LDF files in their appropriate folders:
C:\DB\SqlServerDataBase.mdf
D:\Logs\SqlServerDatabase.ldf
When using the SQL Server Management studio Attach Database option, I point to the MDF file however the log file is automatically is pointed to C:\DB\SqlServerDatabase.ldf instead of D:\Logs\SqlServerDatabase.ldf and subsequently the attach fails since it can not find the log file.
Interesting thing is if I use :
EXEC sp_attach_db #dbname =N'SqlServerDataBase',
#filename1=N'C:\DB\SqlServerDataBase.mdf',
#filename2=N'D:\Logs\SqlServerDatabase.ldf';
The database is restored and pointing to the correct paths for both MDF and LDF.
Thanks
When you're using the GUI (SQL Server Management Studio) and you select Attach Database and go pick a .mdf file, by default SQL Server Mgmt Studio will use the path that the database log file was originally stored on - since that's the only information it has.
But you can and you must change that to match your current setup - just type in the proper path (or select it from the path selector popup when you click on [...] if you prefer that)
Once you've typed in the correct paths, click [OK] and your database will be attached as requested.

SQL Server database export to another computer

I use SQL Server Managment Studio, SQL Server Express. I gererate a script with schema and data from Task -> Generate Script. And I try to import this script in other computer with the same SQL Server but this giving me error :
Directory lookup for the file "C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\WHFM.mdf" failed with the operating system error 3 (The system cannot find the path specified.).
What should I have done to import /export entire database from one PC to another .
If you read through the first few lines of the script, you will see code that specifies the name and location of the files of the database. In your case look for 'C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\WHFM.mdf' in your script. Then look at the directory structure of the target machine. You will need to either:
1. create a directory 'C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\' and make sure the sql instance on the machine has RW access to that directory.
or much better,
2. Modify the script to put the file in a location that the target server wants to put files.
In regards to #2, you can have the target server give you a hint as to where to put files by right-clicking on "Databases" in Microsoft SQL Server Management Studio and create a script to create a database. The directory of the files in the create database script from the target-server should show you what to do.

Resources