數(shù)據(jù)庫生成xml的兩個方法

字號:


    第一個示例方法:
    1 sqlconnection conn = new sqlconnection();
    2 conn.connectionstring = server=127.0.0.1;user
    id=sa;password=fdahgdrethj31313210212121;
    database=northwind;persist security info=true;
    3 conn.open();
    4 sqldataadapter da = new sqldataadapter(select * from 表, conn);
    5 sqlcommandbuilder thisbulder = new sqlcommandbuilder(da);
    6 dataset ds = new dataset();
    7 da.fill(ds);
    8 ds.writexml(@c:\temp.xml);
    第二個示例方法:
    1 private void writexmltofile(dataset thisdataset)
    2 {
    3 if (thisdataset == null) { return; }
    4 // create a file name to write to.
    5 string filename = myxmldoc.xml;
    6 // create the filestream to write with.
    7 system.io.filestream myfilestream = new system.io.filestream
    8 (filename, system.io.filemode.create);
    9 // create an xmltextwriter with the filestream.
    10 system.xml.xmltextwriter myxmlwriter =
    11 new system.xml.xmltextwriter
    (myfilestream, system.text.encoding.unicode);
    12 // write to the file with the writexml method.
    13 thisdataset.writexml(myxmlwriter);
    14 myxmlwriter.close();
    15 }