tags:

views:

61

answers:

4

I have Created One XML File..... Node :

<Patient>Kanna</Patient>
<Id>12</Id>

And I transferred those values to SQL Db. Using C#.Net

Now I added One More Value in Same XML File

<Patient>Kanna</Patient>
<Id>12</Id>
<Patient>Raja</Patient>
<Id>13</Id>

Question Is : How to append New Value to SQL Db....

Can U Suggest Me any Idea ?

Thanks In Advance:-)

+1  A: 

Question Is : How to append New Value to SQL Db....

Answer is: with how you did for first value...

Im not sure your question is clear btw.

Serkan Hekimoglu
ya u r correct,,,but i set SNo (Pkey) In SqlDb Table;
kummsp
First time its inserted..then second time it does not allow duplication,,,
kummsp
i want to append only new data.
kummsp
Whats is SNo? And what is first and second value of SNo?
Serkan Hekimoglu
SQL doesnt allow insert, if your PK is not unique. So, be careful abour your PK SNo. It must have unique value. I suggest you to use AutoIncrement int, or uniqueIdentifier (Guid).
Serkan Hekimoglu
No. In XML FILe i didnt Have SNo.....For My Usage I Create SNo Field in SQL Table. SNO(PKey),Patient(Varchar),Id(Int)..
kummsp
THree fileds in SQl Table.I want to store Patient,Id in Sql Db from XML file.
kummsp
If your SNo is unique, sql allows that insert. What is your SNo field type?
Serkan Hekimoglu
I want to SNo filed to be automaticaly increase;;;when new data can be appended from xml file...
kummsp
SNo(Int)(PKey).
kummsp
So you must make your SNo field type INT, and set Auto Increment property TRUE.
Serkan Hekimoglu
K i done ,,its increasing. automatically.
kummsp
So SQL must allow insertion now.
Serkan Hekimoglu
But I inserted Second time the Values from XML File repeated storing in Database...{ I want to Just Append New Value from XML File}
kummsp
can you write your code pls.
Serkan Hekimoglu
int sno = 1; XmlDocument xmlDoc = new XmlDocument(); //Load the Xml File xmlDoc.Load("D:\\sample.xml"); //Specify the nodes in Xml File. XmlNodeList dataNodes = xmlDoc.SelectNodes("/information/data");
kummsp
foreach (XmlNode node in dataNodes) { int id = Convert.ToInt32(node.SelectSingleNode("id").InnerText); string name = node.SelectSingleNode("name").InnerText; int age = Convert.ToInt32(node.SelectSingleNode("age").InnerText);
kummsp
connectionstringmethod(); SqlCommand cmd = new SqlCommand("INSERT INTO XmlToSql (SNo,Id,Name,Age) Values(@sno,@id,@name,@age)", connection); {cmd.Parameters.AddWithValue("@sno", sno); cmd.Parameters.AddWithValue("@id", id); cmd.Parameters.AddWithValue("@name", name); cmd.Parameters.AddWithValue("@age", age); cmd.ExecuteNonQuery();
kummsp
}sno++; } .. Its Coding to Insert XMl Nodes into SQL
kummsp
Please remove int sno =1; and sno++;. Because if you use auto increment int in sql fields, you dont need to use PK value like in here. Remove it please.
Serkan Hekimoglu
A: 

Two easy ways:

1) Do a regular update:

UPDATE myTable SET myvalue = myValue + @newXML WHERE myID = @id

2) Update the XML using SQL XML commands:

Example (From egghead cafe link):

update [News].[News] set

Article.modify('replace value of(/NewsArticle/@Title)[1] with
sql:variable("@Title")') where ID=@ID;

http://www.eggheadcafe.com/software/aspnet/33280808/update-xml-column-in-a-single-statement.aspx

http://whyiamright.wordpress.com/2008/01/02/updating-xml-column-in-sql-server-2005/

http://www.15seconds.com/issue/050818.htm

Ryan Ternier
A: 

If the XML can be imported into DataSet using ReadXml(...) method, you may store it into a database in very elegant way.

Schematically:

// Create and fill dataset
DataSet set = ...;
set.LoadXml(...);

// Open sql connection, make command and adapter
SqlDataAdapter adapter = ...;
adapter.InsertCommand = new SqlCommand("your sql command", connection);

// Save the dataset
adapter.Update(set, "TableName");
Dmitry Karpezo
A: 

Maybe this will help in gettin you started, I agree with Lucero that you need to look closely at your xml structure...

DECLARE @xml xml
SET @xml = '<Patients/>'

DECLARE @insertedPatient nvarchar(45)
SET @insertedPatient = 'Nads McSteamy'

DECLARE @insertedID int
SET @insertedID = 1


SET @xml.modify
            ('
            insert element Patient {sql:variable("@insertedPatient")} into 
            (/Patients)[1]
            ')

SET @xml.modify
            ('
            insert element ID {sql:variable("@insertedID")} after 
            (/Patients/Patient)[1]
            ')

select @xml
imlovinit