如何读取xml文件并插入数据库 [英] how to read xml files and insert into database

查看:72
本文介绍了如何读取xml文件并插入数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序,我需要读取xml文件并使用readxml()方法并将数据传输到数据库中。但是我遇到了一些问题。请帮我这样做

代码是这样的:



I am developing an application where i need to read xml file and using readxml() method and transfer data into the database. But I am getting some problem. Please help me to do this
Code is something like this:

DataSet ds = new DataSet("student");
        DataSet ds1 = new DataSet("student");
        private void button2_Click(object sender, EventArgs e)
        {

            
            SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=master;Integrated Security=true;");
            con.Open();
            SqlDataAdapter da = new SqlDataAdapter("select * from student", con);
            ds.ReadXml(textBox1.Text);
            da.Fill(ds1,"student");
            ds.Merge(ds1);
            da.Update(ds, "student");
            dataGridView1.DataSource = ds.Tables[0].DefaultView;
            MessageBox.Show("Updated Successfully.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

推荐答案

试试这个...



Try this ...

protected void button1_Click(object sender, EventArgs e)
   {
       SqlCommand command;
       SqlDataAdapter adpter = new SqlDataAdapter();
       DataSet ds = new DataSet();
       XmlReader xmlFile;
       string sql = null;
       string Product_Name = null;
       string path = MapPath("XMLFile.xml");
       xmlFile = XmlReader.Create(path, new XmlReaderSettings());
       ds.ReadXml(xmlFile);
       int i = 0;
       con.Open();
       for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
       {
           Product_Name = ds.Tables[0].Rows[i].ItemArray[1].ToString();
           sql = "insert into tab1 values(''" + Product_Name + "'')";
           command = new SqlCommand(sql, con);
           adpter.InsertCommand = command;
           adpter.InsertCommand.ExecuteNonQuery();
       }
       con.Close();
   }


看看这个..

http://csharp.net-informations.com/xml/xml-to-database.htm [ ^ ]
Have a look at this..
http://csharp.net-informations.com/xml/xml-to-database.htm[^]


这篇关于如何读取xml文件并插入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