同时把六个表的内容导出成XML文档,带着结构,以便于以后导入。这是在access下调试成功的。如果是sql sever,只要把select字符串修改一下就好。也包括了连接ACCESS数据库的方法。
你测试时要自行建立一下表,然后把表名与字段名改了就好,转载请注明蚂蚁资讯网原创,如果有兴趣的话,点一下你感兴趣的广告连接。
private void accesstoxml()//把ACCESS数据库的内容转成XML文档
{
//定义一下生成的文件名
string xmlname = "myxml";
//连接数据库
OleDbConnection conn = null;
bool connet = false;//表示数据库没连接上
if (!connet)//如果没连上就连接
{
string loadSource = @"Jet OLEDB:Registry Path=;Data Source=" + path.currentpath + "\\mymdb.mdb" + ";Jet OLEDB:Database Password=1234567;Jet OLEDB:System database=;Jet OLEDB:Global Bulk Transactions=1;User ID=Admin;Provider=" + "Microsoft.Jet.OLEDB.4.0" + ";Jet OLEDB:Don’t Copy Locale on Compact=False;Jet OLEDB:SFP=False;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Engine Type=5;Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:Global Partial Bulk Ops=2;Extended Properties=;Mode=Share Deny None;Jet OLEDB:Create System Database=False;Jet OLEDB:Database Locking Mode=1";
// loadform.progressBar1.Value=40;
conn = new OleDbConnection(loadSource);
try
{
conn.Open();
connet = true;//连上了
//MessageBox.Show("connet");
}
catch (Exception e2)
{ MessageBox.Show(e2.Message.ToString()); }
connet = true;
}
//else//如果已经连上了就开始读写
if (conn != null)
{
try
{
DataSet ds = new DataSet();
OleDbCommand comm = null;
comm = new OleDbCommand(@"SELECT * FROM [Crew_training_recorder] where [toxml]=’a’", conn);
OleDbDataAdapter da = new OleDbDataAdapter();
da.SelectCommand = comm;
da.Fill(ds, "Crew_training_recorder");
comm = new OleDbCommand(@"SELECT * FROM [DeckLog] where [toxml]=’a’", conn);
da.SelectCommand = comm;
da.Fill(ds, "DeckLog");
comm = new OleDbCommand(@"SELECT * FROM [EngineLog] where [toxml]=’a’", conn);
da.SelectCommand = comm;
da.Fill(ds, "EngineLog");
comm = new OleDbCommand(@"SELECT * FROM [Ship_Maintenance_Recorder] where [toxml]=’a’", conn);
da.SelectCommand = comm;
da.Fill(ds, "Ship_Maintenance_Recorder");
comm = new OleDbCommand("SELECT * FROM [Ship_Particullar]", conn);
da.SelectCommand = comm;
da.Fill(ds, "Ship_Particullar");
comm = new OleDbCommand(@"SELECT * FROM [Trouble_shooting_Recorder] where [toxml]=’a’", conn);
da.SelectCommand = comm;
da.Fill(ds, "Trouble_shooting_Recorder");
read.toxml("Crew_training_recorder");
read.toxml("DeckLog");
read.toxml("EngineLog");
read.toxml("Ship_Maintenance_Recorder");
read.toxml("Ship_Particullar");
read.toxml("Trouble_shooting_Recorder");
if(File.Exists(path.currentpath.ToString().Trim() + @"\data\"+xmlname.Trim()+".xml"))
{
File.Delete(path.currentpath.ToString().Trim() + @"\data\"+xmlname.Trim()+".xml");
}
ds.WriteXml(path.currentpath.ToString().Trim() + @"\data\"+xmlname.Trim()+".xml", XmlWriteMode.WriteSchema);
comm.Dispose();
da.Dispose();
ds.Dispose();
}
catch (Exception e3)
{ MessageBox.Show(e3.Message.ToString()) ; }
conn.Close();
this.timer2.Enabled = false;
}
else
{
MessageBox.Show("The database is not connected,try again please.");
this.timer2.Enabled = false;
}
}