Monday, December 5, 2011

Insert XML data into SQL Server 2008

 protected void Button1_Click(object sender, EventArgs e)   
  {   
       string EmpCode = null;   
       string City = null;   
       string State = null;   
       string Country = null;   
       string str = null;   
       SqlConnection con = new SqlConnection("Data Source=localhost;Initial Catalog=Test;Integrated Security=True");   
       XmlDocument document = new XmlDocument();   
       document.Load("E:\\Santosh\\EmpAddress.xml");   
       XmlNodeList xmlNodeList = document.SelectNodes("EmpAddress/Address");   
       foreach (XmlNode node in xmlNodeList)   
       {   
            EmpCode = node["EmpCode"].InnerText;   
            City = node["City"].InnerText;   
            State = node["State"].InnerText;   
            Country = node["Country"].InnerText;   
            str = "INSERT INTO AddressDetail (EmpCode,City,State,Country) values ('" + EmpCode + "', '" + City + "','" + State + "','" + Country + "')";   
            SqlCommand cmd = new SqlCommand();   
            cmd = new SqlCommand(str, con);   
            cmd.CommandType = CommandType.Text;   
            con.Open();   
            cmd.ExecuteNonQuery();   
            con.Close();   
       }   
  }    

1 comment:

  1. This is just the kind of information that I had been looking, Thank you very much for posting this blog its very nice.....

    MS Office Training in Dallas

    MS Office Training in Dallas

    ReplyDelete