SQL与XML交互

1、SQLSERVER导入XML

 String sConnection = "Data Source=127.0.0.1;Initial Catalog=testdb;User ID=sa;PassWord=newman2007";
            SqlConnection mySqlConnection = new SqlConnection(sConnection);
            SqlCommand mySqlCommand = new SqlCommand("select   *   from   ecsjsjhtxx   FOR   XML   AUTO   ", mySqlConnection);
            mySqlCommand.CommandTimeout = 15;
            mySqlConnection.Open(); 
          
            SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter(" select   *   from   ecsjsjhtxx ", sConnection);
            DataSet myDataSet2 = new DataSet();
            mySqlDataAdapter.Fill(myDataSet2);


            //myDataSet1.WriteXml("ecSjsjhtxxNew1.xml");
            myDataSet2.WriteXml("d://ecSjsjhtxxNewfdsafsda444444444442.xml",XmlWriteMode.WriteSchema    );

2、XML导入SQLSERVER

 

--Up_HyConvertDataToDbFromXML 'd:/ecsjsjhtxx.xml'
alter PROCEDURE Up_HyConvertDataToDbFromXML
 @v_FileName varchar(20)--XML文件名及路径
as
BEGIN   
        set nocount on
     if not exists (select   *   from   sysobjects   where   id   =   object_id('DrTbFrmXml'))  
  begin  
    create  table DrTbFrmXml(doc  varchar(8000))  
  end  
  --1、读取XML
  exec ('bulk   insert   DrTbFrmXml   from   '''+@v_FileName+''''   ) 

  delete from DrTbFrmXml where (doc is null ) or (left(ltrim(doc),5)='<?xml')
  
  if  exists (select   *   from   sysobjects   where   id   =   object_id('XmlTemp'))  
  begin  
     drop table XmlTemp
  end
  
  --2、产生流水号
  select idx=Identity(int,1,1),* into XmlTemp from DrTbFrmXml
  --3、遍历读取数据,并写入正式表
  
  declare @v_idx  int
  declare  @v_Doc   Varchar(8000)  
  declare  @v_DocA   Varchar(8000)  
  declare @idoc int
  declare curList cursor
  for select idx from temp where idx<10000
  open curList
  fetch next from curList into @v_idx
  while @@fetch_status=0
  begin
     set @v_Doc='<?xml   version="1.0"   encoding="GB2312"?>   '
     select @v_Doc=@v_Doc+doc from temp where Idx=@v_idx 
     EXEC   sp_xml_preparedocument  @idoc OUTPUT , @v_Doc ;
             INSERT INTO ecsjsjhtxx   SELECT * FROM  OPENXML (@idoc, N'/ecsjsjhtxx')  WITH ecsjsjhtxx                            
     EXEC sp_xml_removedocument @idoc
     fetch next from curList into @v_idx 
  end
  close curList
  deallocate curList
        set nocount off
END

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值