Restore and Backup with Entity Framework - c#

I use C#, .net 4, Entity Framework and SQL Server 2008 R2 in a project.
I have no familiarity with backup and restore from database by Entity Framework. Please help me to write restore and backup code in Entity Framework

Entity Framework is an ORM - object-relational mapper - designed to handle interactions with single entities and/or short lists of entities. It's neither designed for bulk operations, nor is it a server admin framework. So no - I don't think you can do this using Entity Framework - that's not its job.
Use an appropriate tool for the job! Either use SQL Server Management Studio to handle backup/restore - or if you must do it programmatically, use the SMO (Server Management Objects) which is intended for exactly these kinds of jobs

To other friends who have this problem ....
Useing ExecuteSqlCommand can backup of db in EF 6+ .
For example : (this code create backup of your DB , I had tested this.)
string dbname = db.Database.Connection.Database;
string sqlCommand = #"BACKUP DATABASE [{0}] TO DISK = N'{1}' WITH NOFORMAT, NOINIT, NAME = N'MyAir-Full Database Backup', SKIP, NOREWIND, NOUNLOAD, STATS = 10";
db.Database.ExecuteSqlCommand(System.Data.Entity.TransactionalBehavior.DoNotEnsureTransaction, string.Format(sqlCommand,dbname, "Amin9999999999999"));
backup saved in C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\Backup
ref=> https://entityframework.codeplex.com/discussions/454994
but I do not recommend for working with this method!
I strongly recommend the use of the article below:
http://www.c-sharpcorner.com/Blogs/8679/backup-and-restore-the-database-in-Asp-Net-web-application.aspx

This should get you going on the restore side:
void LoadDB(
System.Data.Entity.DbContext context,
string backup_filename,
string orig_mdf, // the original LogicalName name of the data (also called the MDF) file within the backup file
string orig_ldf, // the original LogicalName name of the log (also called the LDF) file within the backup file
string new_database_name
)
{
var database_dir = System.IO.Path.GetTempPath();
var temp_mdf = $"{database_dir}{new_database_name}.mdf";
var temp_ldf = $"{database_dir}{new_database_name}.ldf";
var query = #"RESTORE DATABASE #new_database_name FROM DISK = #backup_filename
WITH MOVE #orig_mdf TO #temp_mdf,
MOVE #orig_ldf TO #temp_ldf,
REPLACE;";
context.Database.ExecuteSqlCommand(
// Do not use a transaction for this query so we can load without getting an exception:
// "cannot perform a backup or restore operation within a transaction"
TransactionalBehavior.DoNotEnsureTransaction,
query,
new[] {
new SqlParameter("#backup_filename", backup_filename),
new SqlParameter("#database_dir", database_dir),
new SqlParameter("#new_database_name", new_database_name),
new SqlParameter("#orig_mdf", orig_mdf),
new SqlParameter("#orig_ldf", orig_ldf),
new SqlParameter("#temp_mdf", temp_mdf),
new SqlParameter("#temp_ldf", temp_ldf),
}
);
}
If you don't know them beforehand, the MDF and LDF LogicalName values can be obtained manually or programmatically from a query like this one:
RESTORE FILELISTONLY FROM DISK #backup_filename

Related

How to create the SQlite database when the application is deployed/installed?

I am new to c#/.NET and developing an application with SQlite datbase and entity framework and it is all working fine during testing. I am specifying the below for the filepath to the .db file.
var path = AppDomain.CurrentDomain.BaseDirectory;
On deployment I need the application to create the database fresh, that doesn't contain the test data I was using during development. So I cannot simply copy the current .db file. Can anyone help with code that checks existence of database, and if not it will create new?
You can use this method:
private static void CreateFreshDb(string dbFilePath)
{
// Create the DB file
SQLiteConnection.CreateFile(dbFilePath);
// Create a table
var connectionString = $"Data Source={dbFilePath};Version=3;";
using (var connection = new SQLiteConnection(connectionString))
{
connection.Open();
string creationScript = "Create Table Person (Id int, Name varchar(20))";
SQLiteCommand command = new SQLiteCommand(creationScript, connection);
command.ExecuteNonQuery();
command.Dispose();
}
}
And call it like that:
string dbFilePath = "C:\\Db\\Database.db";
if (!File.Exists(dbFilePath))
{
CreateFreshDb(dbFilePath);
}
Note that I use the System.Data.SQLite library here.
An appliation that creates databases should be in principle a script that creates databases. If you application has its own business line which requires a database, but it is not intended to create a database at first, better you create it manually outside the code and then simply let the code choose between which database to use between testing and production, via appSettings or similar solution.

