if (dataSet.Tables[0].Count > 0)
{
// This routine checks to see if the table exists in the
// database and creates the table if it does not exist
TableCheck();
// This routine inserts the data from the XML file into the database
TableInsert();
//If the message label is empty this means there were no errors
if (Message.Text == String.Empty)
{
// This message will display when complete if there were no errors
Message.Text = "Inserting XML into OleDb data source is complete.";
}
}
private void TableCheck()
{
// Create an OleDb database connection using the connection string
// provided when the web form is submitted
OleDbConnection oledbConn = new OleDbConnection(@"Provider=SQLOLEDB;Data Source=(local)\SQLEXPRESS; Initial Catalog=aspnetdb;User ID=sa;Password=123456;");
try
{
// Open the database connection
oledbConn.Open();
// Retrieve database schema information for only the table we are looking for
// In this example the table name is the name of the XML file without the extension
DataTable schemaTable = oledbConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,
new object[] { null, null, tableName, "TABLE" });
String sqlCmd = "";
// Check to see if the table exists in the database schema
// If the table exists in the schema there will be 1 row in the DataTable
// If the table does not exist in the schema the DataTable row count will be zero
if (schemaTable.Rows.Count < 1)
{
// Make the create table sql command by iterating through the XML file's
// columns. This way the database columns will have the same name as the XML file.
sqlCmd = "create table " + tableName + " (";
for (int i = 0; i < dataTableXml.Columns.Count; i++)
{
// This adds each column as a text/string type with a length of 100
sqlCmd = sqlCmd + dataTableXml.Columns[i].ColumnName.ToString() + " char(100),";
}
// Remove the last ","
sqlCmd = sqlCmd.Substring(0, sqlCmd.Length - 1) + ");";
// Create and execute the create table command
OleDbCommand oledbCmd = new OleDbCommand(sqlCmd, oledbConn);
oledbCmd.ExecuteNonQuery();
}
}
catch
{
// If there are errors you will get this message
Message.Text = "The table could not be created or database does not exist.";
}
finally
{
// Close the database connection
oledbConn.Close();
}
}
private void TableInsert()
{
OleDbConnection oledbConn = new OleDbConnection(@"Provider=SQLOLEDB;Data Source=(local)\SQLEXPRESS; Initial Catalog=aspnetdb;User ID=sa;Password=123456;");
try
{
// Open the database connection
oledbConn.Open();
// Iterate rows in the dataset
foreach (DataRow dr in dataTableXml.Rows)
{
// Create the sql insert command for each row
string sqlCmd = "insert into [" + tableName + "] (";
string animalGuid = dr["动物GUID"].ToString();
if (!IsExistent(animalGuid))
{
// Iterate the datatable columns
for (int i = 0; i < dataTableXml.Columns.Count; i++)
{
// Add the column name
sqlCmd = sqlCmd + dataTableXml.Columns[i].ColumnName.ToString() + ",";
}
sqlCmd = sqlCmd.Substring(0, sqlCmd.Length - 1) + ") values (";
// Iterate the datatable columns
for (int x = 0; x < dataTableXml.Columns.Count; x++)
{
// Add the column value for this row
sqlCmd = sqlCmd + "'" + dr[x].ToString().Replace("'", "''") + "',";
}
sqlCmd = sqlCmd.Substring(0, sqlCmd.Length - 1) + ");";
}
// Create and execute the insert command
if (sqlCmd != "insert into [" + tableName + "] (")
{
OleDbCommand oledbCmd = new OleDbCommand(sqlCmd, oledbConn);
oledbCmd.ExecuteNonQuery();
}
}
}
catch
{
// If there are errors you will get this message
Message.Text = "There was an error adding the XML data to the table.";
}
finally
{
// Close the database connection
oledbConn.Close();
}
}