我在 C#/ASP.NET 中有一个 Web 应用程序。我想使用相同的唯一键 ( Company ID
) 导出所有数据库表的数据。所有表都有这个键(每个公司都不同)。我想这样做以备将来备份。Linq 或经典 C# 是否可行?怎样才能实现这种备份和恢复呢?
问问题
4197 次
2 回答
2
解决方案的一个例子是
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
以后可用于恢复备份
于 2013-04-04T06:06:12.160 回答
-2
SaveFileDialog SD = new SaveFileDialog();
if (SD.ShowDialog() != System.Windows.Forms.DialogResult.Cancel)
{
System.IO.StreamWriter SW = new System.IO.StreamWriter(SD.FileName);
foreach (string str in checkedListBox1.CheckedItems)
{
string XMLSTR = //Heads.funtions.ConvertDatatableToXML(
Heads.funtions.fire_qry_for_string(string.Format("Select * from {0} FOR XML AUTO,TYPE, ELEMENTS ,ROOT('{0}')", str)); // , str);
SW.Write(XMLSTR);
SW.WriteLine("###TABLEEND###");
}
SW.Close();
//_CommonClasses._Cls_Alerts.ShowAlert("Clean Up Completed...!!!", "CleanUP", MessageBoxIcon.Information);
}
于 2014-07-14T15:20:35.773 回答