Synchronize two SQL Server database using Dotmim sync framework

I have a WinForms database driven application that I want to make it work in online/offline mode using Dotmim sync framework that I find an article by their author here.
The documentation for the library is here
this is my code to sync the two SQL Server databases one is localdb and the other one is now on the SQL Server Management Studio for the testing purpose:
string connect = #"Data Source=(LocalDb)\MSSQLLocalDB;Initial Catalog=bright_square_db;Integrated Security=SSPI;AttachDBFilename=D:\Folder\project_file\bright_square_db.mdf";
string constring = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
SqlSyncProvider serverProvider = new SqlSyncProvider(constring);
SqlSyncProvider clientProvider = new SqlSyncProvider(connect);
SyncAgent agent = new SyncAgent(clientProvider, serverProvider, new string[] { "I have listed all the tables here" });
var progress = new SynchronousProgress<ProgressArgs>(s => MessageBox.Show($"{s.Context.SyncStage}:\t{s.Message}"));
var syncContext = await agent.SynchronizeAsync(progress);
MessageBox.Show(syncContext.ToString());
But, when I try to run the code. I am getting this error
The columns that indicated in the error are for a table that created by the sync process named "scope_info" inside the SQL Server database.
I have solved the problem by swapping the client and server connection string link in the third and fourth line of the above code. I don't know what exactly cause the problem, but lastly this changed makes the code work for me.

SQL Server CE two way sync with remote Access database

