How to insert data from XML to database
Click here to Print Source Codeusing System; using System.Data; using System.Windows.Forms; using System.Xml; using System.Data.SqlClient; namespace WindowsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { string connetionString = null; SqlConnection connection; SqlCommand command ; SqlDataAdapter adpter = new SqlDataAdapter(); DataSet ds = new DataSet(); XmlReader xmlFile ; string sql = null; int product_ID = 0; string Product_Name = null; double product_Price = 0; connetionString = "Data Source=servername;Initial Catalog=databsename;User ID=username;Password=password"; connection = new SqlConnection(connetionString); xmlFile = XmlReader.Create("Product.xml", new XmlReaderSettings()); ds.ReadXml(xmlFile); int i = 0; connection.Open(); for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++) { product_ID = Convert.ToInt32(ds.Tables[0].Rows[i].ItemArray[0]); Product_Name = ds.Tables[0].Rows[i].ItemArray[1].ToString(); product_Price = Convert.ToDouble(ds.Tables[0].Rows[i].ItemArray[2]); sql = "insert into Product values(" + product_ID + ",'" + Product_Name + "'," + product_Price + ")"; command = new SqlCommand(sql, connection); adpter.InsertCommand = command; adpter.InsertCommand.ExecuteNonQuery(); } connection.Close(); MessageBox.Show("Done .. "); } } }
C# XML - Related Contents
- How to XML in C#
- How to create an XML file in C#
- How to open and read XML file in C# , XmlReader , XmlTextReader , XmlDocument
- How to create XML file from Dataset
- How to Open and read an XML file to Dataset
- How to create an XML file from SQL
- How to search in a XML file
- How to filter in a XML file
- How to create Excel file from XML
- How to create XML file from Excel
- How to XML to DataGridView
- How to create a TreeView from XML File
- How to create a Crystal Reports from XML File
- XML Serialization Tutorial
- XML Serialization
- XML DeSerialization