如何在asp.net c#中将sql查询记录或响应转换为xml文件 [英] how do I convert a sql query record or response to xml file in asp.net c#

查看:78
本文介绍了如何在asp.net c#中将sql查询记录或响应转换为xml文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在实现一个获取sql选择命令查询的服务,并将其转换为关于所选查询的xml文件。

任何代码或示例请?

I am implementing a service that get a sql "Select command"query and transform it to xml file with respect to the selected query.
any code or example please?

推荐答案

为什么要使用C#? SQL Server可以为您提供查询的XML版本 - http://msdn.microsoft.com/en -us / library / ms178107.aspx [ ^ ]
Why using C#? SQL server can provide you with a XML version of the query - http://msdn.microsoft.com/en-us/library/ms178107.aspx[^]


您需要输出sql查询到xml.if的内容,以便您可以使用以下代码。





What you need an output of an sql query to xml.if so you can use the below code.


using (System.Data.SqlClient.SqlConnection c = new SqlConnection(ConfigurationManager.ConnectionStrings["connectionstringname"].ConnectionString))
    using (System.Data.SqlClient.SqlCommand cmd = c.CreateCommand())
    {
        cmd.CommandText = "yourstoredprocedure";
        cmd.CommandType = CommandType.StoredProcedure;

        c.Open();

        System.Xml.XmlReader r = cmd.ExecuteXmlReader();
        XmlDocument document = new XmlDocument();
        document.Load(r);

        Response.ContentType = "text/xml";
        document.Save(Response.Output);

        c.Close();
    }



}


}


这篇关于如何在asp.net c#中将sql查询记录或响应转换为xml文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