In my unit tests, I'm using a SQL Server LocalDB database. You could be nit picky and say that because of that fact it's not unit tests but integration tests and you would be right, but the point is that I am using the MSTest Framework to run those tests. Every test is copying an existing database and running their one test on this database.
private NAMETestSystem([CallerMemberName] string testCase = null)
{
this.destinationDirectory = Path.Combine(Directory.GetCurrentDirectory(), testCase ?? "Undefined_" + Guid.NewGuid().ToString("N"));
var connectionString = $"Data Source=(LocalDB)\\MSSQLLocalDB; Integrated Security = True; AttachDbFilename ={Path.Combine(this.destinationDirectory, "NAMEIntegrationTest.mdf")}";
var entityFrameworkData = $"metadata=res://*/NAME.csdl|res://*/NAME.ssdl|res://*/NAME.msl;provider=System.Data.SqlClient;provider connection string=\"{connectionString}\"";
// [...]
Copy(SourceDirectory, this.destinationDirectory);
My "problem" is that each of those copies pops up in my SQL Server management studio. All 100+ or them. I don't need them there. They don't exist anymore. And to make things worse, you cannot batch-detach... I have to press Del+Enter about 150 times just to clear that window up.
Is there a way to not have my temporary local db instances appear in my SQL server management studio?
Maybe special way to close or dispose, something in the connection string I can set? Or maybe a way to detach all of them at the same time in management studio?
So in the end, what I did and what is working fine for now is this:
public void Dispose()
{
// disposing other stuff
// sql db
if (Directory.Exists(this.destinationDirectory))
{
DetachDatabase(this.connectionString);
Directory.Delete(this.destinationDirectory, true);
}
}
public static void DetachDatabase(string connectionString)
{
using (var connection = new SqlConnection(connectionString))
{
connection.Open();
using (var command = connection.CreateCommand())
{
var sql = "DECLARE #dbName NVARCHAR(260) = QUOTENAME(DB_NAME());\n" +
"EXEC('ALTER DATABASE ' + #dbName + ' SET OFFLINE WITH ROLLBACK IMMEDIATE;');\n" +
"EXEC('exec sp_detach_db ' + #dbName + ';');";
command.CommandText = sql;
command.ExecuteNonQuery();
}
}
}
Might not be the prettiest solution, but at least it keeps my sanity intact while the number of tests (and number of databases) rises.
Related
This question already has answers here:
Backup Permissions
(5 answers)
Closed 1 year ago.
I'm trying to back up my database using this C# code How to backup and restore SQL Server in WPF with C# and Entity Framework
private static void CreateBackup(string databaseName, string backupFilePath)
{
GlobalConfig gb = new GlobalConfig();
string connectionString = gb.GetConnectionString();
backupFilePath = backupFilePath + "\\" + databaseName + ".bak";
backupFilePath = #""+backupFilePath;
var backupCommand = "BACKUP DATABASE #databaseName TO DISK = #backupFilePath";
using (var conn = new SqlConnection(connectionString))
using (var cmd = new SqlCommand(backupCommand, conn))
{
conn.Open();
cmd.Parameters.AddWithValue("#databaseName", databaseName);
cmd.Parameters.AddWithValue("#backupFilePath", backupFilePath);
cmd.ExecuteNonQuery();
}
}
CreateBackup("Test","C:\Desktop\Backup\\Test.bak");
But I got this error :
Cannot open backup device 'C:\Desktop\Backup\Test.bak'. Operating system error 5(Access is denied.).
What I'm doing wrong with this code?
How can I fix this error?
The SQL Server process typically does not run with the permissions of the currently logged in user, therefore it cannot access the users desktop (nor most of the folders of the user or any network folders). It is not possible to freely choose the folder for the backup.
Your best solution is to export to a folder where the server process has access to (i.e. the system temp folder) and then copy the backup from there to wherever you want it.
private static void CreateBackup(string databaseName, string backupFilePath)
{
GlobalConfig gb = new GlobalConfig();
string connectionString = gb.GetConnectionString();
// Create the backup in the temp directory (the server should have access there)
var backup = Path.Combine(Path.GetTempPath(), "TemporaryBackup.bak");
var backupCommand = "BACKUP DATABASE #databaseName TO DISK = #backup";
using (var conn = new SqlConnection(connectionString))
using (var cmd = new SqlCommand(backupCommand, conn))
{
conn.Open();
cmd.Parameters.AddWithValue("#databaseName", databaseName);
cmd.Parameters.AddWithValue("#backup", backup);
cmd.ExecuteNonQuery();
}
File.Copy(backup, backupFilePath); // Copy file to final location
}
I have successfully backed up SQL Server databases using Microsoft.SqlServer.Management.Smo.Backup, might want to try that. Mine was in VB years ago but it is still working today. Here is the VB code if it helps:
Dim mySourceServer As New Server(My.Settings.SQLInstance)
Dim bkpDBFullWithCompression As New Backup()
' Specify whether you want to back up database or files or log
Me.Cursor = Cursors.WaitCursor()
bkpDBFullWithCompression.Action = BackupActionType.Database
' Specify the name of the database to back up
bkpDBFullWithCompression.Database = _sBackupDatabaseName
bkpDBFullWithCompression.CompressionOption = BackupCompressionOptions.[On]
bkpDBFullWithCompression.Devices.AddDevice(_sBackupFilePath, DeviceType.File)
bkpDBFullWithCompression.BackupSetName = _sBackupDatabaseName + " database Backup - Compressed"
bkpDBFullWithCompression.BackupSetDescription = _sBackupDatabaseName + " database - Full Backup"
Try
bkpDBFullWithCompression.SqlBackup(mySourceServer)
Catch ex As SmoException
blSuccess = False '
Me.Cursor = Cursors.Default
End Try
This solution ( Check Local System account instead of This account ) worked for me, but I didn't have any idea if it's a good solution for security or no.
You can find the LogOn tab setting under this :
Services -> SQL Server -> Properties -> Log on
I have an application which uses EF and LocalDB as it's database, published by ClickOnce.
it's my first time using LocalDB and I don't know how can i add a feature to my application to Backup/Restore The Database Programmatically.
My App Path Installed by ClickOnce :
C:\Users\Mahdi Rashidi\AppData\Local\Apps\2.0\NOL11TLW.9XG\CZM702AQ.LPP\basu..tion_939730333fb6fcc8_0001.0002_fd707bbb3c97f8d3
and This is the location which Database files Installed :
C:\Users\Mahdi Rashidi\AppData\Local\Apps\2.0\NOL11TLW.9XG\CZM702AQ.LPP\basu...exe_939730333fb6fcc8_0001.0002_none_8c555c3966727e7f
How Should I Backup/Restore the Database?
How Can I Keep Database Safe from ClickOnce further Updates?
Thanks alot :)
This is what I did for backup and restore of my localDb
public void BackupDatabase(string filePath)
{
using (TVend2014Entities dbEntities = new TVend2014Entities(BaseData.ConnectionString))
{
string backupQuery = #"BACKUP DATABASE ""{0}"" TO DISK = N'{1}'";
backupQuery = string.Format(backupQuery, "full databsase file path like C:\tempDb.mdf", filePath);
dbEntities.Database.SqlQuery<object>(backupQuery).ToList().FirstOrDefault();
}
}
public void RestoreDatabase(string filePath)
{
using (TVend2014Entities dbEntities = new TVend2014Entities(BaseData.ConnectionString))
{
string restoreQuery = #"USE [Master];
ALTER DATABASE ""{0}"" SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
RESTORE DATABASE ""{0}"" FROM DISK='{1}' WITH REPLACE;
ALTER DATABASE ""{0}"" SET MULTI_USER;";
restoreQuery = string.Format(restoreQuery, "full db file path", filePath);
var list = dbEntities.Database.SqlQuery<object>(restoreQuery).ToList();
var resut = list.FirstOrDefault();
}
}
Hope this is what you want.
I had a bugger of a time getting my backup/restore to work from code in my application. I'm using LOCALDB and wanted to make sure that regardless of the state of the database or the location of the .mdf file that the backup and restore functions would work. After all - the DBMS should take care of that for you. In the end this is how I got my backup and restore functions to work:
Note: code in VB - save the ";" :)
Backup:
Dim cbdfilename As String = controlPath & "\Backup\Temp\cbdb.bak"
Dim connString As String = (server + ";Initial Catalog=master;Integrated Security=True;")
Dim conn As New SqlConnection(connString)
Dim sql As String
sql = "Backup database #DBNAME " _
& " to Disk = #FILENAME" _
& " with Format"
SqlConnection.ClearAllPools()
'execute backup
Dim dbcmd As New SqlCommand(sql, conn)
dbcmd.Parameters.AddWithValue("#DBNAME", database)
dbcmd.Parameters.AddWithValue("#FILENAME", cbdfilename)
conn.Open()
Try
dbcmd.ExecuteNonQuery()
Catch ex As Exception
MsgBox("Backup DB failed" + ex.ToString)
Finally
conn.Close()
conn.Dispose()
End Try
A key thing to note above is the SqlConnection.ClearAllPools() statement. Even though I was sure that all connections had been properly closed and disposed of in other parts of my app - somehow the DBMS was still showing an open thread.
And now the Restore:
SqlConnection.ClearAllPools()
Dim connString As String = (server + ";Initial Catalog=master;Integrated Security=True;")
Dim conn As New SqlConnection(connString)
Dim sql As String
sql = "Use master;" _
& "Alter Database " & database & " Set Single_User With Rollback Immediate;" _
& "Restore Database " & database & " From Disk = #FILENAME" _
& " With Replace;" _
& "Alter Database " & database & " Set Multi_User;"
'execute restore
Dim dbcmd As New SqlCommand(sql, conn)
dbcmd.Parameters.AddWithValue("#FILENAME", cbdfilename)
conn.Open()
Try
dbcmd.ExecuteNonQuery()
Catch ex As Exception
MsgBox("Restore DB failed" + ex.ToString)
Finally
conn.Close()
conn.Dispose()
End Try
What was really weird in the SQL above is that I initially tried to use #Parms for the database name but the ALTER statements would not accept them. Kept kicking back with exceptions.
The biggest difference between my restore and the one from the earlier solution is that I only use the database name ie. "MyDB_TEST" and not the .mdf file name in my Alter and Restore statements.
I am writing a simple database backup and restore routine for an application. I can backup my database without issues, however when I restore is I am unable to gain exclusive access to my database.
I am trying all the combinations of fixes on SO, putting in single user mode, taking it offline then placing it back only with no success.
I can successfully restore the database within studio manager (express)
This method is the only connection to the SQL server at the time, so I don't understand why I can't perform the restore.
Appreciate the help to point out where the issue may be.
internal void RestoreDatabase(string databaseFile)
{
//get database details
var databaseConfiguration = new DatabaseConfiguration().GetDatabaseConfiguration();
try
{
//construct server connection string
var connection = databaseConfiguration.IsSqlAuthentication
? new ServerConnection(databaseConfiguration.ServerInstance,
databaseConfiguration.SqlUsername,
databaseConfiguration.SqlPassword)
: new ServerConnection(databaseConfiguration.ServerInstance);
//set database to single user and kick everyone off
using (
var sqlconnection =
new SqlConnection(new DatabaseConfiguration().MakeConnectionString(databaseConfiguration)))
{
sqlconnection.Open();
using (
var sqlcommand = new SqlCommand("ALTER DATABASE " + databaseConfiguration.DatabaseName + " SET Single_User WITH Rollback IMMEDIATE",
sqlconnection))
{
sqlcommand.ExecuteNonQuery();
}
using (
var sqlcommand = new SqlCommand("ALTER DATABASE " + databaseConfiguration.DatabaseName + " SET OFFLINE",
sqlconnection))
{
sqlcommand.ExecuteNonQuery();
}
using (
var sqlcommand = new SqlCommand("ALTER DATABASE " + databaseConfiguration.DatabaseName + " SET ONLINE",
sqlconnection))
{
sqlcommand.ExecuteNonQuery();
}
sqlconnection.Close();
}
//setup server connection and restore
var server = new Server(connection);
var restore = new Restore();
restore.Database = databaseConfiguration.DatabaseName;
restore.Action = RestoreActionType.Database;
restore.Devices.AddDevice(databaseFile, DeviceType.File);
restore.ReplaceDatabase = true;
restore.Complete += Restore_Complete;
restore.SqlRestore(server);
}
catch (Exception ex)
{
//my bad
restoreDatabaseServerError(ex.InnerException.Message, EventArgs.Empty);
}
finally
{
//set database to multi user
using (
var sqlconnection =
new SqlConnection(new DatabaseConfiguration().MakeConnectionString(databaseConfiguration)))
{
sqlconnection.Open();
using (
var sqlcommand = new SqlCommand("ALTER DATABASE " + databaseConfiguration.DatabaseName + " SET Multi_User",
sqlconnection))
{
sqlcommand.ExecuteNonQuery();
sqlcommand.Dispose();
}
sqlconnection.Close();
}
}
}
If anybody is connected to your database, SQL Server cannot drop it, so you have to disconnect existing connections, as you have tried. The problem with single_user is, that it still allows a single user to connect. As you yourself cannot be connected to the database when dropping it you have to get out of there. That opens up that slot for someone else to connect and in turn prevent you from dropping it.
There are a few SQL Server processes that are particularly good at connecting to a database in that split second. Replication is one example. (You shouldn't really drop a database that is published anyway, bat that is another story.)
So what can we do about this? The only 100% safe way is to prevent users from connecting to the database. The only practical way is to switch the database offline and then drop it. However, that has the nasty side effect, that SQL Server does not delete the files of that database, so you have to do that manually.
Another option is to just be fast enough. In your example you bring the database back online before you drop it. That is a fairly resource intensive process that gives an "intruder" lots of time to connect.
The solution I have been using with success looks like this:
ALTER DATABASE MyDb SET RESTRICTED_USER WITH ROLLBACK IMMEDIATE;
USE MyDb;
ALTER DATABASE MyDb SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
USE tempdb;
DROP DATABASE MyDb;
This first sets the database to restricted user and connects to it. Then, while still connected it sets the database to single user. Afterwards the context is switched to tempdb and the drop is executed immediately thereafter. Important here is, to send these commands as one batch to SQL Server to minimize the time between the USE tempdb; and the DROP. Setting the database to restricted user in the beginning catches some rare edge cases, so leave it in even though it does not make sense at first glance.
While this still leaves a theoretical gap for someone else to get in, I have never seen it fail.
After the database is dropped you can run your restore as normal.
Good luck.
Your restore needs to take place on the same connection you set the DB server to single user mode.
In summary for the changes below, I moved the end of the using to below your restore code, and moved the close for the SQL connection to after the restore so it uses the same connection. Also removed set offline and online since they aren't needed. Can't test at the moment, so let me know if it works.
//set database to single user and kick everyone off
using (var sqlconnection = new SqlConnection(new DatabaseConfiguration().MakeConnectionString(databaseConfiguration)))
{
sqlconnection.Open();
using (var sqlcommand = new SqlCommand("ALTER DATABASE " + databaseConfiguration.DatabaseName + " SET Single_User WITH Rollback IMMEDIATE",sqlconnection))
{
sqlcommand.ExecuteNonQuery();
}
//setup server connection and restore
var server = new Server(sqlconnection);
var restore = new Restore();
restore.Database = databaseConfiguration.DatabaseName;
restore.Action = RestoreActionType.Database;
restore.Devices.AddDevice(databaseFile, DeviceType.File);
restore.ReplaceDatabase = true;
restore.Complete += Restore_Complete;
restore.SqlRestore(server);
sqlconnection.Close();
}
I'm currently learning ADO.NET on C#. I'm learning by a book and tutorials that I found online. I wanted to try some of the samples to get myself familiarized with the whole SQL connnection and command objects and so on. Hence, I tried this:
namespace ConsoleApplication
{
class SqlDemo
{
public void InitConnection ()
{
string connString = #"data source=C:\SQL Server 2000 Sample Databases; database=northwnd; integrated security=SSPI";
SqlConnection conn = null;
try
{
conn = new SqlConnection (connString);
conn.Open ();
Console.WriteLine ("DataBase connection established");
}
catch
{
Console.WriteLine ("DataBase connection not established");
}
finally
{
if (conn != null) conn.Close ();
}
Console.ReadKey (true);
}
static void Main (string[] args)
{
SqlDemo d = new SqlDemo ();
d.InitConnection ();
}
}
}
And no matter how I try, I can connect to the local database. "data source=(local)" don't work.
A couple of things:
1) It looks like you may have a typo in your database name. It should probably be:
database=northwind
2) Your data source should be (local) or . OR you may have an instance installed, in which case you may need to include the instance name as well, such as .\SQLExpress or .\SQLServer.
If you wish to connect to a database file using a path:
Server=.\SQLExpress;AttachDbFilename=|DataDirectory|mydbfile.mdf; Database=dbname;Trusted_Connection=Yes;
From: http://www.connectionstrings.com/sql-server-2008
However, you may also need to "Attach" the database to Sql Server. In Management studio, right click the Databases folder and select "Attach..."
If you are using SQL Server 2000, then just put 'local' or simply '.' (exclude the quotes) for the data source. And you have a typo in the database name. It should be 'Northwind'
I got some data inputed by the user that should be added to a Database File (.sdf). I've choose Sql Server CE because this application is quite small, and i didn't saw need to work with a service based database.
Well any way.
Here goes the code:
public class SqlActions
{
string conStr = String.Format("Data Source = " + new System.IO.FileInfo(System.Reflection.Assembly.GetExecutingAssembly().Location).DirectoryName + "\\basedados.sdf");
public SqlCeConnection SQLCEConnect()
{
SqlCeConnection Connection = new SqlCeConnection(conStr);
Connection.Open();
return Connection;
}
public Boolean AdicionarAuditorio(string Nome, int Capacidade)
{
string Query = "INSERT INTO auditorios (nome, capacidade) VALUES (#Nome, #Capacidade)";
using (var SQLCmd = new SqlCeCommand(Query, SQLCEConnect()))
{
SQLCmd.Parameters.AddWithValue("#Nome", Nome);
SQLCmd.Parameters.AddWithValue("#Capacidade", Capacidade);
if (SQLCmd.ExecuteNonQuery() == 1)
{
return true;
} else {
return false;
}
}
}
}
I use the AdicionarAuditorio(string Nome, int Capacidade) function to Insert the data. running ExecuteNonQuery() which is supposed to return the number of affected rows after he as run the query.
So it should return 1 if the query as successful, right?
In the end he returns 1, but if I browser the table data, the data that the query should add isn't there.
So whats wrong here?
NOTE. If your thinking that the
problem is the connection: I can't see
why is the problem once i got some
Select statements that use that
connection function SQLCEConnect()
and they all work pretty well.
Thanks in advance.
Are you sure you are looking at the right file? When you build your app in VS, it copies the SDF file as content to the target folder, so the database in your project will not reflect any updates. Your code is picking up the the file location there.
This is btw not a good practice, because once deployed, the program folders are not writable to your app (could this be the problem - did you already deploy?). Instead, the database file should reside in your appdata folder.
Is it possible that you make the call to AdicionarAuditorio in a TransactionScope without calling transactionScope.Complete()?