SQL Server - update table using xml - Asked By muthuraman alexander on 05-Jan-12 04:54 AM

hi all
I'm developing asp.net/c# web app
In that i have a XML data.....
I want to update the SQL table using that xml ...how can i do this
I can insert that table ... but it ll change the primary ..... i dnt want to change the primary key value
How can i do this
Please advice me

Thanks in advance
Jitendra Faye replied to muthuraman alexander on 05-Jan-12 04:57 AM
I think you want to pass data from xml to database.

use this code-

using System;
using System.Data;
using System.Windows.Forms;
using System.Xml;
using System.Data.SqlClient;

namespace WindowsApplication1
    public partial class Form1 : Form
      public Form1()

      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());
        int i = 0;
        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;
        MessageBox.Show("Done .. ");

kalpana aparnathi replied to muthuraman alexander on 05-Jan-12 04:57 AM

Insert into #TempTable
//Basically do bulk insert into temp table then...
Update MyTable
  Set Field1 = tt.Field1,
    Field2 = tt.Field2,
FROM #TempTable tt
where primaryKey = tt.PrimaryKey
Riley K replied to muthuraman alexander on 05-Jan-12 05:02 AM

The best bet is to use SQL Bulk copy

DataSet reportData = new DataSet();
SqlConnection connection = new SqlConnection(”CONNECTION STRING”);
SqlBulkCopy sbc = new SqlBulkCopy(connection);
sbc.DestinationTableName = “report_table”;
//if your DB col names don’t match your XML element names 100%
//then relate the source XML elements (1st param) with the destination DB cols
sbc.ColumnMappings.Add(”campaign”, “campaign_id”);
sbc.ColumnMappings.Add(”cost”, “cost_USD”);
//table 4 is the main table in this dataset
//remove the xml file

Sandeep Mittal replied to muthuraman alexander on 05-Jan-12 07:27 AM
You can pass the XML to stored proc as parameter and insert the data in stroed proc. Refer the below example
SET @XML = '<DocumentElement>
SELECT  VisitId = Node.Data.value('(VisitId)[1]', 'VARCHAR(100)')
    , Day = Node.Data.value('(Day)[1]', 'VARCHAR(100)')
    , Amount = Node.Data.value('(Amount)[1]', 'NUMERIC(16,4)')
FROM    @XML.nodes('/DocumentElement/DetailPart') Node(Data)