Copying data in SQLExpress between servers

If you have one instance of SQLExpress and you'd like to copy the data from that server to another SQL Server, Express version or not, you'll run into problems.

Management Studio Express will not let you copy the contents directly. If you have access to the file system or can log in directly to the server you can do it by simply moving the database files and attaching them to the database.

But if you don't have that access and you still need to move the data, here are some steps that might rescue you:

  1. Create the tables in the destination database

  2. Use the program supplied here to copy the data in the tables

Creating the tables in the destination database

Ask someone to do this for you if you don't know how.

Use the following C# program:

You are going to need the connection string for the source and the target database. You can download the express edition of Visual C# to compile it with. Use at own risk. I've used it and it has works nicely. Mail any suggestions to raxptor@gmail.com

Copy and paste from this page or download it here.

/* 
 * Copies a table from one database to the other.
 *  
 * Use the program like this:
 * 
 *    MSSQLCopy <connectionstring-SOURCE> <tablename> <connectionstring-TARGET>
 * 
 * This program expects the tables to be empty before you insert. If they aren't you are going to receive errors.
 * There is no error handling whatsoever and you'll have to look at the exceptions and be clever.
 * 
 */


using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.Sql;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MSSQLCopy
{
    class Program
    {
        static void Main(string[] args)
        {
            //You must use the $ after the object you reference in the spreadsheet
            SqlConnection target = new SqlConnection(args[2]);
            target.Open();

            //You must use the $ after the object you reference in the spreadsheet
            using (SqlConnection conn = new SqlConnection(args[0]))
            {
               conn.Open();               
               using (SqlCommand cmd = new SqlCommand("SELECT * FROM [" + args[1] + "]", conn))
                {
                    using (SqlDataReader rdr = cmd.ExecuteReader())
                    {
                        using (SqlCommand cmdA = new SqlCommand("SET IDENTITY_INSERT " + args[1] + " ON", target))
                        {
                            try
                            {
                                cmdA.ExecuteScalar();
                            }
                            catch (Exception)
                            {

                            }
                        }

                        while (rdr.Read())
                        {
                            StringBuilder fields = new StringBuilder();
                            StringBuilder data = new StringBuilder();
                            using (SqlCommand insert = new SqlCommand("", target))
                            {
                                for (int i = 0; i < rdr.FieldCount; i++)
                                {
                                    if (i > 0)
                                    {
                                        fields.Append(", ");
                                        data.Append(", ");
                                    }
                                    fields.Append(rdr.GetName(i));
                                    data.Append("@" + rdr.GetName(i));
                                    insert.Parameters.AddWithValue("@" + rdr.GetName(i), rdr.GetValue(i));
                                }
                                insert.CommandText = "INSERT INTO " + args[1] + " (" + fields + ") VALUES (" + data + ")";
                                insert.ExecuteNonQuery();
                                Console.Write(insert.CommandText);
                            }
                        }
                        using (SqlCommand cmdAun = new SqlCommand("SET IDENTITY_INSERT " + args[1] + " OFF", target))
                        {
                            try
                            {
                                cmdAun.ExecuteScalar();
                            }
                            catch (Exception)
                            {

                            }
                        }
                    }
                }
            }
        }
    }
}