Click here to Skip to main content
15,909,205 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

This may be a dumb question, but I just cannot find an answer. I would like to know how to backup SQL data only so that when I add columns, tables, etc. to my database I can simply backup the data, add the new database and import the old data without problem. I know the old data will not have all the values like, lets say I have the following database:

table = clients
columns = name, number

These have values.

Now I add column = surname.

I want to import name and number to the new table = Clients with columns = name, surname, number. Leaving surname blank ofcourse.

So far, every time I backup my database, the entire database is backed up, so when restoring it copies over my new database causing me to lose my new tables and columns.

Please can someone explain to me how this works?

This is how I backup and restore my database:

C#
public void BackupEntireSqlDatabaseToDisk(string fileName)
        {
            using (SqlConnection Conn = new SqlConnection(Connect.sConnStr))
            {
                Conn.Open();

                string database = Conn.Database.ToString();
                string sSql = string.Format("BACKUP DATABASE [" + database + "] TO DISK='{0}' WITH FORMAT", fileName);

                using (SqlCommand cmd = new SqlCommand(sSql, Conn))
                {
                    cmd.ExecuteNonQuery();
                }

                Conn.Close();
                Conn.Dispose();
            }
        }

        public void RestoreEntireSqlDatabaseToEzyGym(string fileName)
        {
            using (SqlConnection Conn = new SqlConnection(Connect.sConnStr))
            {
                Conn.Open();

                string database = Conn.Database.ToString();
                string UseMaster = "USE master";
                SqlCommand UseMasterCommand = new SqlCommand(UseMaster, Conn);
                UseMasterCommand.ExecuteNonQuery();

                string Alter1 = @"ALTER DATABASE [" + database + "] SET Single_User WITH Rollback Immediate";
                SqlCommand Alter1Cmd = new SqlCommand(Alter1, Conn);
                Alter1Cmd.ExecuteNonQuery();

                string Restore = string.Format("Restore database [" + database + "] from disk='{0}'", fileName);

                SqlCommand RestoreCmd = new SqlCommand(Restore, Conn);
                RestoreCmd.ExecuteNonQuery();

                string Alter2 = @"ALTER DATABASE [" + database + "] SET Multi_User";
                SqlCommand Alter2Cmd = new SqlCommand(Alter2, Conn);
                Alter2Cmd.ExecuteNonQuery();

                Conn.Close();
                Conn.Dispose();
            }
        }


Regards,
Chris

What I have tried:

I have attached my code for you to see what I tried.
Posted
Updated 16-Jun-16 12:39pm
v2
Comments
FranzBe 16-Jun-16 15:49pm    
When you need to make changes to your database structure, you could easily a) do a backup before you begin to apply you change, so you have a backup of the "before" state; and b) then after having done the change make a second backup the holds the "after" state.

You do not mention it, but you seem to work with MS SQL SERVER, so perhaps you might want to read something about backups of Transaction Logs; In a production environment you will have a complete backup say once a day and a backup of the transaction log perhaps performed every hour. So when there is a need to restore, you will restore the complete backup followed by a chain of restores of the various transactions logs. This way you will get the state of the database is has as the last restored transaction log was put to backup.

It's always a good idea to have the changes to be applied to the database ready to go in a prepared change-script. If you have this script you can always run it against a backup of a database that was taken before.

1 solution

Short answer, you cannot perform a backup of the database that contains just the data.

Slightly longer answer, you can script the database data to a file using SQL Server Management Studio (SSMS).
Open SSMS
Right-click the database & select Tasks >> Generate Scripts
On the Choose Objects page select the option Select specific database objects and the tick the Tables option
Click Next until you get to the Set Scripting Options page
Click the Advanced button
In the General Options section select Data only for the option Types of data to script

This will generate a script of all of the database data but it is a dumb script of the data in that it contains an Insert Statement for each data row.

Hope this helps
 
Share this answer
 
Comments
Christopher Smit 17-Jun-16 0:56am    
I have found a way to add this column without losing data. When i restore my database that contains the data I run the alter table command on the database while the application is running. I then backup the database again and the column is added to the table and data is still there. Is this a correct way of doing this or can it cause problems later on? The alter command is not in my code. I run it manually with visual studio sql server data objects.
an0ther1 19-Jun-16 17:53pm    
Hi Chris,

There is no problem doing that. Adding a column to a table will not remove the data that exists but some changes cannot be made - for instance modifying a timestamp column is not allowed - refer to MSDN Alter Table Command for more info.

Kind Regards

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900