views:

53

answers:

2

I want to export xml file data to sql database table. Can anyone guide me for this?

+2  A: 

If it's an SQL Server, I already answered a similar Question. Have a look at the following posting:

http://stackoverflow.com/questions/3574836/net-how-to-insert-xml-document-into-sql-server/3574884#3574884

You can use that small c# part to store your data. You only have to amend the table and column fields.

class Program
{
    private static void SaveXmlToDatabase(DbConnection connection,
          XmlDocument xmlToSave)
    {
       String sql = "INSERT INTO xmlTable(xmlColumn) VALUES (@xml)";

       using (DbCommand command = connection.CreateCommand())
       {
          XPathNavigator nav = xmlToSave.CreateNavigator();
          string xml = nav.SelectSingleNode("/catalog/cd[title='Manowar']").InnerXml;

          command.CommandText = sql;
          command.Parameters.Add(
            new SqlParameter("@xml", SqlDbType.Xml) 
               {Value = new SqlXml(new XmlTextReader(xml
                           , XmlNodeType.Document, null)) });

          DbTransaction trans = connection.BeginTransaction();
          try
          {
             command.ExecuteNonQuery();
             trans.Commit();
          }
          catch (Exception)
          {
             trans.Rollback();
             throw;
          }
       }
    }

    static void Main(string[] args)
    {
        XmlDocument document = new XmlDocument();
        document.Load(args.First());

        SqlConnection connection = new SqlConnection(
            "Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;");

        SaveXmlToDatabase(connection, document);

        connection.Close();
    }
}
BitKFu
Is it possible to filter the fields from the xml file while passing it as the parameter to insert?
Geetha
you could add a xpath query to filter the xml file.
BitKFu
Can you please explain a bit more please.
Geetha
Yes, you can do that with the XPathNavigator for example.XPathNavigator nav = xmlToSave.CreateNavigator();string xml = nav.SelectSingleNode("/catalog/cd[title='Manowar']").InnerXml;
BitKFu
+2  A: 

check below link for this

http://www.simple-talk.com/sql/t-sql-programming/beginning-sql-server-2005-xml-programming/

you can find the solution.

KuldipMCA