2012-06-06 2 views
0

XMl 파일이 있습니다.이 코드를 사용하여 모든 XML을 읽었습니다. 하지만 내가 XML 레코드를 SQL 테이블에 삽입하려고하는 동안, 내가 읽은 것이 무엇이든간에. .XMl은 행 수로 구성되어 있으며 올바른 방법으로 삽입 할 수있는 방법은 찾지 못했습니다.XML to SQL SERVER 레코드 삽입

 if (File.Exists(xmlpath)) 
      { 
      try 
       { 
       XDocument xmlDoc = XDocument.Load(xmlpath); 
       var vrresult = from a in xmlDoc.XPathSelectElements("/Parts/Part") 
           select new 
           { 
            S = a.Element("Section").Value, 
            M = a.Element("Mark").Value, 
            Q = a.Element("Qty").Value, 
            W = a.Element("Weight").Value, 
            RD = a.Element("databaseUpdateMark").Value 

           }; 
       GridView1.DataSource = vrresult; 
       GridView1.DataBind(); 

      } 
      catch (Exception ex) 
      { 
       Lbmsg.Text = ex.Message; 
      } 
      finally 
      { 

      } 
     } 
+0

당신이 무엇을하려고 했습니까? –

+0

xml에서 SQL Server 테이블로 읽은 레코드를 삽입하고 싶습니다. –

+0

답변을 확인하고 upvote 잊지 마시고 그것을 허용하는 경우 그것은 당신을 위해 작동합니다 ... –

답변

2

여기 OPENXML 기능을

Bulk Insertion of Data Using C# DataTable and SQL server OpenXML function 기능의


만들기 사용을 사용하는 방법을 하나의 예입니다 : SQL Server의 avialble 당신이 서버에 데이터를 삽입 할 수 있습니다 OPENXML (Transact-SQL)을 ...

도 읽음 : Use OPENXML to insert values from a string of XML

예 :

--Use OPENXML to pull records from an XML string and insert them into a database 

DECLARE @idoc int 
DECLARE @doc varchar (1000) 

--XML document 

SET @doc =' 
<ROOT> 
    <Employee> 
     <FirstName>Tom</FirstName> 
     <LastName>Jones</LastName> 
    </Employee> 
    <Employee> 
     <FirstName>Gus</FirstName> 
     <LastName>Johnson</LastName> 
    </Employee> 
</ROOT>' 

--Create an internal representation of the XML document 
--the idoc variable is set as the document handler, so we can refer to it later 

EXEC sp_xml_preparedocument @idoc OUTPUT, @doc 

-- Use the OPENXML rowset provider with an Insert 
-- @idoc lets us know which internal representation of an xml doc to use 
-- '/ROOT/Employee' shows us which node in the xml tree to work on 
-- the 2 denotes we want to use elemenet centric mapping for the values, as oppsed to attributes or a combination of both 
-- in the 'With' we specify how the output rowset will look 

INSERT INTO Employees (FirstName, LastName) 
SELECT * 
FROM OPENXML (@idoc, '/ROOT/Employee',2) 
WITH (FirstName varchar(10), 
LastName varchar(20) 
) 

-- Clear the XML document from memory 

EXEC sp_xml_removedocument @idoc 
+0

거기에 다른 방법이 있습니다. 그냥 읽기에서 수 vrresult에서 직접 읽을 수있는 각 값. 아니면 해결책을 설명 할 수 있습니까? –

+0

@kuldeepverma - 당신은 이것을 위해 proceudre를 생성해야하며 xml 문자열을이 코드와 xml 문자열에 전달해야합니다. 매개 변수에 의해 바뀌 었습니다 .... 이해해 주길 바랍니다. –

+0

Pranay Rana @ 이 코드를 이해했습니다. 어디서 어떻게이 저장 프로 시저를 호출 할 수 있습니까? 경로는 동적입니다. 사용자 입력에 따라 삽입 할 XML 파일이 많이 있습니다. –