将access数据库转换成XML文件

来源:互联网 发布:淘宝上靠谱的手机店 编辑:程序博客网 时间:2024/04/30 01:57

//按钮的事件

protected void Button1_Click(object sender, EventArgs e)
    {
      
        using (OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|/MmcDWZ.mdb"))
        {
            conn.Open();
            DataTable tablesName = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "Table" });
            //dataGridView1.DataSource = tablesName.DefaultView;
            conn.Close();
            try
            {
                string Mypath = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "myxmldata";
                for (int num = 0; num < tablesName.Rows.Count; num++)
                {
                    DataTable dt = new DataTable();
                    string AccessTableName = tablesName.Rows[num]["TABLE_NAME"].ToString().Trim();
                    //MessageBox.Show(Path.DirectorySeparatorChar + "xmldata" + Path.DirectorySeparatorChar);
                    OleDbCommand cmd = new OleDbCommand("select * from [" + AccessTableName + "]", conn);
                    OleDbDataAdapter dap = new OleDbDataAdapter(cmd);                 
                    dap.Fill(dt);
                    WriteTableToXml(AccessTableName, Mypath, dt, AccessTableName);
                    cmd.Dispose();
                }
                Response.Write("<script language='javascript'>alert('XML数据导出成功');</script>");
            }
            catch (Exception ex)
            {
                conn.Close();
                throw new Exception(ex.Message);
            }
        }
    }
     /// <summary>
        /// 将表中数据保存到文件夹folderName下的xml文件xmlName;
        /// </summary>
        /// <param name="xmlName">要保存的xml文件名称</param>
        /// <param name="folderName">文件所在路径,但是并不包括文件名称(Application.StartupPath + Path.DirectorySeparatorChar + "FolderName" + Path.DirectorySeparatorChar;)</param>
        /// <param name="dataTable">要保存的数据表</param>
        /// <returns>保存成功返回true,否则是false</returns>
        public static bool WriteTableToXml(string xmlFileName, string serverFilePath, DataTable dataTable, string tableName)
        {
            if (dataTable.TableName != tableName)
            {
                dataTable.TableName = tableName;
            }
            if (!serverFilePath.EndsWith("//"))
                serverFilePath += "//";
            if (!xmlFileName.EndsWith(".xml"))
                xmlFileName += ".xml";
            serverFilePath += xmlFileName;
            try
            {
                string directoryName = Path.GetDirectoryName(serverFilePath);//获取文件所在目录
                bool isExists=Directory.Exists(directoryName);//检查该目录是否存在
                if (!isExists)//不存在该文件目录,则创建
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(serverFilePath));
                }
                dataTable.WriteXml(serverFilePath, XmlWriteMode.WriteSchema);
                return true;
            }
            catch
            {
                return false; ;
            }
        }

原创粉丝点击