Wednesday, January 9, 2013

Insert XML data into SQL Server 2008

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();
}
}

No comments:

Post a Comment