C#查询数据库把结果输出到XML的例子

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.SqlClient; using System.Data; namespace ConsoleApplication4 { class Program { static void Main(string[] args) { //sqlserver身份验证 //string sqlconn = "server=(local);database=keede1228;user id=sa;password=123;"; //windows身份验证 string sqlconn = "server=(local);database=keede1228;integrated security=SSPI;"; string select = "dirr4"; SqlConnection conn = new SqlConnection(sqlconn); Console.WriteLine("33"); conn.Open(); Console.WriteLine("11"); //SqlCommand cmd = new SqlCommand(select, conn); //SqlDataReader reader = cmd.ExecuteReader(); //while (reader.Read()) //{ // Console.WriteLine("cityid:{0} city:{1} area:{2}", reader[0], reader[1], reader[2]); //} SqlDataAdapter da = new SqlDataAdapter(select, conn); DataSet ds = new DataSet(); da.Fill(ds, "custo"); foreach (DataRow row in ds.Tables["custo"].Rows) Console.WriteLine("'{0}'from {1}", row[0], row[1]); ds.WriteXml("write.xml", XmlWriteMode.WriteSchema); conn.Close(); Console.WriteLine("22"); Console.ReadKey(); } } }

 

 

DataSet类太强大了!

你可能感兴趣的:(xml,数据库,String,C#,database,dataset)