Restoring database to a new server with a new name - c#

I am trying to create backup and restore of a database to new server with new name using C# and SQL server database. I can create a backup of the database but I am not able to restore it to the new server using the new name.
My query looks something like this: it doesn't work and errors out:
RESTORE DATABASE test1 FROM DISK = 'C:\Program Files\Microsoft SQL Server\MSSQL\BACKUP\setupdb\BackupForsetupdb.bak'
WITH MOVE 'setupdb_Data' TO '\\newserver\e$\MSSQL\DATA\test1_Data.MDF',
MOVE 'setupdb_log' TO '\\newserver\e$\MSSQL\DATA\test1_Log.LDF';
I am trying to achieve this through C# code.It looks like the database cannot be restored to the remote servers. Please throw some light on this.
Thanks!

You can't have SQL Server databases on network shares normally: local/SAN type drives only
The backup file can be on a share but the MDF and LDFs must be local
There is an MS KB article on it: it can be done but at your own risk

Your paths for the move command have to be relative to the server.
e:\MSSQL\Data\test1_data.mdf
And your restore from path has to be relative to the server as well. If the c:\ is from your local machine, you either need to point it to a UNC path (\\yourpc\c$\...) or move it to the server. But be aware that if using the UNC path option, the user the server process is running as has to have permissions to access the share as well. So you're probably better off copying to a drive on the remote computer.

Related

NOSQL (RavenDB) Query from a folder on C: Drive

