简体   繁体   English

使用XML将数据保存到MSSQL Db

[英]Saving data to MSSQL Db using XML

I have created a ASP.NET C# MVC application. 我创建了一个ASP.NET C#MVC应用程序。 I need to insert some records in the database, and the following code works fine. 我需要在数据库中插入一些记录,并且以下代码可以正常工作。 What i want to do now is to save records in the DB by using XML. 我现在要做的是使用XML将记录保存在DB中。

I found an example in a tutorial , but i am unable to modify my code send data to MSSQL via XML. 我在教程中找到了一个示例,但是我无法修改我的代码通过XML将数据发送到MSSQL。

                using (SqlCommand c = new SqlCommand("INSERT INTO PPL  (Name, Age) VALUES (@Name,@age)", c)){
                c.Open();
                c.ExecuteNonQuery();}

Can someone help me here ? 有人可以帮我吗?

** The tutorial http://www.mssqltips.com/sqlservertip/2118/scripts-to-use-xml-to-insert-and-update-rows-in-a-sql-server-table/ **本教程http://www.mssqltips.com/sqlservertip/2118/scripts-to-use-xml-to-insert-and-update-rows-in-a-sql-server-table/

UPDATE 更新

I created a SP 我创建了一个SP

CREATE PROCEDURE INSERT_PPL

@NAME nchar(200),
@AGE nchar(3),

AS

INSERT INTO PPL(NAME,AGE)

VALUES (@NAME,@AGE)

Now i am suppose to use EXEC sp_xml_preparedocument @hdoc OUTPUT, @xmlData add this line (according to the tutorial), but no clue how to edit it so it works in my program. 现在,我想使用EXEC sp_xml_preparedocument @hdoc OUTPUT, @xmlData添加此行(根据教程),但是不知道如何对其进行编辑,因此它可以在我的程序中工作。

This is all what i have done. 这就是我所做的一切。

I think a good approach is to serialize you classes to XML and pass them to SQL. 我认为一种好的方法是将您的类序列化为XML并将它们传递给SQL。 For the server part, a path to begin with would 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
    as
    begin

        insert Books (Title, Rating)
        select
            n.value('@Title', 'varchar(50)'),
            n.value('@Rating', 'int')
        from
            @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')
        from
            @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 * from Books;
    select * from 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 to pack the procedure statements in a transaction. 并且不要忘记将过程语句包装在事务中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM