C# Backing up data to file which can be restored later - c#

I'm looking for some feedback as to what is the best way to back data up from a sql server database which can be restored at a later date. This back up needs to be in a file which the user can use to restore data at a later date.
Does anyone have any ideas or examplea as to the best way to do this using C# and Sql Server?
EDIT:
This shouldn't back the whole database, just a set of data specified by the user using dates.
Thanks

If I understand properly, you want to audit just some of your data for changes (as opposed to having a full DB backup) and to be able to selectively roll them back to a previous state.
If that's what you want to do, you may do that from within sql by using a "audit" table and populating it via triggers on the table where your original data is stored (i.e. each time a row is inserted/updated/deleted the trigger will write on the "audit" table what the previous value was and when it was changed).
See here for an example.

Try this bit of Code. i have used this before.
http://www.daniweb.com/forums/thread202843.html

Related

How to log changes in database value with the user who has changed it?

I have a (MYSQL database and .NET server) and I want to log every change in value by the user (means user of application).
I want the log to have previous and changed values as well as the user who have change it.
I know that MYSQL give event when the data is changed and I can perform a trigger on that but I don't no how to pass it the user name. I want to know if there is any framework that tackle this problem.
I can write a procedures instead of writing a query for CRUD operations but I don't know how to get the previous values before the updates without the selecting it before in the procedure.

Azure SQL: How to determine if table was altered or created and by whom

My team works with a large Azure SQL database where several other teams insert and read data from our database. They sometimes need to create or alter tables but those actions should be coordinated with our team and unfortunately has not been the case. We've had a couple scenario's where one of those teams updated a stored procedure. As a result their changes are not under our source control and if we create a local database for development or do a backup/restore we get errors because of missing references.
We are looking for a way to programmatically determine if a table was altered or modified. It doesn't need to be real-time. I considered reading logs and looking for alter or create commands. I've not has much success as the logs are binary and I don't currently know how to parse them. My other thought is to keep a copy of the master database sys tables and routinely compare them to see if something changed. I'm not sure how well that would work or if I could determine who made the change. Thoughts, Ideas?
Please keep in mind that this is using an Azure SQL Database which is a bit more limited than a standard SQL database.
You can use DDL Triggers as explained here.
CREATE TRIGGER safety
ON DATABASE
FOR DROP_TABLE, ALTER_TABLE
AS
PRINT 'Save change on a log'
SELECT EVENTDATA().value('(/EVENT_INSTANCE/TSQLCommand/CommandText)[1]','nvarchar(max)');
Additionally you can use Extended Events to track schema changes. Look at samples here.
Finally you can also see how Azure SQL Auditing may fit your needs.

How can I tell if a database record has changed?

I'm writing a process that will run on a schedule and periodically scan records in a SQL Server database and check for changes to those records. Unfortunately the records in this database are not versioned/timestamped in any way and I can't change the schema because it's not my database. Short of comparing every single field I don't have any idea how to check to see if a change has actually occurred.
Right now I'm using entity framework for all data access, but I can switch to direct queries if needed. Is there an easy way to do this?
EDIT: I need a way to check each individual record for changes, not a database/table as a whole.
I don't think checking each record can work for big databases (it can take forever)
But you could calculate hash for each row and store it in your apps database. Then on every run recalculate hash and compare with what's in your apps db.
Also I know you can't change schema, but maybe you could add triggers to monitored dataabse that will log changes?
Another option could be setting up replication and detecting changes (with triggers as above) in database that receive's replicated data (but I have to admit I never configured replication and I may be wrong about how it works).
If you're using SQL Server 2008 or greater, you may be able to make use of either Change Data Capture or Change Tracking. Here's a link to the MSDN documentation.

How I can track all changes that made to database and save them for review?

I am building an application that users can add, update data.
Is there any way to track changes to a database and save these tracks in file or in another database for example.
Thanks in advance.
I've never tried them but there are built in tools and utilities:
SQL Server Change Tracking
In SQL Server, there's Query Profiler which can run a trace against a live database and log all the queries to another database or a file
http://msdn.microsoft.com/en-us/library/ms187929.aspx
You can use triggers on the tables you want to track, and with those triggers you can determine which fields have changed (or if a record has been inserted or deleted), and then write the appropriate entry to an audit table.
Here is one article i found after a real quick search... Adding simple trigger-based auditing to your SQL Server database (Googling on the keywords sql server trigger audit should bring you mucho satisfaction and knowledge).
We currently use Audit tables to do this. So for every "Table" there is a corresponding "Table_AT" and there are triggers for every CRUD operation. So, on any CRUD function, the new data is written to the audit table.
This link has examples http://web.archive.org/web/20071006100909/http://sqlserver2000.databases.aspfaq.com:80/how-do-i-audit-changes-to-sql-server-data.html

Best way to track changes and make changes from Mysql -> MSSQL

So I need to track changes that happen on a Mysql table. I was thinking of using triggers to log all the changes made to it and then save these changes in another table. Then I will have a cron script get all these changes and propagate the changes into the Mssql database.
I really dont expect a lot of information to be proporgated, but the data is very time sensitive. Ideally the MSSQL will see these changes within a minute, but I know that this requirement may be too high.
I was wondering if anyone had a better solution.
I have the bulk of the site written in .net but use vbulletin as the forums (sorry but there are no .net forums as powerful or feature rich like vbulletin)
The majority of the replicator tools use this technique. Fill another table on insert/update/delete triggers that containt the tablename and the PK or a unique key.
Then a reader reads this table, do the proper "select" if insert/update to get the data, then updates the other database.
HTH

Categories