I have a database on the C drive and I need to query from it to get all of the names of restaurants. I'm new to RavenDB and can't seem to find any documentation on how to query from a folder that sits on the C drive of the computer. Does anyone have any idea on how to do this?
If the database was created in drive C at creation time, then RavenDB knows where it is located.
Run your RavenDB server.
Specify your localhost URL when initializing the store:
var store = new DocumentStore
{
Urls = new[] {"http://127.0.0.1:8080"},
Database = "your-database-name"
};
store.Initialize();
After that, you just query your database.
If you manually moved the database from its original created location to some other folder then you couldn't query.
You can Export the database and Import it to a new database in a new location.
You can point RavenDB server to a specific data directory - in this way you can load your database and query it.
Simply edit settings.json in the folder where you find Raven.Server.exe and write the desired data folder in DataDir property. In the end, your settings file would look similar to this:
{
"DataDir": "D:\\foo\\bar",
/* other settings... */
}
Note, if you run RavenDB in Docker, you can adjust the configuration using environment variables. (more about this here: https://ravendb.net/docs/article-page/4.2/csharp/start/installation/running-in-docker-container)

Have Sql SMO Delete Database Backup File

I have create a C# application that allows a database to be backed up and restored. It does this by first backing the database up to a local file on the Sql server using:
Backup backup = new Backup();
backup.Devices.AddDevice(Path.GetFullPath(backupFilePath), DeviceType.File);
...
backup.SqlBackup(server);
And then I create the new database by restoring from the backup file using:
Restore restore = new Restore();
restore.Devices.AddDevice(Path.GetFullPath(backupFileToRestoreFrom), DeviceType.File);
...
restore.SqlRestore(server);
After the new database has been created I want to delete the temp backup file that we created. Because I have admin rights on the Sql server box, I can delete the file on the remote server using:
File.Delete("\\SqlServer\C$\Backups\BackupFileToDelete.bak")
and it works. However, if somebody else who doesn't have rights on the Sql server box runs the app, it will throw an exception about not having permissions.
So is there a Sql SMO function that I can call to delete the backup file that was created on the remote Sql server?
If you delete the file using the xp_cmdshell then it will use the rights of the sql server instead of the rights of the person.
For example:
EXEC master..xp_cmdshell 'Del \\SqlServer\C$\Backups\BackupFileToDelete.bak', NO_OUTPUT
Here's the reference on xp_cmdshell
The important thing to note from that article is:
Because malicious users sometimes attempt to elevate their privileges
by using xp_cmdshell, xp_cmdshell is disabled by default. Use
sp_configure or Policy Based Management to enable it. For more
information, see xp_cmdshell Server Configuration Option.
Another possibility is to use SQLCLR, which is more secure than using xp_cmdshell. There's even a CodePlex project call SQLCLR File Functions that has the functionality written as stored procs for me. I think I'm going to see if I can convince our DBAs to go this route. The downside is that I believe the sprocs that this creates would need to be installed on every existing and new SQL Server that gets created; not much different than having to enable xp_cmdshell on every server though I guess.

Showing error while back up the database in sql server

I am facing some error while i am trying to back up my database in sql server R2.
it is showing the following error:
TITLE: Microsoft SQL Server Management Studio
Restore failed for Server 'Ironhide\SQLEXPRESS'. (Microsoft.SqlServer.SmoExtended)
ADDITIONAL INFORMATION:
System.Data.SqlClient.SqlError: The media set has 2 media families but only 1 are provided. All members must be provided. (Microsoft.SqlServer.Smo)
This i commonly a problem with how the database backup was taken, it looks like when the backup was created it was split into two files, you wont be able to restore it unless you have both those files. Try creating the backup again but when you do the backup insure that you only have one file in the destination section.
This question has been already asked in SO: Any other solutions for SQL's “The media set has 2 media families but only 1 are provided. All members must be provided.” error?
Anyway, your backup set was splitted in two files, but you're trying to restore the data from just only one backup file.
One solution is to backup your data in just one file and repeat the restore process. The other one is to provide both backup files and try to restore again the data. In this way, SQL Server won't warn you again with that error.
The Microsoft Project Manager for SQL Backup explains here the error.

Copying a local database from one computer to another

I'm very new to SQL and made a small program where a user can input some data, click submit and the data is then stored in a table in the database.
I know want to move the application onto a friends computer, which i'm assuming has no SQL software installed, what would be the easiest way to do this, when obviously the connection string is unique to my computer and the database is stored on my computer.
You will have to install SQL Server on their machine first and foremost. Once this is done, you can obtain a relevant connection string. Note, for the 'Server name' part of the connection string, if you are using SQL Express, instead of using 'localhost', or the name of the server instance (i.e. 'MyMachine'), you would use 'localhost\SQLEXPRESS'/'MyMachine\SQLEXPRESS'.
After setting up the SQL Server instance on the new machine, to copy the required database, first detach the database to avoid any corruption. Now you are free to merely copy the file from your machine to theirs and go through the usual attachment process using SQL Server Management Studio (SQLMS).
I hope this helps.
You can use SQL CE or other file databases. On this way you need to install SQL CE(you can include SQl CE installer into your program installer) on target computer and after that you can easy copy db-file from you computer to target computer.
Also, you can use relative path to db-file from your exe file instead of fixed connection string:
string dbDirPath=Path.Combine(AppDomain.CurrentDomain.BaseDirectory,"DB" );
private const string CONN_STR_TEMPLATE = "Data Source={0};Persist Security Info=False;";
string dbFilePath = Path.Combine(dbDirPath, "my.sdf");
_connStr =String.Format(CONN_STR_TEMPLATE,dbFilePath);
You cannot simply copy your database since SQL Server database is NOT a standalone database as SQL Compact edition/MS Access.
You may configure your router to remote access SQL Server instance over the Internet by forwarding the port
Accessing SQL Server Instance through NAT

connect to .mdf file in vs2010 , wpf application

I am using VS2010 , and I built a .mdf file using SQL server 2008
I want to use this database file in my wpf application so that I can add rows to it and delete rows from it
the problem is , I can't access this file , and all the insertion and deleting is actually hapening to the datacontexct i created .
I used myDataContexct.Submitchanges() but it didn't work either
I tried to add a connection string when I define the datacontexct that holds the url of my .mdf file and this it gave me a runtime error when trying to access this file and the error messege says :
An attempt to attach an auto-named database for file Trial.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.
please help me because I searched alot but I couldn't find any help
If the application is not going to be installed in a manner that many clients are accessing the same server, you would want to consider using SQL Server Compact Edition.
Are you sure the connection string in your app.config refers to the local mdf file? perhaps it refers to the server instance?
What technology do you use, is it LinqToSql or Entity Framework (I think you have t call SaveChanges, not AcceptChanges)?
If you do intend to access the server instance, then the problem seems to be a security restriction.
Please add more details on statement no. 1, and I'll write further info.

Categories

Resources