Import Asp.net XML to SQL database

Source: Internet
Author: User

Protected void btnxml2_click (Object sender, eventargs E)
{
Dataset DS = dsreadxml (server. mappath ("DB. xml "));
This. dtlcontent. datasource = Ds. Tables [0];
This. dtlcontent. databind ();
String SQL = string. empty;
Foreach (datarow DR in DS. Tables [0]. Rows)
{
SQL + = "insert into IMG (";
For (INT I = 0; I <Ds. Tables [0]. Columns. Count; I ++)
{
SQL = SQL + Ds. Tables [0]. Columns [I]. columnname. tostring () + ",";
}
SQL = SQL. substring (0, SQL. Length-1) + ") values (";

For (INT x = 0; x <Ds. Tables [0]. Columns. Count; X ++)
{
SQL = SQL + "'" + Dr [X]. tostring (). Replace ("'", "'' ") + "',";
}

SQL = SQL. substring (0, SQL. Length-1) + ");";
}
Sqlcommand sqlinsertcommand = new sqlcommand ();
Sqlinsertcommand. commandtext = SQL;
Sqlinsertcommand. Connection = conn;
Conn. open ();
Sqlinsertcommand. executenonquery ();
Conn. Close ();
}

 

 

Public static dataset dsreadxml (string path)
{
Dataset DS = new dataset ();
Filestream FS = NULL;
Streamreader reader = NULL;
Try
{
FS = new filestream (path, filemode. Open, fileaccess. Read );
Reader = new streamreader (FS );
DS. readxml (Reader );
Return Ds;
}
Finally
{
FS. Close ();
Reader. Close ();
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.