How to save data into database objects using XML format:
We have situations where we need to send the data from our application to database using XML format. In that scenario, first we need to clear about how to read XML document and insert into SQL objects. If you are not clear about the XML file reading in SQL, Please click the follwing link http://sqlpearl.blogspot.com/2012/08/how-to-open-read-xml-document-in-sql.htmlWith C# we can build the XML file by using the class XmlWriter form the name space System.XML. XmlWriter provides the specific methods to write data into xml format with the help of either datasets or other data object types. See the below example to know how to prepare XML object and send to database object.
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
namespace SampleWebApplication
{
public partial class WebForm1 : System.Web.UI.Page
{
private
string strConnectionString = ConfigurationManager.AppSettings["strConnection"];
protected
void Page_Load(object
sender, EventArgs e)
{
DataSet
ds = GetProducts(strConnectionString);
StringBuilder
strOverview = new StringBuilder();
XmlWriter
objOverview = XmlWriter.Create(strOverview);
ds.Tables[0].WriteXml(objOverview);
SaveProducts(strConnectionString,
strOverview.ToString());
}
public
static DataSet
GetProducts(string DBConnectionString)
{
try
{
return
SqlHelper.ExecuteDataset(
DBConnectionString,
CommandType.StoredProcedure,
"GetProducts"
);
}
catch
(SqlException sqlEx)
{
throw
sqlEx;
}
catch
(Exception ex)
{
throw
ex;
}
finally
{
}
}
public
static void
SaveProducts(string DBConnectionString, string strOverview)
{
try
{
SqlHelper.ExecuteNonQuery(
DBConnectionString,
CommandType.StoredProcedure,
"xmlProducts",
new SqlParameter("@XmlOverView", strOverview)
);
}
catch(SqlException sqlEx)
{
throw
sqlEx;
}
catch(Exception ex)
{
throw
ex;
}
finally
{
}
}
}
}
|
No comments:
Post a Comment