Friday, November 12, 2010

How read and write the XML file using Sql server and dot net framework 3.5

Hi.. friends,
Its so simple
first of all use this code to write xml file using dot net
using system.Data
using System.Data.SqlClient
Dataset ds=new Dataset()
sqlDataAdapter da=new sqldataadapter("select * from tablename",connectionstring)
da.fill(ds)
ds.writexml("filename")
now Suppose the name of file is abc.xml
and if you want to convert this xml file into table format and insert its values into another table then use the sql server 2005 this procedute........
create proc xyz @xmldoc varchar(max)
as declare @ihandle int
exec Sp_Xml_PrepareDocument @ihandle output, @xmldoc
select * into #tablename1 from openxml(@ihandle,'/newdataset/tablenameofdataset',2) with
( columnname datatypes(size) ,.......)
insert into tablename(columnname.......) select columnname,......... from #tablename1

using this code you can convert xml file into table format after that insert into database
xmdoc is the variable where pass the xml file as a string abd in openxml ()pass 3 argument first is handler sendone isxmlfile firsttablename and third one is 2 and after the with specify all columnsname and datatypes of the table. after that you can do this.

How to get all datatypes of sqlserver 2005

Use the command........
Select * from sys.types
To get all column information of database then use the
select * from information_schema.columns