Programmatically load and use databases to MS SQL Server 2008 - c#

I have MS SQL Server 2008 and a program written in C#, using Entity Framework. Without using any third-party tools, can I:
Do daily backup to a file of only certain tables from the database?
Programmatically tell the SQL Server "here is the file with some tables, restore it to the new database A" and then connect to the database A and use the data, if needed, delete the database and load next file?

You could. The daily backup should be done by SQL then you program would be used to locate the backup and restore it to a NEW DB. You can do the backup outside of SQL but not sure why you would need/want to.
Steps would be
- create new empty DB
- restore backup to new empty DB
- fix user permissions on DB (if applicable)
You can overload the constructor of you EF model to take in a connection string that you would make on the fly based on the DB you just reloaded.
As far as ony backing up and restoring some tables you may want to try the following.
Create seperate DB for the "data heavy" items that you do not need to backup and restore as often or that have no value for this process.
Move these items from the current DB to the New DB
Add views in the original DB that have the same exact name as the tables that moved out and pull in a views of the new tables on the second DB
As long as the views are names 100% the same name as the tables that you moved and the view is ONLY on the single table your program should not know the difference but you would now be able to backup the smaller DB and not include all the file and image data. Hope this makes sense
Can try to provide more info if you have a more pointed question

Related

Copy Database Content (Schema) from one SqlAzure Database to another existing SqlAzure Database

I'm looking for good ideas how to copy the whole Schema from a SqlAzure Database to another SQL Azure Database. (Tables, Data)
Goal:
I'd like to use a "template" Database seperated with Schemas. Each schema represent another kind of "template" data. On creating a new database (ef6, c#) I want to add the selected Template-Data...
What I tried:
=> Creating bacpac and try to import this.
Works great, but you can only import to new database, not to an existing database.
=> Creating scripts
Works but painfull.
Anyone can give me an advise ?
Have you tried using the schema compare feature in the SQL Server Data Tools available for Visual Studio? It will allow you to compare the complete schema of your template database and your target database. Once it has done the diff, you should use Group By > Schema to organize the results which will then enable you to selectively apply the changes for one or more specific Schema(s) to the target database. For more info on SQL Server Data Tools see https://msdn.microsoft.com/en-us/mt186501

Entity framework creating duplicate tables

I have inherited an entity framework application that i have been tasked to create within a separate environment with a completely separated database as well on a new db server. Seems easy enough. I backup the database from the original and restore it on the new database server. the data is the exact same at this point.
For testing purposes i change the local connection string to the new database server and run my web app locally. I get an error in the browser that is "MySql.Data.MySqlClient.MySqlException: Can't write; duplicate key in table '#sql-3b87_2d5f91'" i dont even have a table with that name and have no idea where it comes from. entity framework also creates duplicate tables for all of my tables except for migrations table.
I have tried other things as well, after restoring the db in the new db server to the original again, i have tried running 'update-database' and the same issues happen.
Now if i disregard restoring the new database with the backup from the original and run "update-database" on the new database server. it creates the schema correctly, but it lacks the data i need for testing.
Any ideas why this may be happening? i would like to avoid writing a sql script to transfer data.

Creating SQL Server Database files on the fly with C#

Is it possible to create database files that are independent from any SQL Server engine on the fly? The thing is that we want to create one database file per user in our application, and we want that database to be stored in the user's directory but we don't want to connect to it through SQL Server. Prety much like an sdf file, but instead of App specific, User specific.
So what we want to do is that every time a user is created in the app, generate their folder, and generate the database schema, which will be populated later.
Sounds like you're looking for SQL Server Compact. You can create any number of these databases at runtime and place them anywhere you want.
Sounds like you really want a combo of "Access" and the Jet engine.
It meets the database in a single file requirement and is reasonably performant for single user access.
Another possibility would sqlite, which is IMHO a better database but support for C# seems immature.
You can also have empty database file stored and each time a new user is created make a copy of that empty database file into the user folder. This way the schema will be there from the empty database file (it is like a template or a model database from the SQL Server).
SQL Server Express/Compact with smo to programatically create your db.

Synchronization between Client database and Central Database

I am trying to develop UI in C# .NET to synchronize 7 instances of backup databases with the central database one by one (All holding same schema) .The backup database( all 7 instances client databases) which is brought to the central server in a removable device such pendrive will consist of mdf and ldf files from each client and will be attached to the server where the central database resides. After all the client backup databases are attached i need to synchronize(update existing data or insert new data to the central database residing in server) each backup database one by one to central database. I want to know as how i can synchronize betweeen a backup database with a central database using C# .NET
Assuming that you will not be altering data from the other 6 databases via each instance you could keep some kind of log of record changes, read that and then update your central database based on the information stored in your log for each database.
If you also need to be updating each of the other 6 instances from each loaded instance, then you'll need to perform an aggregation of the overall changes then send them to each other.
you could also use row versioning to do compares of data, and load it to your central one via that (but you'll need to read rows of each table to find this).
If you also need to update data from your central database -> your client ones you could use a similar technique.
Edit
To perform the actual Update you will need to have a DataReader open to your "Source" database that finds the records.
Then from this object create a secondary reader that
"select **modified ** columns from each of the designated tables" and read that
You then create a command object and ExecuteNonQuery against your Destination database.

How to create empty local databases based on an existing .sdf file on VS2008 and SQL Server CE

I'd like to use Visual Studio 2008 IDE for editing my local database schema for a SQL Server Compact 3.5 database. However, everything in the documentation and in the UI seems to make an assumption that the application/assembly only wants to use a single database and never wishes to create new ones based on the existing schema.
I'd like to be able to edit the DB schema using visual tools and then to have a mechanism for creating new database files based on the edited schema. This way each document that my application creates would be stored in their own separate database files (.sdf). Furthermore, for the purpose of unit testing, I'd like to be able to create empty temporary .sdf files in order to run my unit tests on an empty database.
The only way I know how to create new empty databases using an existing schema is just to copy my master .sdf file to a new file.
What would be the recommended way to create new .sdf files using C# code? I.e., how to implement MyModel.Create("foo.sdf") so that it creates .sdf files using the schema I've created in Visual Studio.
EDIT: After a long investigation, I ended up ditching VS2008 local database .sdf editor completely and started creating new .sdf files by using LINQ-to-SQL. I now just specify a bunch of C# classes, annotate them to work with LINQ-to-SQL and create new database files by calling my DataContext's CreateDatabase method. MSDN has a good document on it: How to: Dynamically Create a Database (LINQ to SQL)
Create a "model.sdf" database, that contains the schema, and possibly needed initial data. If you want to create a new database, you create a copy from this model.
You could also create the Sql-DDL from an existing database (alternative link), then create an empty database and execute this SQL on it. This will be a lot slower.

Categories