【问题标题】:Cant Export to XML无法导出到 XML
【发布时间】:2014-02-22 01:04:37
【问题描述】:

我有一个名为Sort.sdf 的sql 紧凑型数据库连接到我的网站,并带有一个名为“sort”的表。我需要将表中的所有记录导出到 XML 文件,但我不断收到以下错误并且找不到解决方案:

A network-related or instance-specific error occurred while 
establishing a connection to SQL Server. The server was not 
found or was not accessible. 

Verify that the instance name is correct and that SQL Server
is configured to allow remote connections. (provider: Named 
Pipes Provider, error: 40 - Could not open a connection to 
SQL Server)

我已经使用相同的连接成功地将数据插入到数据库中,所以我认为那里没有问题。

SqlCeConnection conn = new SqlCeConnection("Data Source=|DataDirectory|Sort.sdf");
conn.Open();

DataSet ds = new DataSet();
System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter("Select * from Sort", conn.ConnectionString);
da.Fill(ds); //error on this line

ds.WriteXml(@"c:\temp\output.xml", XmlWriteMode.WriteSchema);

【问题讨论】:

  • 你需要一个 SqlCeDataAdapter 。 conn 从未在这里使用过。
  • 谢谢问题解决了:)

标签: c# asp.net sql xml


【解决方案1】:
SqlCeConnection cnn = new SqlCeConnection("Data Source=|DataDirectory|Sort.sdf");
cnn.Open();
SqlCommand cmd = new SqlCommand("select * from  sort", cnn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds=new DataSet();
da.Fill(ds);

StreamWriter xmldoc=new StreamWriter(Server.MapPath("~/Testdo.xml"), false); 
ds.WriteXml(xmldoc);
xmldoc.Close();

【讨论】:

  • 该代码有什么问题?查看您的应用程序存储区域,它将在那里生成
猜你喜欢
  • 2019-04-08
  • 1970-01-01
  • 2020-01-15
  • 2012-06-15
  • 1970-01-01
  • 2017-04-12
  • 2019-02-13
  • 1970-01-01
  • 2021-10-08
相关资源
最近更新 更多