Insert XML file into the database with XML2OL2OLDB

xiaoxiao2021-03-06  83

Original: http: //aspalliance.com/articleViewer.aspx aId = 477 & pId = -1 Author:? Andrew Mooney translation: Mencius Chapter E [download the source code http://aspalliance.com/download/477/ASPAlliance477.zip] Xml2OleDb Introduction XML is the best way to share data in the Internet, and data in XML format can be easily integrated into different web applications. But what if you want to insert an XML file into the database? XML2OL2OLDB will explain to the OLEDB database, such as SQL Server, Access, Excel, Visual FoxPro, FoxPro, and DBase, etc. Insert XML files, etc.

First, load the XML file into the DataSet and get the first table. This table is the DataTable we have to join the database; Next, remove the extension of the XML file, the file name removes the extension will be the table name used in our database . If the XML format is inaccurate, an error will be reported, and an example in the source code has an example of XML file format. See Authors.xml

// Load the XML file we submit to DataSetDataSetXml.ReadXML (httpcontext.current.server.mappath (textboxxml.text)); // Get the first table DataTableXML = DataSetXml.tables [0]; // Generate Table Name TableName = TextBoxxml.Text.Substring (0, TextBoxxml.Text.length -4);

Once the XML load is successful, first check if the data table has data (ROWS), then check if the database exists, if you do not exist, create one; then insert the data into the database from XML.

// Check if there is data existence (ROWS) IF (DataTableXML.Rows.count> 0)

Creating a database table Create a database connection to get the database schema information we have to add a table.

// create a database connection, open the database, the database table schema information obtained OleDbConnection oledbConn = new OleDbConnection (textBoxOleDb.Text); oledbConn.Open (); DataTable schemaTable = oledbConn.GetOleDbSchemaTable (OleDbSchemaGuid.Tables, new object [] {null, NULL, TABLENAME, "Table"});

// Check if the table exists, if there is a record IF in DataTable, SChem T.Rows.count <1) sqlcmd = "CREATE TABLE" TABLENAME "("; for (int i = 0; i

// Traverse the RowsForeach (DataRow Dr in DataTableXML.Rows) {string sqlcmd = "Insert Into [" TableName "] ("; // traversing the column for the DataTable (INT i = 0; i

Note: When testing, you will download the source code, create an empty database, set the modified permissions Database.mdb, Database.xls, create an empty directory for FoxPro / DBASE

转载请注明原文地址:https://www.9cbs.com/read-108101.html

New Post(0)