将xml导入到mdb

时间:2012-10-31 18:16:27

标签: c# asp.net

我正在尝试将SQL-Server查询导出到XML文件,我想将该文件导入Access。我不明白该怎么做。

以下是我用来生成XML的代码:

protected void Button1_Click(object sender, EventArgs e) {
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["SqlSconn"].ConnectionString);
    con.Open();

    string strSQL = "select * from dbo.table_"+test.Text.ToString()+"";
    SqlDataAdapter dt = new SqlDataAdapter(strSQL, con);

    DataSet ds = new DataSet();
    dt.Fill(ds, "" + test.Text.ToString() + "");
    ds.WriteXml(Server.MapPath("temp.xml"));
}

2 个答案:

答案 0 :(得分:1)

这可能是一个开始。

static void SaveToMDB(DataSet ds, string strMDBFile)
{
    OleDbConnection cAccess = new OleDbConnection(
        "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" + strMDBFile);
    cAccess.Open();

    foreach (DataTable oTable in ds.Tables)
    {
        OleDbCommand oCommand = new OleDbCommand(
            "DROP TABLE [" + oTable.TableName + "]", cAccess);
        try
        {
            oCommand.ExecuteNonQuery();
        }
        catch (Exception) { }

        string strCreateColumns = "";
        string strColumnList = "";
        string strQuestionList = "";
        foreach (DataColumn oColumn in oTable.Columns)
        {
            strCreateColumns += "[" + oColumn.ColumnName + "] VarChar(255), ";
            strColumnList += "[" + oColumn.ColumnName + "],";
            strQuestionList += "?,";
        }
        strCreateColumns = strCreateColumns.Remove(strCreateColumns.Length - 2);
        strColumnList = strColumnList.Remove(strColumnList.Length - 1);
        strQuestionList = strQuestionList.Remove(strQuestionList.Length - 1);

        oCommand = new OleDbCommand("CREATE TABLE [" + oTable.TableName
            + "] (" + strCreateColumns + ")", cAccess);
        oCommand.ExecuteNonQuery();

        OleDbDataAdapter da = new OleDbDataAdapter(
            "SELECT * FROM [" + oTable.TableName + "]", cAccess);
        da.MissingSchemaAction = MissingSchemaAction.Add;
        da.FillLoadOption = LoadOption.OverwriteChanges;

        da.InsertCommand = new OleDbCommand(
            "INSERT INTO [" + oTable.TableName + "] (" + strColumnList
            + ") VALUES (" + strQuestionList + ")", cAccess);
        foreach (DataColumn oColumn in oTable.Columns)
        {
            da.InsertCommand.Parameters.Add(
                oColumn.ColumnName,
                OleDbType.VarChar,
                255,
                oColumn.ColumnName
                );
        }

        foreach (DataRow oRow in oTable.Rows)
            oRow.SetAdded();
        da.Update(oTable);
    }
}

答案 1 :(得分:0)

此数据仅适用于使用系统的ID和名称: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data.Sql; using System.Data.SqlClient;使用System.Configuration;使用System.Data;使用System.IO; 使用System.Xml.Linq;

//将SelectiveDatabaseBackup.xml导入test9表

        string connectionString = ConfigurationManager.ConnectionStrings["ApplicationServices3"].ConnectionString;
        SqlConnection sqlConnection1 = new SqlConnection(connectionString);
        SqlCommand cmd = new SqlCommand();
        cmd.CommandType = CommandType.Text;



        DataSet ds = new DataSet();

        ds.ReadXml(XDocument.Load("c:/d/SelectiveDatabaseBackup.xml").CreateReader());


        foreach (DataTable table in ds.Tables)
        {

            //Create table

            foreach (DataRow row in table.Rows)
            {


                string name = row[1].ToString();
                string id = row[0].ToString();
                cmd.CommandText = "INSERT  test9  VALUES ('"+ id +"','"+ name + "')";
                cmd.Connection = sqlConnection1;
                sqlConnection1.Open();
                cmd.ExecuteNonQuery();
                sqlConnection1.Close();

            }
        }


        //--------------------------