Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将xml导入mdb_C#_Asp.net - Fatal编程技术网

C# 将xml导入mdb

C# 将xml导入mdb,c#,asp.net,C#,Asp.net,我现在正试图将SQL Server查询导出到XML文件,并希望导入该文件以访问。我不知道该怎么做 以下是我用来生成XML的代码: protected void Button1_Click(object sender, EventArgs e) { SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["SqlSconn"].ConnectionString); con.Open()

我现在正试图将SQL Server查询导出到XML文件,并希望导入该文件以访问。我不知道该怎么做

以下是我用来生成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"));
}
这可能是一个开始

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);
    }
}

此数据将仅使用ID和名称,使用系统: 使用制度;使用System.Collections.Generic;使用System.Linq;使用System.Web;使用System.Web.UI;使用System.Web.UI.WebControl;使用System.Data.Sql;使用System.Data.SqlClient;使用系统配置;使用系统数据;使用System.IO; 使用System.Xml.Linq

//将DatabaseBackup.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();

            }
        }


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

它不是要放入access的查询,而是一个结果集。要导入access,您必须以access友好格式输出,或通过另一个数据库连接直接连接到access DB。在access中,请尝试“外部数据”->“XML文件”,然后按照向导步骤将结果集导入到单个表中。是的,我知道这一点,但我需要使用代码来执行此操作,因为我将在web应用程序上使用它:S(SetAdded和SetModified只能在DataRowState未更改的DataRows上调用。)现在我遇到了此错误。。