dotnetspider.com
Login Login    Register      

TutorialsForumCareer DevelopmentResourcesReviewsJobsInterviewCommunitiesProjectsTraining

Subscribe to Subscribers
Talk to Webmaster
Tony John

Facebook
Google+
Twitter
LinkedIn
Online MembersFarzin Janta
Asheej T K
More...
Join our online Google+ community for Bloggers, Content Writers and Webmasters




Forums » .NET » SQL Server »

How to convert Physical XML files into Sqlserver Tables


Posted Date: 10 Aug 2012      Posted By:: Murali     Member Level: Silver    Member Rank: 0     Points: 2   Responses: 2



How to convert Physical XML files into Sqlserver Tables?

* Physical XML need to be converted into XML table with respective columns
* XML file may contains multiple tables, in such case multiple tables has to be created in db

Please help me out...




Responses

#683914    Author: Ravindran        Member Level: Diamond      Member Rank: 3     Date: 10/Aug/2012   Rating: 2 out of 52 out of 5     Points: 4

Hi,

You can read XML and its data using dataset XML Read method. If have only table is there then use bulk upload method


using System.Data.SqlClient;
using System.Data.OleDb;
using System.Configuration;

public partial class _Default : System.Web.UI.Page
{
SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["Con"].ToString());
SqlCommand sqlcmd = new SqlCommand();
SqlDataAdapter da = new SqlDataAdapter();
DataTable dt = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
lblmsg.Text = "";
LoadGrid();
}

protected void Button1_Click(object sender, EventArgs e)
{

ExportToSQLServer("D:\\Sample.xlsx");

}

void ExportToSQLServer(string path)
{
System.Data.DataSet DtSet = null;
System.Data.OleDb.OleDbDataAdapter MyCommand = null;
try
{
string excelconstr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + path + "';Extended Properties=Excel 12.0;";
OleDbConnection excelcon = new OleDbConnection(excelconstr);
MyCommand = new System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1$]", excelconstr);
DtSet = new System.Data.DataSet();
MyCommand.Fill(DtSet, "[Sheet1$]");
dt = DtSet.Tables[0];
//Here data are filled in dt so you can use loop to get field and insert in various table or single table only then use below sql bulk copy
SqlBulkCopy sqlBulk = new SqlBulkCopy(ConfigurationManager.ConnectionStrings["Con"].ToString());
sqlBulk.DestinationTableName = "emp";
sqlBulk.WriteToServer(dt);
excelcon.Close();
lblmsg.Text = "Excel data upload successfully";
}
catch (Exception ex)
{
lblmsg.Text = ex.ToString();
}

}
}


Regards
N.Ravindran
Your Hard work never fails



 
#683925    Author: Prasad kulkarni        Member Level: Diamond      Member Rank: 8     Date: 10/Aug/2012   Rating: 2 out of 52 out of 5     Points: 4

Every XML has an internal Table like structure. you can use following code snippet to covert XML to datatable/Dataset then you can update that dataset to SQL database
check following example


DataSet objDS = new DataSet();
objDS.ReadXml(new XmlTextReader(new StringReader(xml)));
DataTable objDTable= objDS.Tables[0];

//OR use XMLdocument to read XML

XmlDocument objDOM= MethodReturnsXmlDocument();
DataSet objDS= new DataSet()
objDS.ReadXml(new XmlNodeReader(objDOM));


hope it helps

Thanks
Koolprasd2003
[DotNetSpider MVM]



 
Post Reply
You must Sign In to post a response.

Next : How to update new row and sum of all the records in SQL SERVER
Previous : What is differences between sqlserver2005 and sqlserver2008
Return to Discussion Forum
Post New Message
Category:

Related Messages



Follow us on Twitter: https://twitter.com/dotnetspider

Active Members
TodayLast 7 Daysmore...

Awards & Gifts
Email subscription
  • .NET Jobs
  • .NET Articles
  • .NET Forums
  • Articles Rss Feeds
    Forum Rss Feeds


    About Us    Contact Us    Copyright    Privacy Policy    Terms Of Use    Revenue Sharing sites   Advertise   Talk to Tony John
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
    2005 - 2012 All Rights Reserved.
    .NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
    Articles, tutorials and all other content offered here is for educational purpose only.
    We are not associated with Microsoft or its partners.