How to copy SQL .mdf file in windows - sql-server

For my school project I need to send .mdf file to my teacher.
I'm trying to copy .mdf file of my database witch is saved in "C:\Program Files\Microsoft SQL Server\MSSQL15.SQL2019\MSSQL\DATA" folder. But windows doesn't let me do that. It gives me an error
Open Error: Access id denied.
I tried to change folder permissions but it didn't works. I don't want backup file or script file, because my teacher said to send .mdf file.
Is there any way to copy that?

Option1: Stop the Database services and copy it
Option2: Detach the database

Since it was restricted by windows, I made a bootable flash drive with linux and run it on startup and use it to copy mdf file. It seems to be the only way which works for me.

Related

All data erased when reboot of edit the program

Each time the program is run, or when the program is edited and run, the SQL Server database (.mdf file) is cleared and only the data given later is seen. All old data is cleared.
I made this program in Visual Studio 2010. How to fix this? How can the data be permanently saved to the SQL Server database?
Have you verified that you are looking at the correct MDF file? When you run your program it will copy to a folder under [ProjectFolder]/bin - if you are running the Debug configuration it will be [ProjectFolder]/bin/Debug/yourFile.mdf and if you are running the Release configurations it will be [ProjectFolder]/bin/Release/yourFile.mdf. These are separate from the MDF file that is likely in the project root ([ProjectFolder]). If you are making changes when the program is running, it will be saved (assuming your code is correct) to one of the MDF files that exist under the [ProjectFolder]/bin folder.

Running bat file from SQL server job

I have a bat file. If I run it, it does everything it's supposed to do.
Now I want to run that bat file from SQL server job.
RunProg.bat consists of this:
"C:\Program Files\R\R-3.3.2\bin\RScript.exe" "C:\Program Files\R\R-3.3.2\bin\QA_Check.R"
Error when I run job is this:
I think problem is in access. How can I fix it?
Users on my machine:
Having a similar issue, what worked for me was to change the location of the .bat file to a folder location where the SQL agent would run it. So, instead of "C:\Users\MyUser\BatFolder\my.bat", I moved it to "D:\BatFolder\my.bat" where the Agent had access.

Where to place database files in Visual Studio

If this question has been asked before please point me in the right direction.
I am working with an MDF file which I attach to localdb in Visual Studio.
However, when I run my application it is copied to the Debug folder (I know this is because the "Copy always" option is set)
This works fine because my connection string is:
Data Source=(localdb)\v11.0;AttachDbFilename=|DataDirectory|\Invoicing.mdf;Integrated Security=True
which means my application will look for the database in the Debug folder.
My question is, where should I place the database file because :
1. The file I am attaching to localdb is under the projects folder
2. Meanwhile, my application looks for the database file in the debug folder
I would appreciate any guidelines
I found this article:
http://msdn.microsoft.com/en-us/library/ms246989.aspx
I guess I will have to attach the one in the bin folder and choose the option to copy if newer
EDIT:
I have done the above and in addition I have created two connections, one for the MDF in the projects folder and the other to view data in the database in the bin folder

Text File Permissions Issue

What is the default SSIS Account that is used in SSIS packages.
In one of my packages I have a Data Flow task that creates a flat file. A BAT file later runs and creates a file based on information in that .txt file. If I execute the .BAT in Windows Explorer it runs fine. When SSIS tries to execute it I can see the CMD window open and it tries to access the txt file and isnt able to. Says "Unable to access nameoffile.txt"
I assume the issue is that the permissions are probably inherited by the SSIS user account so I am trying to figure out which account that is.
If you are running it from the IDE then it uses your permissions. Most likely the problem is that your file is locked by some other process in your SSIS package. To verify:
Add a PAUSE to your batch file to make it wait for any key.
Run your package. While the dos prompt is waiting, go into windows explorer and run your batch file again - you'll find the same error.
I changed the attrib value in dos for that specific folder by attrib -r -s and it worked. It thought it was read-only.
I am using Windows Server 2012 server, with a SQL Server 2012 database.
Adding Modify, Read, Write permissions to the folder containing the required file to the NETWORK SERVICE user seems to work for me.

Is there any way to undelete a SQL server database and stored procedures?

Is there any way to undelete a SQL server database and stored procedures?
Edit:
No I don't have a backup. That is the reason I need help
Restore from backup.
You do have a backup, right?
Edit:
I'm assuming you probably did a right-click, "Delete" from SQL Management Studio? If so, that actually does a "drop" of the database, which literally removes the files from the hard drive. If this is the case, you'll have to restore from your latest backup (if you have one).
If you just did a "detach", then the MDF and LDF files are still on the hard drive, and you can simply attach them.
How exactly have you deleted it.
If you haven't made any changes to you disks file system since then (and not defraged) then its possible the file will still be sitting on the disk as windows doesn't delete the file completely, just the File Handle.
Have a look at something like this and you may be able to recover the .mdf/.ldf files from your HD.
http://www.undelete.com/file-recovery.aspx
If not, you'll have to restore from your last back.
And if you don't have a backup... you're screwed.
If you accidently deleted the database in SQL Server Management Studio by right clicking and hitting delete in a hurry and you have no backup then there might still be a chance of getting back the database:
Don't do any save / delete file operations on the disk where the database file was stored, browsing should be ok.
Find the location where the .mdf file used to be. In my case it was "c:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS2012\MSSQL\DATA". If you have multiple databases you can right click on an existing one, select properties and files. Find the location entry in the listbox under path (select and ctrl+c to copy it)
Download a portable undelete program e.g. Recuva to a USB drive/stick (unzip) and run it from the USB drive/stick.
Set the copied path as the "search location" in your undelete program (ctrl+v), recover "YOURDATABASE.mdf" and "YOURDATABASE_log.ldf" to the USB drive.
Copy the files from USB drive/stick to the original location.
In SQL Server Management Studio right click on "Databases" select "Attach.." click Add, select "YOURDATABASE.mdf".
Get some coffee and think about backups :-)
Did you drop the database or detach it ? If you've detached it then just reattach it again from the mdf and ldf files.

Resources