c# - Saving data to MSSQL Db using XML -
i have created asp.net c# mvc application. need insert records in database, , following code works fine. want save records in db using xml.
i found example in tutorial, unable modify code send data mssql via xml.
using (sqlcommand c = new sqlcommand("insert ppl (name, age) values (@name,@age)", c)){ c.open(); c.executenonquery();}
can me here ?
update
i created sp
create procedure insert_ppl @name nchar(200), @age nchar(3), insert ppl(name,age) values (@name,@age)
now suppose use exec sp_xml_preparedocument @hdoc output, @xmldata
add line (according tutorial), no clue how edit works in program.
this have done.
i think approach serialize classes xml , pass them sql. server part, path begin be:
create table books (bookid int identity, title varchar(50), rating int); create table chapters (bookid int, title varchar(50), sequence int); go create procedure addbook @book xml begin insert books (title, rating) select n.value('@title', 'varchar(50)'), n.value('@rating', 'int') @book.nodes('/book') x(n); declare @bookid int; set @bookid = scope_identity(); insert chapters (bookid, title, sequence) select @bookid, n.value('@title', 'varchar(50)'), n.value('@sequence', 'int') @book.nodes('/book/chapters/chapter') x(n); end; go declare @data xml; set @data = ' <book title="new book" rating="9"> <chapters> <chapter title="chapter 1" sequence="1" /> <chapter title="chapter 2" sequence="2" /> <chapter title="chapter 3" sequence="3" /> </chapters> </book>' exec addbook @data; select * books; select * chapters;
result:
bookid title rating ----------- -------------------------------------------------- ----------- 1 new book 9 (1 row(s) affected) bookid title sequence ----------- -------------------------------------------------- ----------- 1 chapter 1 1 1 chapter 2 2 1 chapter 3 3 (3 row(s) affected)
and don't forget pack procedure statements in transaction.
Comments
Post a Comment