请问如何从数据库中读取一个存储过程并把查询结果生成一个xml文件(c#)
create proc [dbo].[iSno_Select]
@sno nchar(7)
as
select sname,ssex,sage,sdept
from student
where sno=@sno
出一个查询结果,我在.Net里调用这个存储结果
string sno = TextBox1.Text.Trim();
string conStr = "Data Source=PC-20110624IRHP\\SQLEXPRESS;Initial Catalog=";
conStr = conStr + "students2;Integrated Security=sspi";
SqlConnection Con = new SqlConnection(conStr);
Con.Open();
string s = "iSno_Select '" + sno + "'";
SqlCommand cmd = new SqlCommand(s, Con);
SqlDataReader dr = cmd.ExecuteReader();
XmlDocument objDom=new XmlDocument();
之后我应该怎么写,包括如何创建节点,如何把数据写入到节点中。谢谢~我是个新手 展开
这个是我写的代码,你把相应的名称改一下就可以了:
==========================================================
using System;
using System.Data;
using System.Data.SqlClient;
namespace WriteXML
{
class Program
{
static void Main(string[] args)
{
// connection string
string connString = @"
server = .\sql2005express;
integrated security = true;
database = northwind";
string qry = @"
select
productname,
unitprice
from
products ";
SqlConnection conn = new SqlConnection(connString);
try
{
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand(qry, conn);
conn.Open();
DataSet ds = new DataSet();
da.Fill(ds, "products");
ds.WriteXml(@"C:\Documents and Settings\Jackey Tine\My Documents\Projects\productstable.xml");
Console.WriteLine("The file is Created");
}
catch (Exception e)
{
Console.WriteLine("Error: " + e);
}
finally
{
conn.Close();
}
}
}
}
=================================================================
显示效果如下:
SqlDataAdapter da=new SqlDataAdapter(cmd,conn);
Dataset ds=new Dataset();
da.fill(ds);
ds.WriteXml(多个重载方法,自己找);
WriteXml(url); 生成xml文件。
ReadXml(url); 读取xml 文件。