2012-10-31 30 views
-1

我试图将SQL-Server查询导出到XML文件,并且我想将该文件导入到Access。我不明白如何做到这一点。import xml to mdb

这里是我用来生成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")); 
} 
+0

这不是你想要投入访问的查询,但结果集。为了导入访问,您必须以Access友好格式输出或通过另一个数据库连接直接连接到Access DB。 – 2012-10-31 18:23:48

+0

我有一个xml文件.. – userprofile1

+0

在Access中,尝试'外部数据' - >'XML文件',然后按照向导步骤将结果集导入到一个表中。 – 2012-10-31 18:38:39

回答

0

这可能是一个开始。

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

谢谢你的:) – userprofile1

+0

(SetAdded和SetModified只能在DataRows上调用不变的DataRowState。)现在我有这个错误。 。 – userprofile1

+0

解决了..感谢ebyrod – userprofile1

0

这一数据将只用编号和名称,使用该系统:使用系统 ;使用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; using 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(); 

      } 
     } 


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