2013-01-15 32 views
0

有人能告訴我用Windows Azure SQL數據庫中的數據生成XML文件的最佳方式嗎?使用Windows Azure SQL數據庫生成XML文件

基本上,我想用Windows Azure SQL數據庫中的數據通過查詢某個表來創建XML,並且數據很大(大約90 MB)。由於我需要至少每隔幾個小時執行一次這項工作,這應該表現得非常好。

有什麼建議嗎?

感謝, Prav

回答

0

這是一個非常普遍的問題,並沒有真正具體到SQL Azure的,所以很難給你一個很好的答案。我建議你研究查詢SQL數據庫的不同方法,以及編寫XML的不同方式。這可能會給你想法提出更具體的問題。

90MB不是特別大 - 它不應該很難被記憶。但是,儘管如此,您可能想要考慮一次只保留一小部分數據的方法。例如,從SqlDataReader讀取數據並立即將其寫入XmlTextWriter,或沿着這些行寫入。

+0

感謝您的意見。 – user1462119

0

一種方法可以查找數據庫並將結果保存到ADO.net DataTable中。獲得DataTable後,使用TableName屬性爲其選擇一個名稱。然後使用DataTable的WriteXml方法將DataTable保存到您選擇的位置。確保指定XmlWriteMode.WriteSchema以確保保存架構和數據。

注意,如果數據表將是使用2GB或更大,你打的.Net的默認對象內存限制。一種解決方案是將您的查詢分解爲更小的塊,並按原始查詢以XML格式存儲多個數據表。另一個解決方案是將.Net中的對象的最大大小增加到大於2Gb。這伴隨着它自己的一系列風險和性能問題。然而,超過2GB的對象大小限制,請確保你的應用是64位,應用程序被編譯到.NET 4.5或更高版本以及app.config文件應啓用=「真」 gcAllowVeryLargeObjects。

using System.Data; 
using System.Data.SqlClient; 

string someConnectionString = "enter Aure connection string here"; 
DataTable someDataTable = new DataTable(); 
SqlConnection someConnection = new SqlConnection(someConnectionString); 
someConnection.Open(); 
SqlDataReader someReader = null; 
// enter your query below 
SqlCommand someCommand = new SqlCommand(("SELECT * FROM [SomeTableName]"), someConnection); 
// Since your are downloading a large amount of data, I effectively turned the timeout off in the line below 
someCommand.CommandTimeout = 0; 
someReader = someCommand.ExecuteReader(); 
someDataTable.Load(someReader); 
someConnection.Close(); 
// you need to name the datatable before saving it in XML 
someDataTable.TableName = "anyTableName"; 
string someFileNameAndLocation = @"C:\Backup\backup1.xml"; 
// the XmlWriteMode is necessary to save the schema and data of the datatable 
someDataTable.WriteXml(someFileNameAndLocation, XmlWriteMode.WriteSchema);