2012-10-31 28 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(); 

      } 
     } 


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