I want to check in a database that if it snapshot is already existing. If yes I want to delete the snapshot and create a new one. How can i do it in an efficient manner. Also is it possible to check the time when it was created? I am using C# at back-end to achieve it.
You could use a SQL query to determine whether a database is a snapshot and then execute that query from with C#.
This SQL query should help you determine if a database snapshot exists:
select *
from sys.databases
where name = '<your_db>'
and source_database_id is not null
I would refer to this article for an example of how to execute SQL from within C#: https://msdn.microsoft.com/en-us/library/dw70f090(v=vs.110).aspx#_SqlClient
The code will likely need elevated privileges within SQL Server in order to execute this query so be careful with how these features are exposed. I strongly suggest using a parameter-based query as shown in the example within that link to avoid exposure to a SQL injection attack.
See this question for more information on how to determine if a database is a snapshot using SQL server: https://dba.stackexchange.com/questions/35105/how-can-i-query-for-existing-database-snapshots-in-sql-server
This article contains information on dropping snapshots from within SQL server: https://msdn.microsoft.com/en-us/library/ms190220.aspx
Similarly, this article indicates how to create one: https://msdn.microsoft.com/en-us/library/ms175876.aspx
Please be careful when dropping snapshots via code. The same permissions used to drop a snapshot allow the user to drop a database.
This article indicates that the date the snapshot was created is stored in the column create_date: https://msdn.microsoft.com/en-us/library/ms178534.aspx
If it were me, I'd look at the Database class in the SMO library. Specifically, the IsDatabseSnapshot and DatabaseSnapshotBaseName properties. If memory serves, CreateDate should reflect the creation date of the snapshot, not the source database.
Related
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.
I am looking for an idea or some direction. I have to transfer data from one database to another both are structurally and schema wise same.
Its a complete database with maybe 70 tables and having relationship between tables at different levels. Even though i ' m going to mess up the identity when i move across database but as of now i am ok with it.
Idea which i thought was to load required data from all table into an XML and then create connection to second database and push data from this XML its kind of repeated and not best way at all. So looking for direction.
Can i use entity framework for this somehow??
I cannot use SSIS for this it has to be C# Sorry.
You can create a linked server as stated in the comments to your question. You seemed to indicate that you know how to do this, but in case not, you can do this from SQL Server Management Studio by drilling down to "Server Objects > Linked Servers" beneath your source database on the source database server, then right-click, "New Linked Server", etc.
Then you would use a statement like this, for example, from your C# code:
insert into DestServer.DBName.dbo.TableName
select * from SourceServer.DBName.dbo.TableName
Assuming you are connected to 'SourceServer' and that 'SourceServer' maintains a linked server object pointing to 'DestServer'. Note: you don't actually need to use the fully-qualified name for the table on 'SourceServer', but I've put it there for clarity. I.E. you could also do this:
insert into DestServer.DBName.dbo.TableName
select * from TableName
Don't forget to setup the permissions properly in your linked server object so that your query can write to the table in the destination server. You can do this any number of ways, and often (because I work in a small environment where it's maintained by just me and a couple other folks) I just use the "sa" login:
Yes, can use linked servers in .NET.
You just use the 4 part name.
What you can do in TSQL in SSMS you can do in a .NET SQLcommand.
My experience is that you get better performance connecting to the server you are writing to.
We have a winform app, all the SQL is inline and no stored procedure is used unfortunately.
What is the best way to keep a trail of what action (INSERT, UPDATE or DELETE) is performed against a SQL table, monitor and capture a record of the activity going on in a single table.
I know SQL server profiler is available but ideally I don't want to keep the profiler running all the time, what I need is something running behind the scenes and capturing all the activity for one table.
I thought of using triggers but there are some disadvantages of using it so I was wondering if there are other options?
Thanks
If you're using SQL Server 2008 (and up), Change Data Capture sounds like a good place to start:
http://msdn.microsoft.com/en-us/library/bb522489(v=sql.105).aspx
If you are using ORACLE, you can audit a table using this statement (if fireid
is the name of the user doing the updates):
AUDIT SELECT TABLE, UPDATE TABLE, INSERT TABLE, DELETE TABLE BY fireid BY ACCESS;
The results will be stored in the SYS.AUD$ table
(If you're using another database just search the documentation for auditing DML statements.)
SQL SERVER: There is a feature called "SQL AUDIT" Is this what you're looking for?
http://www.bradmcgehee.com/2010/03/an-introduction-to-sql-server-2008-audit/
There are several ways. If you just want to do it in Code (depending on your program)
You can just add the commands to a list and write them to a file.
If you are using EntityFrameWork then I would log the data changes. And use of stored procs is really best when using EF.
A sample of your code or project would help. TransactionLogs already capture that information.
And OBVIOUSLY your using Sql Server THOSE ORACLE PEOPLE ARE JUST SO JEALOUS.
Also look at using DataSourceViews, it's a way to tell what is going on with the server.
I think SQL Server CHANGE TRACKING TeEchinique is best suitable for your requirements.
As stated in the title, how can I programmatically create a SQL Server CE 4.0 from a remote SQL Server?
I want my application to allow users to delete the .sdf and create a new one based on the new remote database schema when there is a database schema update. And then download relevant data for offline use.
I already read up about the SqlCeEngine part, but I am not good at SQL Server CE queries - seem to give many syntax errors when trying out in Management Studio.
I also tried Microsoft Sync Framework Snapshot synchronization but it feels too bulky and the Local Cache Database modifies my database schema and generates a lot of junk I do not need. Maybe a lower level solution like querying information.schema or something may work better?
Checkout DMO. Using managed code, you can enumerate objects like tables, columns on the sql server side.
http://msdn.microsoft.com/en-us/library/aa174487(v=sql.80).aspx
Here's a tutorial to get you started:
http://www.codeproject.com/KB/database/SMO_Tutorial_1.aspx
Concerning the data, one option is the bcp utility
http://msdn.microsoft.com/en-us/library/aa337544.aspx
Those are good starting points if you want to extract and create a new database. For mirroring/sync, probably not a good path. If it's read only data on the client and you jst want to update the local data, then you can just extract again and throw away the old 'data cache'
You can use my scripting API and command line tools to do this: http://exportsqlce.codeplex.com - see for example this blog post: http://erikej.blogspot.com/2010/02/how-to-use-exportsqlce-to-migrate-from.html
This may be a more up to date way using SQL only:
SELECT * FROM INFORMATION_SCHEMA.COLUMNS
I have a system where the customer wants to rework the current model so that everytime a user makes a change an administrator must accept the change before its written to the database..
I was thinking of doing a quick fix for this by overriden SaveChanges and taking each object in the ObjectStateManager and adding its intended sql code to a limbo table that would keep the inteded sql query saved until an admin has accepted it (and then run it).
I know that you can use ToTraceString() on database querys, but can you somehow pull the intended sql query on the object taken from ObjectStateManager?
Was thinking something like this:
var modified = DB.ObjectStateManager.GetObjectStateEntries(System.Data.EntityState.Modified);
foreach (var mod in modified)
{
//Insert the query to the limbo table
tblPendingChanges change = new tblPendingChanges();
//Code omitted
change.sql = mod.Query;
//Code omitted
DB.tblPendingChanges.AddObject(change);
mod.Delete();
}
DB.SaveChanges();
Your solution is terrible. If you have the requirement that each change must be approved it leads to approval workflow where you save changes to some temporary store and move them to the main tables once approved. It is really not done on SQL level. If you need something working on SQL level don't use high level tools like Entity framework because they are really not designed to support this. For example EF will not provide you SQL commands generated for data modifications.
I solved this issue by using a entity wrapper found here
This allowed me to read each sql statement before it was sent to the server. Redirecting it.
I had to edit the wrapper to allow parameters to be inserted correctly into the statement so that the sql statement could be run.