Click here to Skip to main content
15,891,473 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
sir,
i am building up a windows form app, and i am dealing with service based database, i want a method to backup my all database, how can i do that? i heard about csv file, but i dont know much about that, can you help me to backup my database. for future use
Posted

1 solution

It depends very much on which database product you are using e.g. you could issue the SQL command to export the database. But assuming you don't have that facility and keeping it simple, you could try something like this ...
DataSet ds = new DataSet(tablename);
            SqlDataAdapter da = new SqlDataAdapter("Select * from " + tablename, GetConnection());
            da.Fill(ds);
            ds.WriteXml(Path.Combine(foldername, tablename) + ".xml", XmlWriteMode.WriteSchema);


This will give you one XML file per table, how you get all the table names again depends on the database you're using.

There's lots of support in C# for this sort of thing - for a more complete solution try reading
SQL Server 2005 Database Backup and Restore using C# and .NET 2.0[^][]
 
Share this answer
 
Comments
shaikh-adil 22-Nov-12 7:33am    
i can you xml as backup option??
This code you have given is used for backup. Then what to do for restoring?
CHill60 22-Nov-12 8:55am    
Yes XML would be a valid backup option.
For restoring you would then start with the DataSet.ReadXml() function.

*But* I really recommend that you read the article and comments at the link I provided - it comes with full code samples, or google "c# database backup and restore"
Good Luck.
sariqkhan 25-Nov-12 4:08am    
thank you sir

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