快捷搜索:

C#将XML导入到ACCESS数据库

//请将ACCESS数据库的路径替换为实际的路径
using (OleDbConnection cn =new OleDbConnection(@"Provider=Microsoft.Jet.OleDb.4.0;Data Source=d:\demo.mdb"))
{
    cn.Open();
    
using (OleDbCommand cmd =new OleDbCommand())
    {
        cmd.Connection
= cn;
        cmd.CommandType
= CommandType.Text;
        
        XmlDocument xml
=new XmlDocument();
        
//请将XML的路径替换为实际的路径
        xml.Load(@"d:\demo.xml");

        
//取得所有的user
        XmlNodeList xn = xml.GetElementsByTagName("user");

        
//遍历所有的user
        foreach (XmlElement xe in xn)
        {
            cmd.CommandText
=string.Format("INSERT INTO userinfo([UserCode],[UserName],[UserSex],[UserAge]) VALUES ('{0}', '{1}', '{2}', {3})",
            xe.GetAttribute(
"code"),    //取得code
            xe.ChildNodes[0].InnerText, //取得Name
            xe.ChildNodes[1].InnerText, //取得Sex
            xe.ChildNodes[2].InnerText  //取得Age
            );

            
//插入数据
            cmd.ExecuteNonQuery();
        }
    }
}

您可能还会对下面的文章感兴趣: