Insert XML Files into Databases Using Xml2OleDb
来源:互联网 发布:mac口红哪个色号最好看 编辑:程序博客网 时间:2024/04/26 06:39
Introduction to Xml2OleDb | |||||
[Download Code] XML is definitely the best way to share data via the World Wide Web. XML data can easily be integrated into web applications for many uses. However, at some point you (or someone you work for) will want XML data added to a database. Hopefully this will be an easy task for you by the time you finish reading this article and looking at the code sample. Now let’s get started. The first step is to load the XML file into a DataSet and get the first table. This table is the DataTable we will be adding to the database. Next remove the extension from the XML file. The file name minus the extension will be used as the table name in the database. An error message will be displayed if the XML file is not in the proper format. Look at the Authors.xml file include in the download to see the proper format. // Load the XML file into a DataSet using the // file name submitted from the web formdataSetXml.ReadXml(HttpContext.Current.Server.MapPath(textBoxXml.Text));// Get the first table in the DataSetdataTableXml = dataSetXml.Tables[0];// Remove the extension from the XML file. The file name minus// the extension will be used as the table name in the database.tableName = textBoxXml.Text.Substring(0,textBoxXml.Text.Length -4); Once the XML file has been loaded, check to see if the DataTable contains any data (rows). Next call two routines: the first checks to see if the table exists in the database and creates the table if it does not exist, and the second inserts the data from the XML file into the database. Finally, if there were no errors, display a message stating that the XML file was successfully inserted into the database. // Check to see if table one contains any data (rows)if(dataTableXml.Rows.Count > 0)
|
Adding the XML Data to the Database | |
[Download Code] |
Code Sample Instructions and Conculsion
Now let’s discuss using the sample code. Place all of the files from the download into a virtual directory. You do not need to compile the source, because I have included the assembly (.dll) file in the download. This sample creates tables in databases, but it does not create the database. You will need to create the database before adding XML data to it. I have included an empty Access database (Database.mdb) and an empty Excel database/workbook (Database.xls) for you to use. You need to give the ASP.NET process account (default is MACHINENAME/ASPNET) write permissions on the database file. For FoxPro/dBase this could be just an empty directory.
Navigate to the virtual directory you created. Enter the name of the XML file (must be in the same virtual directory). Don’t enter the path, just the file name (Authors.xml). I have included a sample XML file (Authors.xml) that you can use. Next, enter the OleDb connection string and click the submit button.
Example connection strings:
Access: Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:/data/database.mdb;
Excel: Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:/data/database.xls;Extended Properties=Excel 8.0;
FoxPro/dBase: Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:/data;Extended Properties=dBASE IV;
SQL Server: Provider=SQLOLEDB; Data Source=localhost; Initial Catalog=database;User ID=sa;Password=;
When I started thinking about writing this article I imagined that code would be very complicated. I was very pleased to find out how few lines of code it took. That’s how easy ASP.NET makes it to add XML data into the OleDb database of your choice.
- Insert XML Files into Databases Using Xml2OleDb
- Transform XML into HTML using XSLT
- Transform XML into HTML using XSLT
- Transform XML into HTML using XSLT
- Using ActiveX Controls Example: Insert Internet Explorer into your Dialogs
- INSERT INTO
- insert into
- insert into
- Using Databases with Python
- insert 和 insert into
- Parser XML and insert into DB with C#
- Read A Flat File Using File Layout And Insert Into The Specific Component Using CI ( Bulk Insert )
- Read A Flat File Using File Layout And Insert Into The Specific Component Using CI ( Bulk Insert )
- Using Databases with Python -Musical Track Database (produce database from XML file)
- Using NHibernate with Multiple Databases
- Android数据存储-Using Databases
- Mapping Objects to XML Files using Java 5 Annotations
- Querying XML Files Using XPATH in ASP.NET
- 知识点---(组合(Composition)VS (inheritace)继承)
- 青岛各饭店餐馆订餐电话大全
- Delphi 5 ADO
- 用正则表达式解析C#文件(Updated)
- 一条检验SQL基础的语句!
- Insert XML Files into Databases Using Xml2OleDb
- Emetrix Developer Support的回复
- 微软高级软件研发主管研修计划(Architect 2000)之:企业IT体系结构
- 考研高分的秘诀 正式版v2.0 作者:richie
- 在服务器段处理鼠标双击点坐标
- .NET 程序在布署到客户机时出现异常问题的解决
- 回到西安。
- vb.net下的socket编程
- 一个程序员的面试经历(二)_Microsoft面试