无法导出到XML

时间:2014-01-29 22:54:39

标签: c# asp.net sql xml

我有一个sql compact数据库连接到我的网站Sort.sdf,其中有一个名为" 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);

1 个答案:

答案 0 :(得分:0)

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