开发者

How to export xml to database - asp.net

开发者 https://www.devze.com 2023-01-13 09:28 出处:网络
开发者_如何学编程I want to export xml file data to sql database table. Can anyone guide me for this?If it\'s an SQL Server, I already answered a similar Question. Have a look at the following posting:

开发者_如何学编程I want to export xml file data to sql database table. Can anyone guide me for this?


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

.NET: How to insert XML document into SQL Server

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消