问题描述
代码如下:usingSystem;usingSystem.Linq;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.Data.SqlClient;usingSystem.Data.SqlServerCe;usingSystem.Xml;usingSystem.Xml.Linq;namespacewxqiuyu_gp{publicpartialclassForm1:Form{publicForm1(){InitializeComponent();}//初始化函数privatevoidbutton1_Click(objectsender,EventArgse){try{SqlConnectioncnn=newSqlConnection("server=127.0.0.1;uid=sa;pwd=123456;database=mobileTest1;");//初始化数据库连接,并给定连接所需的登录属性cnn.Open();//根据所给属性初始化数据库连接SqlCommandcomm=newSqlCommand(@"SELECT*FROMtable1",cnn);//初始化数据库SQL操作实例DataSetds=newDataSet();//初始化本地数据缓存实例dsSqlDataAdapterda=newSqlDataAdapter();//填充DataSet,并且使用SQL命令更新数据库与数据连接da.SelectCommand=comm;//选择从数据库中获取username以及id的数据da.Fill(ds,"table1");//向ds中添加UserTable中匹配行的数据ds.WriteXml(@"MyDocumentsa.xml",XmlWriteMode.WriteSchema);//将ds中的数据写入本地a.xmlcnn.Close();cnn.Dispose();}//Pc端catch(SqlExceptionex){MessageBox.Show(ex.Message);//抛出异常}if(System.IO.File.Exists(@"MyDocumentsxml.sdf"))System.IO.File.Delete(@"MyDocumentsxml.sdf");SqlCeEngineeng=newSqlCeEngine(@"DataSource=MyDocumentsxml.sdf");//初始化本地数据库eng.CreateDatabase();//创建本地数据库SqlCeConnectionconn=newSqlCeConnection(@"DataSource=MyDocumentsxml.sdf");//初始化本地数据库连接,匿名登录conn.Open();//连接本地数据库SqlCeCommandcmndLocal=newSqlCeCommand();//初始化本地数据库SQL操作实例cmndLocal.Connection=conn;//并发连接cmndLocal.CommandText="CREATETABLETable_1(Idnchar(10),Namenchar(10),Gradenchar(10),Sexnchar(10))";//创建本地表,加入键值对username和namecmndLocal.ExecuteNonQuery();//返回受SQL命令影响的行数DataSetdsEx=newDataSet();//初始化本地数据缓存dsExdsEx.ReadXml(@"MyDocumentsa.xml",XmlReadMode.ReadSchema);//读取本地a.xml内的数据stringtabname="Table_1";stringseleStr="SELECT*FROMTable_1";//要执行的读取命令SqlCeDataAdaptersda=newSqlCeDataAdapter();//填充DataSet,并且使用SQL命令更新本地数据库与数据连接sda.SelectCommand=newSqlCeCommand(seleStr,conn);//从本地表中读取username以及id的数据newSqlCeCommandBuilder(sda);//初始化SqlCeCommandBuildersda.Fill(dsEx,tabname);//向dsEx填充Table_1的数据dsEx.Tables["Table_1"].Merge(dsEx.Tables["UserTable"]);//将UserTable中的数据添加到Table_1中sda.Update(dsEx,tabname);conn.Close();//关闭数据库conn.Dispose();//释放资源MessageBox.Show("数据下载成功");Dis_Formdisplay_form=newDis_Form();display_form.Show();this.Hide();}//移动端privatevoidbutton2_Click(objectsender,EventArgse){try{DataSetds=newDataSet();SqlCeConnectioncn=newSqlCeConnection(@"DataSource=MyDocumentsxml.sdf");cn.Open();SqlCeCommandcomm=newSqlCeCommand(@"SELECT*FROMTable_1",cn);SqlCeDataAdapterda=newSqlCeDataAdapter();da.SelectCommand=comm;da.Fill(ds,"Table_1");XmlWriterb=XmlWriter.Create(@"MyDocumentsb.xml");ds.WriteXml(@"MyDocumentsb.xml",XmlWriteMode.WriteSchema);cn.Close();cn.Dispose();}catch(SqlCeExceptionex){MessageBox.Show(ex.Message);}DataSetdsEx=newDataSet();dsEx.ReadXml(@"MyDocumentsb.xml",XmlReadMode.ReadSchema);SqlConnectionconn=newSqlConnection("server=127.0.0.1;uid=sa;pwd=123456789;database=mobileTest1;");conn.Open();stringtabname="UserTable";stringseleStr="SELECT*FROMdbo.UserTable";SqlDataAdaptersda=newSqlDataAdapter();sda.SelectCommand=newSqlCommand(seleStr,conn);newSqlCommandBuilder(sda);SqlCommandcmndLocal=newSqlCommand();cmndLocal.Connection=conn;cmndLocal.CommandText="deletefromUserTable";cmndLocal.ExecuteNonQuery();sda.Fill(dsEx,tabname);dsEx.Tables["usertable"].Merge(dsEx.Tables["Table_1"]);sda.Update(dsEx,tabname);conn.Close();conn.Dispose();MessageBox.Show("数据上传完成");}//上传privatevoidForm1_Load(objectsender,EventArgse){}privatevoidpictureBox1_Click(objectsender,EventArgse){//}}}F10报错出现在【dsEx.ReadXml(@"MyDocumentsa.xml",XmlReadMode.ReadSchema);//读取本地a.xml内的数据】这里。报错原因是找不到mydocumenta.xml但是上面创建a.xml语句没有问题啊。求解