using (OleDbConnection cn = new OleDbConnection(@"Provider=Microsoft.Jet.OleDb.4.0;Data Source=E:\Czgy.mdb"))
{
cn.Open();
using (OleDbCommand cmd = new OleDbCommand())
{
cmd.Connection = cn;
cmd.CommandType = CommandType.Text;
XmlDocument xml = new XmlDocument();
//请将XML的路径替换为实际的路径
xml.Load(@"E:\baseDictionary.xml");
//取得所有的user
XmlNodeList xn = xml.GetElementsByTagName("baseDictionary");
//遍历所有的user
foreach (XmlElement xe in xn)
{
cmd.CommandText = string.Format("INSERT INTO baseDictionary([id],[parentid],[title],[code],[value],[isbuildin],[iscreatetb],[descr],[lineno],[createdat],[createdby],[createdbyname],[updatedat],[updatedby],[updatedbyname],[ip]) VALUES ('{0}', '{1}', '{2}', '{3}', '{4}', '{5}', '{6}', '{7}', '{8}', '{9}', '{10}', '{11}', '{12}', '{13}', '{14}', '{15}', '{16}')",
xe.ChildNodes[0].InnerText,
xe.ChildNodes[1].InnerText,
xe.ChildNodes[2].InnerText,
xe.ChildNodes[3].InnerText,
xe.ChildNodes[4].InnerText,
xe.ChildNodes[5].InnerText,
xe.ChildNodes[6].InnerText,
xe.ChildNodes[7].InnerText,
xe.ChildNodes[8].InnerText,
xe.ChildNodes[9].InnerText,
xe.ChildNodes[10].InnerText,
xe.ChildNodes[11].InnerText,
xe.ChildNodes[12].InnerText,
xe.ChildNodes[13].InnerText,
xe.ChildNodes[14].InnerText,
xe.ChildNodes[15].InnerText,
xe.ChildNodes[16].InnerText
);
//插入数据
cmd.ExecuteNonQuery();
}
}
}
Console.ReadKey();
}
------解决思路----------------------
绑定Access数据生成XML
protected void btnCreateXML_Click(object sender, EventArgs e)
{
OleDbConnection dataConn = CreateAccessCon();//创建数据库连接对象
Uri Path = Request.Url;//提供统一资源标识符URI
String ServerUrl = Path.ToString();//定义一个字符串标识URI路径
ServerUrl = ServerUrl.Substring(0, ServerUrl.LastIndexOf("/") + 1);
try
{
Literal1.Text = "已经在你的相同目录下创建了一下文件:<br/>";//生成XML文件成功给予如下提示
dataConn.Open();
//应用了指定的限制之后,按照 GUID 的指示从数据源返回架构信息
DataTable schemaTable = dataConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
for (int I = 0; I < schemaTable.Rows.Count; I++)
{
//创建数据适配器
OleDbDataAdapter dbAdapter = new OleDbDataAdapter("select * from [" + schemaTable.Rows[I].ItemArray[2].ToString() + "]", dataConn);
DataSet tableData = new DataSet();//创建数据集
dbAdapter.Fill(tableData, schemaTable.Rows[I].ItemArray[2].ToString());//填充数据集
//将DataSet数据及其架构填充到XML文件中
tableData.WriteXml(HttpContext.Current.Server.MapPath(schemaTable.Rows[I].ItemArray[2].ToString() + ".xml"));
Literal1.Text = Literal1.Text + "<a href='" + ServerUrl + schemaTable.Rows[I].ItemArray[2].ToString() + ".xml'>" + schemaTable.Rows[I].ItemArray[2].ToString() + ".xml</a><br/>";
}//codego.net/tags/11/1/
}
catch (Exception ex)
{
Label1.Text = ex.Message.ToString();
}
finally
{
dataConn.Close();
}
}