using MySql.Data.MySqlClient; using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Xml.Linq; namespace XML { class Program { static void Main(string[] args) { //Xml导入数据库 //读取XML XDocument xdoc = XDocument.Load(@"C:\Users\Administrator\Desktop\xml\stu.xml"); //根节点 XElement root = xdoc.Root; //遍历根节点下所有的子节点 foreach (var item in root.Elements()) { string sql = "INSERT INTO t_student(name,age,gender) VALUES(@name,@age,@gender);"; MySqlParameter[] ps = { new MySqlParameter("@name",item.Element("StuName").Value), new MySqlParameter("@age",item.Element("StuAge").Value), new MySqlParameter("@gender",item.Element("StuGender").Value) }; MySqlHelper.ExecuteNonQuery(sql,ps); } Console.WriteLine("数据导入成功!"); Console.ReadKey(); } } }
时间: 2024-12-03 09:17:25