简体   繁体   中英

Export sql server table structure with data programmatically using C#

I have a table with data in sql server. I need to take the backup of that table structure as well as data into some sort of file programmatically using C# . May be .sql extension file. Same way as we do using Task option in sql server management studio.

I googled it but did not find anything related to this. If anybody suggest me any link will be highly appreciated.


(Edited in from comments)

The backup is for safety purpose. As I am going execute the conversion logic for one of my scheduler control. The process is:

  1. Get the backup of the table data into a list.
  2. Truncate the table.
  3. Convert the data to desired format.
  4. Insert the converted data into the same table.

If any conversion logic problem, I can fix it later using the backup file.

This may help you:

select * into newtable from yourtable

you do not need to create newtable manually, just execute the query.

Update :You can download your Database data to dataset and use dataset to write xml with or without schema using DataSet.WriteXml method like this:

ds.WriteXml(@"E:\Temp.xml", XmlWriteMode.WriteSchema);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM