【问题标题】:struggling on getting XML from database to show on the form on a table or grid努力从数据库中获取 XML 以显示在表格或网格的表单上
【发布时间】:2015-11-25 09:00:21
【问题描述】:

我一直在努力检索我存储在 sql server 的 XML 字段中的 XML 数据,现在我需要在网页/表单中显示信息,以便为用户提供有关存储的报告 xml 数据的信息,以下是我的尝试,它抛出一个 index 0 异常

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Data;
using System.Xml.Linq;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        XmlDocument xdoc = new XmlDocument();
        SqlConnection cnn = null;
        SqlCommand cmd = null;



        try
        {
            cnn = new SqlConnection();
            cnn.ConnectionString = "ICDBConnectionString";
            cnn.Open();

            string selectQry = "WITH XMLNamespaces('TUReport' AS Debtor) SELECT* FROM TUReport WHERE ConsumerID = @ID FOR XML RAW('TUReport');";
            cmd = new SqlCommand(selectQry, cnn);
            int ID = Convert.ToInt32(txtIDNumber.Text);
            cmd.Parameters.AddWithValue("@ID", ID);

            DataSet dataset = new DataSet();

            //System.IO.StringReader xmlSR = new System.IO.StringReader(cmd)

            XmlReader reader = cmd.ExecuteXmlReader();

            dataset.ReadXml(reader, XmlReadMode.ReadSchema);

            results.DataSource = reader;
            results.DataBind();
            cnn.Close();


            //if (reader.Read())

            //    xdoc.Load(reader);


        }

        catch (Exception ex)
        {
            throw ex;
        }

        finally
        {
            cmd.Dispose();
            cnn.Close();
        }

    }
}

【问题讨论】:

    标签: c# asp.net sql-server linq


    【解决方案1】:

    SQL 中的“Select”和“”之间缺少一个空格。 (从 TUReport 中选择

    【讨论】:

    • catch 语句仍然向我抛出此错误。App_Web_1nvanvoq.dll 中发生“System.ArgumentException”类型的异常,但未在用户代码中处理附加信息:初始化字符串的格式不符合从索引 0 开始的规范。
    • 哪一行抛出异常? TUReport 中的 ConsumerID 是 varchar 吗?
    • 将字符串更改为 int,但在调试程序时仍然出现相同的错误
    猜你喜欢
    • 1970-01-01
    • 2021-05-02
    • 1970-01-01
    • 2021-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-23
    • 1970-01-01
    相关资源
    最近更新 更多