将多个表从SQL Server导出到XML以进行还原 [英] Export multiple tables from SQL Server to XML for restore
本文介绍了将多个表从SQL Server导出到XML以进行还原的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在C#/ASP.NET中有一个Web应用程序.我想使用相同的唯一键(Company ID
)导出所有数据库表的数据.所有表都有此键(每个公司都不同).我想这样做,以备将来备份. Linq或经典C#是否有可能?如何实现这种备份和还原?
I have a web application in C# / ASP.NET. I want to export all the database table's data, with the same unique key (Company ID
). All the tables have this key (different for every company). I want to do that for future backup. Is it possible with Linq or classic C#? How can achieve this backup and restore?
推荐答案
解决方案的一个示例是
using System.Data.SqlClient;
using System.Data;
using System.IO;
namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
var connStr = "Data Source=MOHSINWIN8PRO\\SQLEXPRESS;Initial Catalog=AB2EDEMO;Integrated Security=True";
var xmlFileData = "";
DataSet ds = new DataSet();
var tables = new[] {"Hospital", "Patient"};
foreach (var table in tables)
{
var query = "SELECT * FROM "+ table +" WHERE (Hospital_Code = 'Hosp1')";
SqlConnection conn = new SqlConnection(connStr);
SqlCommand cmd = new SqlCommand(query, conn);
conn.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
conn.Close();
conn.Dispose();
xmlFileData+=ds.GetXml();
}
File.WriteAllText("D://SelectiveDatabaseBackup.xml",xmlFileData);
}
}
}
这将创建SelectiveDatabaseBackup.xml
,以后可用于还原备份
this will create SelectiveDatabaseBackup.xml
which can be later used to restore the backup
这篇关于将多个表从SQL Server导出到XML以进行还原的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文