Backup a SQL Server Database

The following instructions work for most versions of SQL Server, Version 2008R2 was the installed version when creating these instructions.

Not all possible options are detailed, this is a quick start set of instructions.

  1. With SQL Server Management Studio open right-click the database to be backed up.
  2. On the context menu select Tasks > Back Up
    sql-server-task-menu
  3. On the backup dialog confirm the desired database is selected from the dropdown.
    sql-server-backup-general
  4. In the Destination section, select the Disk option and click the Add Button. Specify the backup filename.
  5. Select Options from the navigation menu on the left. Select the Overwrite all existing backup sets option under Overwrite media group.
    sql-server-backup-options
  6. Select the Verify backup when finished box in the Reliability section.
  7. Click OK.
  8. A summary of the backup settings will be displayed summarising the options chosen.

That is it in the simplest form. There are many other options to choose once you get into it.

Serialise a class to XML in C#

This simple example shows how to serialise a class as XML. It is an easy way of getting the a full textual copy of the class structure, no matter how complicated the class is. In the past I have seen this used for logging values of a class instance.

First of all we need an class. I have made these up, with a few different types.

public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateTime DOB { get; set; }
    public Address Address { get; set; }
    public int NoOfDependents { get; set; }
    public double Height { get; set; }
}

public class Address
{
    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string Town { get; set; }
    public string City { get; set; }
    public string PostCode { get; set; }
}

And an instance of the class with some data. For this example I am actually creating a list of Person objects.

var listOfPeople = new List<Person>();

listOfPeople.Add(new Person
{
    DOB = DateTime.Today,
    FirstName = "Bernadette",
    LastName = "Williams-Harrison",
    NoOfDependents = 2,
    Height = 4.6,
    Address = new Address
    {
        Address1 = "2345 Dead End Street",
        Address2 = string.Empty,
        City = "Pembely",
        PostCode = "BB67 9PP",
        Town = "Little Stichins",
    }
});

listOfPeople.Add(new Person
{
    DOB = DateTime.Today,
    FirstName = "Andrew",
    LastName = "Carton",
    NoOfDependents = 1,
    Height = 6.0,
    Address = new Address
    {
        Address1 = "11 Park View",
        Address2 = "Chester House",
        City = string.Empty,
        PostCode = "CM1 4BY",
        Town = "Mordon",
    }
});

And the final part is the code to perform the sreialisation.

var xmlSerializer = new XmlSerializer(listOfPeople.GetType());
var textWriter = new StringWriter();
xmlSerializer.Serialize(textWriter, listOfPeople);

var result = textWriter.ToString();

The result variable will be of string type, containing XML. This is what it looks like

<?xml version="1.0" encoding="utf-16"?>
<ArrayOfPerson xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Person>
    <FirstName>Bernadette</FirstName>
    <LastName>Williams-Harrison</LastName>
    <DOB>2014-05-14T00:00:00+01:00</DOB>
    <Address>
      <Address1>2345 Dead End Street</Address1>
      <Address2 />
      <Town>Little Stichins</Town>
      <City>Pembely</City>
      <PostCode>BB67 9PP</PostCode>
    </Address>
    <NoOfDependents>2</NoOfDependents>
    <Height>4.6</Height>
  </Person>
  <Person>
    <FirstName>Andrew</FirstName>
    <LastName>Carton</LastName>
    <DOB>2014-05-14T00:00:00+01:00</DOB>
    <Address>
      <Address1>11 Park View</Address1>
      <Address2>Chester House</Address2>
      <Town>Mordon</Town>
      <City />
      <PostCode>CM1 4BY</PostCode>
    </Address>
    <NoOfDependents>1</NoOfDependents>
    <Height>6</Height>
  </Person>
</ArrayOfPerson>

One thing to note is the resulting text may contain formatting characters, such as \r, \n, \” or others.  Do with them what you will.

Creating a new repository with VisualSVN and TortoiseSVN

