问题
I have SQL Server database for articles.
Articale
table:
CREATE TABLE [dbo].[T_Articale]
(
[id] [int] IDENTITY(1,1) NOT NULL,
[Title] [nvarchar](100) NULL,
[Text] [nvarchar](max) NULL,
[LocationID] [int] NULL,
[TribeID] [int] NULL,
[ArticaleText] [nvarchar](max) NULL,
[ArticaleDate] [date] NULL,
[SearchID] [int] NULL,
[ClassificationID] [int] NULL,
[CountryID] [int] NULL,
[isLocal] [int] NULL,
[Note] [nvarchar](150) NULL,
CONSTRAINT [PK_T_Articale]
PRIMARY KEY CLUSTERED
)
Where ArticaleText
is the content of the article with formatting (HTML tags)
Files
table:
CREATE TABLE [dbo].[T_Articale_Files]
(
[id] [int] IDENTITY(1,1) NOT NULL,
[Artricle_id] [int] NULL,
[FileName] [nvarchar](50) NULL,
[FileData] [varbinary](max) NULL,
[FileData2] [nvarchar](max) NULL,
CONSTRAINT [PK_T_Articale_Files]
PRIMARY KEY CLUSTERED
)
Here the user can attach files with the article FileData
binary column is the actual file
I have a function to export the database to XML
var xmlFileData = "";
DataSet ds = new DataSet();
var tables = new[] { //List of DB Tables };
foreach (var table in tables)
{
var query = "SELECT * FROM " + table;
SqlConnection conn = GetConnection();
SqlCommand cmd = new SqlCommand(query, conn);
conn.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable(table);
da.Fill(dt);
conn.Close();
conn.Dispose();
ds.Tables.Add(dt);
}
// xmlFileData = ds.GetXml();
var xmlstream = new StringWriter();
ds.WriteXml(xmlstream, XmlWriteMode.WriteSchema);
string xmlWithSchema = xmlstream.ToString();
File.WriteAllText("D://SelectiveDatabaseBackup.xml", xmlWithSchema);
My problem is now the database grows to 700MB (if I take a script with data) my XML function give out of memory exception
What fixes or optimizations can solve that problem (clustering, indexing ...) (the database will continue to grow)
Edit
Why I need XML??
The situation is I have some computers that can not access the server and does not have SQL Server installed so I need to make that data available without DB Engine. So the first thought was XML. It is not for backup purpose.
It is a Windows Forms application.
回答1:
Let’s consider an example for generating a XML:
CREATE PROC GenerateXMLproc
AS
SELECT [Name], ProductNumber, Color
FROM dbo.Product
FOR XML raw(‘Product’), elements, root(‘Products’)
Now we’ll discuss the above mentioned options for saving this XML data:
- Using CLR Stored Procedure: In this method we can create a CLR stored procedure which will take the XML data generated, file name & location of the file as input parameters. And then it can process that XML as per the requirements and can save that XML file to the desired location.
Creating CLR stored procedure:
[Microsoft.SqlServer.Server.SqlProcedure]
public static void SaveXMLOutput(SqlXml XmlData, SqlString Filename)
{
//Save the XML data being passed to the SP to a file location
//specify the name of the file suppiled to the SP
XmlDocument xmlDoc = new XmlDocument();
SqlPipe output = SqlContext.Pipe;
xmlDoc.LoadXml(XmlData.Value);
xmlDoc.Save(Filename.Value);
}
//This will give a DLL ‘SaveXMLOutput.dll’ Creating Assembly in SQL Server
CREATE ASSEMBLY SaveXMLOutputAssembly
from ‘C:\Temp\SaveXMLOutput.dll’
WITH PERMISSION_SET = EXTERNAL_ACCESS
–‘ trustworthy ’ property of the database must be set to ‘ON’ before using the EXTERNAL_ACCESS for the aseembly.
–This can be done using ALTER DATABASE [DataBaseName] SET trustworthy ON
Creating stored procedure from the Imported DLL/Assembly:
CREATE PROCEDURE SaveXMLOutput
@xmldata XML,
@filename nvarchar(1024)
AS
EXTERNAL NAME SaveXMLOutputAssembly.[XMLOutput].SaveXMLOutput
–XMLOutput is the name of class and SaveXMLOutput is the name of the SP as defined in the class
–Executing this SP
execute SaveXMLOutput ‘Pass the XML Data generated from other SP’
,‘ C:\Temp\MyXML.xml’
来源:https://stackoverflow.com/questions/45163636/export-sql-server-database-to-xml-optimization