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
MUTHU
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()
      {
        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 .. ");
      }
    }
}

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

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();
reportData.ReadXml(Server.MapPath(”report.xml”));
 
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”);
 
connection.Open();
 
//table 4 is the main table in this dataset
sbc.WriteToServer(reportData.Tables[4]);
connection.Close();
 
//remove the xml file

Regards
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
DECLARE @XML XML
SET @XML = '<DocumentElement>
<DetailPart>
<VisitId>1</VisitId>
<Day>Friday</Day>
<Amount>320.0000</Amount>
</DetailPart>
<DetailPart>
<VisitId>2</VisitId>
<Day>Friday</Day>
<Amount>520.0000</Amount>
</DetailPart>
</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)