The following steps describe how to create a new repository in Visual SVN (SVN) and set it up as a working folder for your project code.  There may be alternative ways to perform some of these steps but this is the way I was taught and it has always worked for me.

Step 1: Create a new repository in VisualSVN (right click) Repositories > Create New Repository

Create Repository Step 1

Step 2: Specify the name of the new repository in the ‘Create New Repository’ dialog.  Ensure to tick the checkbox if you want the repository to contain the default folder structure (trunk, branches and tags) and I strongly recommend you do.  In this case the trunk folder will be the main folder containing the code.

Be sure to make a note of the Repository URL, you will need it later.

Create Repository Step 2

Click OK and you now have an empty code repository in SVN.

Step 3: The next step is to set up a working folder on a local drive.  This can be an existing folder containing code for an existing project, or if this is to be a new project a new empty folder.  In this example I am using an existing project.

Right-click the working folder and select SVN Checkout from the context menu.

Create Repository Step 3

Step 4: In the checkout dialog specify the URL of the repository which you made a note of earlier.

The Checkout directory will prepopulate with the location of the folder you right-clicked on.

Create Repository Step 4

Step 5: After clicking the OK button this dialog will be displayed if the working folder is not empty and already contains project code.  Because we know the SVN repository is empty it is safe to click the Yes button which is what you want to do on this occasion.

If the working folder was empty skip to step 6.

Note: If the SVN repository was not empty and the working folder was not empty you do not want to checkout to the folder as you will potentially end up with mixed projects in the same folder.

Create Repository Step 5

Step 6: The Checkout Finished dialog confirms a working folder has been linked to the SVN repository.  Click OK.

Create Repository Step 6

Step 7: Going back to Windows Explorer you will see the folder now has a green icon (known as an icon overlay).  There are a number of icon overlays describing different states.  The green circle with a tick indicates that the working folder is up to date and in sync with the SVN repository.

Create Repository Step 7

Step 8: If the working folder already contained files and subfolders they will not yet have an icon overlay because we have only set up and linked the working folder with SVN not any of it’s contents that already existed, as they did in my case.

As files and folders are added to the working folder they will not be added to the SVN repository automatically, so they too will not have an icon overlay initially.

We need to explicitly tell SVN to include the sub files and folders in the repository.  Select all the working folder contents, right-click the selection and select TortoiseSVN > Add from the context menu.

[Every time files or folders are created these steps can be repeated to add them to the SVN repository.]

Create Repository Step 8

Step 9: The Add dialog is displayed with the folder contents listed.  Un-tick the checkboxes for the items not to be added to the SVN repository, then click OK.  It is common practise to exclude bin and obj folders containing compiled assemblies, solution user options (.suo) and project user files (csproj.user) to name a few.

Create Repository Step 9

Step 10: Once the OK button is clicked on the Add dialog a new dialog is displayed confirming the contents have been added.

Create Repository Step 10

Step 11:  We are nearly there.  The files and folders added will now have an icon overlay indicating the fact they have been marked for inclusion in SVN,  they have not yet been physically added to the SVN repository .  As in the image below this is usually represented by a blue cross.

Create Repository Step 11

Step 12: To physically add the added items to the SVN repository we need to Commit the changes by returning to the parent working folder, right-clicking on the working folder and select SVN Commit from the context menu.

Create Repository Step 12

Step 13: The Commit dialog will be displayed.  As this is the initial commit check the path you will commit to.

Always add a meaningful message.  As this is a new project the text ‘Initial Commit’ is sufficient.  If you have been working on an existing project, make the message relevant to the changes being committed. e.g. “Added Logging for person class”.  Add multiple messages for multiple changes.  Alternatively if there are multiple changes to commit, write the message for each change and only select the files relevant to the change, then repeat for the next change and so on.

Create Repository Step 13

Step 14: After the OK button is clicked on the Commit dialog the list of files committed will be confirmed in the Commit Finished dialog.

Create Repository Step 14

That, as they say, is that.

The Add and Commit actions can be performed as and when required during development.