-1

我现在正在尝试将 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"));
}
4

2 回答 2

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);
    }
}
于 2012-11-01T16:05:10.670 回答
0

此数据仅适用于 ID 和名称,使用系统:使用 System;使用 System.Collections.Generic;使用 System.Linq;使用 System.Web;使用 System.Web.UI;使用 System.Web.UI.WebControls;使用 System.Data.Sql;使用 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();

            }
        }


        //--------------------------
于 2013-12-10T23:09:45.263 回答