I'm working on a pretty special, legacy project where I need to build an app for PDA devices under Windows Mobile 6.5. The devices have a local database (SQL Server CE) which we are supposed to sync with a remote database (Microsoft Access) whenever they are docked and have network access.
So the local database using SQL Server CE works fine, but I can’t figure out a way to sync it to the Access database properly.
I read that ODBC and OLEDB are unsupported under Windows Mobile 6.5, most ressources I find are obsolete or have empty links, and the only way I found was to export the local database relevant tables in XML in the hope to build a VBA component for Access to import them properly. (and figure out backwards sync).
Update on the project and new questions
First of all, thanks to everyone who provided an useful answer, and to #josef who saved me a lot of time with the auto path on this thread.
So a remote SQL Server is a no go for security reasons (client is paranoid about security and won't provide me a server). So I'm tied to SQL Server CE on the PDA and Access on the computer.
As for the sync:
The exportation is fine: I'm using multiple dataAdapters and a WriteXML method to generate XML files transmitted by FTP when the device is plugged back in. Those files are then automatically imported into the Access database. (see code at the end).
My problem is on the importation: I can acquire data through XML readers from an Access-generated file. This data is then inserted in a dataset (In fact, I can even print the data on the PDA screen) but I can't figure out a way to do an "UPSERT" on the PDA's database. So I need a creative way to update/insert the data to the tables if they already contains data with the same id.
I tried two methods, with SQL errors (from what I understood it's SQL Server CE doesn't handle stored procedures or T-SQL). Example with a simple query that is supposed to update the "available" flag of some storage spots:
try
{
SqlCeDataAdapter dataAdapter = new SqlCeDataAdapter();
DataSet xmlDataSet = new DataSet();
xmlDataSet.ReadXml(localPath +#"\import.xml");
dataGrid1.DataSource = xmlDataSet.Tables[1];
_conn.Open();
int i = 0;
for (i = 0; i <= xmlDataSet.Tables[1].Rows.Count - 1; i++)
{
spot = xmlDataSet.Tables[1].Rows[i].ItemArray[0].ToString();
is_available = Convert.ToBoolean(xmlDataSet.Tables[1].Rows[i].ItemArray[1]);
SqlCeCommand importSpotCmd = new SqlCeCommand(#"
IF EXISTS (SELECT spot FROM spots WHERE spot=#spot)
BEGIN
UPDATE spots SET available=#available
END
ELSE
BEGIN
INSERT INTO spots(spot, available)
VALUES(#spot, #available)
END", _conn);
importSpotCmd.Parameters.Add("#spot", spot);
importSpotCmd.Parameters.Add("#available", is_available);
dataAdapter.InsertCommand = importSpotCmd;
dataAdapter.InsertCommand.ExecuteNonQuery();
}
_conn.Close();
}
catch (SqlCeException sql_ex)
{
MessageBox.Show("SQL database error: " + sql_ex.Message);
}
I also tried this query, same problem SQL server ce apparently don't handle ON DUPLICATE KEY (I think it's MySQL specific).
INSERT INTO spots (spot, available)
VALUES(#spot, #available)
ON DUPLICATE KEY UPDATE spots SET available=#available
The code of the export method, fixed so it works fine but still relevant for anybody who wants to know:
private void exportBtn_Click(object sender, EventArgs e)
{
const string sqlQuery = "SELECT * FROM storage";
const string sqlQuery2 = "SELECT * FROM spots";
string autoPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase); //get the current execution directory
using (SqlCeConnection _conn = new SqlCeConnection(_connString))
{
try
{
SqlCeDataAdapter dataAdapter1 = new SqlCeDataAdapter(sqlQuery, _conn);
SqlCeDataAdapter dataAdapter2 = new SqlCeDataAdapter(sqlQuery2, _conn);
_conn.Open();
DataSet ds = new DataSet("SQLExport");
dataAdapter1.Fill(ds, "stock");
dataAdapter2.Fill(ds, "spots");
ds.WriteXml(autoPath + #"\export.xml");
}
catch (SqlCeException sql_ex)
{
MessageBox.Show("SQL database error: " + sql_ex.Message);
}
}
}
As Access is more or less a stand-alone DB solution I strongly recommend to go with a full flavored SQL Server plus IIS to setup a Merge Replication synchronisation between the SQL CE data and the SQL Server data.
This is described with full sample code and setup in the book "Programming the .Net Compact Framework" by Paul Yao and David Durant (chapter 8, Synchronizing Mobile Data).
For a working sync, all changes to defined tables and data on the server and the CE device must be tracked (done via GUIDs, unique numbers) with there timestamps and a conflict handling has to be defined.
If the data is never changed by other means on the server, you may simply track Device side changes only and then push them to the Access database. This could be done by another app that does Buld Updates like described here.
If you do not want to go the expensive way to SQL Server, there are cheaper solutions with free SQLite (available for CE and Compact Framework too) and a commercial Sync tool for SQLite to MSAccess like DBSync.
If you are experienced, you may create your own SQLite to MS ACCESS sync tool.

Programmatically Build Access Query

A Microsoft Office Access database contains Tables, Queries, Forms and Reports.
Is it possible to build and save a query in the Access database from C#?
For example, I know I can use OLEDB to connect to an Access database and use SQL commands using its tables and already defined queries, but how would I build a new query and then save it in the database?
If you want to add Querydefinitons to an existing access database you can do so with the Access Interop asssembly.
Create a new c# project and add a reference to:
Microsoft Office 12.0 Access database engine Object Library
(or a version that matches your Office/Access version)
This code creates a Query in the Access Database for every table in the database to query the count of rows:
var dbe = new DBEngine();
var db = dbe.OpenDatabase(#"c:\path\to\your\youraccessdatabase.accdb");
// loop over tables
foreach (TableDef t in db.TableDefs)
{
// create a querydef
var qd = new QueryDef();
qd.Name = String.Format("Count for {0}", t.Name);
qd.SQL = String.Format("SELECT count(*) FROM {0}", t.Name);
//append the querydef (it will be parsed!)
// might throw if sql is incorrect
db.QueryDefs.Append(qd);
}
db.Close();

How do I find the owner of a given database?

Using .NET's DbConnection.GetSchema(), how do I find the owner of a given database?
Alternatively, if you have another solution that is not coupled to a specific impelementation of SQL, I'd like to hear that as well.
The GetSchema call of DbConnection unfortunately doesn't retrieve the DB owner for you :-(
But you can try this on SQL Server:
select
db.name, db.database_id, l.name, l.type
from
sys.databases db
inner join
sys.login_token l on db.owner_sid = l.sid
If you want to connect to SQL Server from .NET, you could use the SMO (SQL Management Objects) and find your owner like this:
Server server = new Server("Your Server");
Database db = server.Databases["Your Database"];
Console.WriteLine("Database owner is: " + db.Owner);
Marc
I don't believe that the SQL-92 standard specifies that a Catalog (a database) must have an owner. As such, I don't know that you can get a non-implementation-specific way of doing this.

Categories