简体   繁体   English

使用C#从XML到SQL

[英]XML to SQL with C#

I am developing a C# application to import about 26,000 XML records to a SQL Server database to be reformatted and imported into a new application. 我正在开发一个C#应用程序,以将大约26,000个XML记录导入到SQL Server数据库中,以重新格式化并导入到新的应用程序中。 I already have a database designed with two tables. 我已经有一个设计有两个表的数据库。 I have a students table with Student_ID as the primary key and a table called Student_Grades that includes all of the grades and also includes Student_ID as it's primary key and foreign key. 我有一个带有Student_ID作为主键的学生表,以及一个名为Student_Grades的表,该表包括所有年级,还包括Student_ID作为主键和外键。 All of the columns that are relevant are in the tables and are ready to receive data. 表中所有相关的列均已准备就绪,可以接收数据。 I also have a view called sview that combines all of the information together. 我还有一个名为sview的视图,它将所有信息组合在一起。 I am not trying to generate columns with the XML files, just transfer the data to the existing database. 我不尝试使用XML文件生成列,而只是将数据传输到现有数据库。 Now all that is left is taking the XML files and creating the application. 现在剩下的就是获取XML文件并创建应用程序。 I have a basic idea on how to do this, but any advice will be very much appreciated. 我对如何执行此操作有基本的想法,但是任何建议都将不胜感激。 One thing I have not figured out yet is how to do this with multiple XML files. 我还没有弄清的一件事是如何使用多个XML文件来实现。 I have around 20 xml files I need to be able to individually upload and insert into the database. 我需要大约20个xml文件,以便能够分别上载并插入数据库中。

<Student>
            <STUDENT_ID>a0068d</STUDENT_ID>
            <ENTRY_VERSION>6</ENTRY_VERSION>
            <TYPE>12</TYPE>
            <NAME>John Doe</NAME>
            <LANGUAGE>EN</LANGUAGE>
            <COMMENTS>Excellent Behavior</COMMENTS>
            <USERNAME>admin</USERNAME>
            <STUDENT_GRADES>
                <STUDENT_GRADE>
                    <NAME>Biology</NAME>
                    <VALUE>A</VALUE>
                    <INHERITED>false</INHERITED>
                </STUDENT_GRADE>
                <STUDENT_GRADE>
                    <NAME>English</NAME>
                    <VALUE>C</VALUE>
                    <INHERITED>false</INHERITED>
                </STUDENT_GRADE>
                <STUDENT_GRADE>
                    <NAME>Math</NAME>
                    <VALUE>B</VALUE>
                    <INHERITED>false</INHERITED>
                </STUDENT_GRADE>
                <STUDENT_GRADE>
                    <NAME>Greek</NAME>
                    <VALUE></VALUE>
                    <INHERITED>true</INHERITED>
                </STUDENT_GRADE>
            </STUDENT_GRADES>
</Student>
<Student>
            <STUDENT_ID>b0362f</STUDENT_ID>
            <ENTRY_VERSION>3</ENTRY_VERSION>
            <TYPE>5</TYPE>
            <NAME>Jane Doe</NAME>
            <LANGUAGE>EN</LANGUAGE>
            <COMMENTS>Takes Insulin Daily</COMMENTS>
            <USERNAME>admin</USERNAME>
            <STUDENT_GRADES>
                <STUDENT_GRADE>
                    <NAME>Science</NAME>
                    <VALUE>77</VALUE>
                    <INHERITED>false</INHERITED>
                </STUDENT_GRADE>
                <STUDENT_GRADE>
                    <NAME>English</NAME>
                    <VALUE>85</VALUE>
                    <INHERITED>false</INHERITED>
                </STUDENT_GRADE>
                <STUDENT_GRADE>
                    <NAME>Spanish</NAME>
                    <VALUE/>
                    <INHERITED>true</INHERITED>
                </STUDENT_GRADE>
                <STUDENT_GRADE>
                    <NAME>SocialStudies</NAME>
                    <VALUE>100</VALUE>
                    <INHERITED>false</INHERITED>
                </STUDENT_GRADE>
            </STUDENT_GRADES>
</Student>

Use the bcp executable that comes with SQL Server rather than to re-invent the wheel. 使用SQL Server附带的bcp可执行文件,而不是重新发明轮子。 See following webpage : https://msdn.microsoft.com/en-us/library/ms178129.aspx 请参阅以下网页: https : //msdn.microsoft.com/en-us/library/ms178129.aspx

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

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