csharp Create table backup

internal static void BackupTable(string table)
{
	try
	{
		using (MyDataContext db = MyDataContext.Create())
		{
			//string tableSuffix = DateTime.Now.ToString("yyyyMMdd");// use if keeping a backup for each day
			string tableSuffix = "_Backup";// use if keeping just the one backup table
			string cmd = "";

			// drop the backup table first
			try
			{
				cmd = String.Format("Drop Table {0}", table + tableSuffix);
				db.ExecuteCommand(cmd);
			}
			catch { }

			// copy contents from table into a backup table
			try
			{
				cmd = String.Format("Select * Into {0} From {1}", table + tableSuffix, table);
				db.ExecuteCommand(cmd);
			}
			catch { }
		}
	}
	catch { }
}
Creates a copy of a table, with a suffix of today's date.

Updated: Tuesday 5th June 2012, 09:39pm

There are 0 comments

Leave a comment of your own

Comments are currently closed.