0

私はXMlファイルを持っています私はこのコードを使用してすべてのXmlを読んでいます。しかし、XMLレコードをSQLテーブルに挿入したいのですが、読んだものは何でもです。そのための解決策は見つかりませんでした。XMlは行数で構成されており、適切な方法で挿入するにはどうすればよいですか。

       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
            {

            }
        }
4

1 に答える 1

3

OPENXML関数の使用方法の一例を次に示します。

C#DataTableとSQLサーバーのOpenXML関数を使用したデータの一括挿入


関数を利用する: SQLサーバーで利用可能なOPENXML(Transact-SQL)を使用すると、サーバーにデータを挿入できます。

また読む:OPENXMLを使用して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
于 2012-06-06T07:48:59.957 に答える