Click here to Skip to main content
15,885,914 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi every one
i want to take backup of product and Stock table instead of complete database of SQL Server.
i want to take backup using c# winform application.
please help me how can i do this.

What I have tried:

i dont know how can i do this. please any one help me.
Thanks in advance
Posted
Updated 26-Jun-19 3:44am
Comments
F-ES Sitecore 26-Jun-19 6:43am    
Have you googled "sql server backup single table" and tried the many suggested results?

As F-ES said in the comments, here is the first google result which includes many different options for you.

https://stackoverflow.com/questions/19698310/backup-a-single-table-with-its-data-from-a-database-in-sql-server-2008
 
Share this answer
 
The quick and easy way to copy the structure and data within a table is to use
SELECT * INTO [new_table_name] FROM [source_table]

Within an application, what I would do is to create the new table with a date(time) naming convention so that you can see when it was backed-up.
C#
private void CreateBackupStock() {
  string NewTableName = string.Format("Stock_bak_{0}", DateTime.Now.ToString("yyyymmdd"));
  string NewCommand = string.Format("SELECT * INTO {0} FROM [Stock]", NewTableName);
  using (SqlConnection conn = new SqlConnection([ConnectionString])) {
    SqlCommand cmd = new SqlCommand(NewCommand, conn);
    conn.Open();
    cmd.ExecuteNonQuery();
    conn.Close();
    cmd.Dispose();
  }
}
 
Share this answer
 
Comments
Sinisa Hajnal 26-Jun-19 9:42am    
Depending on business needs, it may be better to have one table with all backups (kind of history table with timestamps of each backup). That way you can have all the data in one table and request the data from certain date without having to change the tables
Fahid Zahoor 27-Jun-19 6:00am    
i want to take backup and save as with .back extension in my computer after that i take this file that has .back extension and put into my second PC and then i restore it. i restore just two tables that(i.e Product and Stock) has in a backup file others table data are not effected due to restore this. please help me how can i do this
MadMyche 27-Jun-19 7:39am    
If the PCs are connected what I would do is create an SSIS package which could do the copy on a schedule or on demand. Add that onto your SQL Server and then you can trigger it from code.
I would create the backup table and add backupTime (timestamp) column. In the application you read from your source table and write into this "history table" with timestamp (may be only date if you don't need exact time) - that way you keep all your data changes in one continuous table and since you control it from the app, you can write the logic for restoring the data depending on the date. In addition, you get to see all changes of certain record if it is relevant at some point ("Karl, why did you delete this client!?" :) )
 
Share this answer
 

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