Click here to Skip to main content
15,893,663 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello to everyone.
please help me.
I have a windows application with c# and sql Express 2008.
In my application,I have database backup and restore function.
my backup function execute without any error,but my restore function work on my computer and don't work on end user system.
error on end user system:

Cannot open database "C:\EasyFactorDB.mdf" requested by the login. The login failed. Login failed for user 'REDAPPLE6\redapple'

my connection string :
Data Source=.\\SQLEXPRESS;Initial Catalog=C:\EasyFactorDB.mdf;Integrated Security=True;User Instance=True

my restore function:

public bool RestoreDatabase(string BackUpFolderName, string BackUpFileName, string DestinitionPath)
{
SqlConnection sc = new SqlConnection("Data Source=.\\SQLEXPRESS;Initial Catalog=" + DestinitionPath + ";Integrated Security=True;User Instance=True");
sc.Open();

Server myServer = new Server(sc.DataSource);
myServer.ConnectionContext.LoginSecure = true;
myServer.ConnectionContext.MultipleActiveResultSets = true;
myServer.ConnectionContext.Connect();
Database myDatabase = myServer.Databases[sc.Database];
Restore restoreDB = new Restore();
restoreDB.Action = RestoreActionType.Database;
restoreDB.Database = myDatabase.Name;

string FilePath = BackUpFolderName + Path.DirectorySeparatorChar + BackUpFileName;

if (!System.IO.File.Exists(FilePath))
{
return false;
}

myServer.KillAllProcesses(sc.Database);

restoreDB.Devices.AddDevice(FilePath, DeviceType.File);

if (!restoreDB.SqlVerify(myServer))
{
return false;
}

restoreDB.SqlRestore(myServer);

if (myServer.ConnectionContext.IsOpen)
{
myServer.ConnectionContext.Disconnect();
}

sc.Close();

return true;
}
Posted

 
Share this answer
 
Comments
majidz 5-Oct-15 3:13am    
thanks's , but this is not my solution.
I'm using c# code for create database backup and restore it.
create database backup work very well,with a connection string same as backup connection string.
The first thing to notice is that you are using the root directory of the boot device, and access to files in that folder has been restricted since Vista and is likely to become even more restricted.

This is possibly a permissions issue - and the best way to get round that is to store your DB in a more "friendly" location. This can help: Where should I store my data?[^]

Another possibility is that the client system does not use a local SQL instance, but one on his LAN - in which case the SQL instance won't be able to access the DB if it's not on the actual server that SQL is installed on - the local hard drive root folder will not be accessible at all!

The third possibility is that the login is just plain wrong: production SQL server instances are almost never configured to use integrated security - it's a major security risk because any user can do anything to the DB! Probably you need yo use a specific user / password combination instead.
 
Share this answer
 
Comments
majidz 5-Oct-15 3:22am    
Cannot open database "C:\Documents and Settings\redapple\Desktop\EasyFactorDB.mdf" requested by the login. The login failed.
Login failed for user 'REDAPPLE6\redapple'. Net SqlClient Data Provider.

then, your first and second solution are not my solution.
but your third solution isn't my solution,because all Section of my application work well,for example : insert record to table,edit a record,select and ...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900