在从数据库中获取XML以显示在表或网格上的表单上时遇到了困难
本文关键字:表单 遇到 网格 数据库 获取 XML 显示 | 更新日期: 2023-09-27 18:24:11
我一直在努力检索存储在sql server中的XML字段中的XML数据,现在我需要在网页/表单中显示信息,以便向用户提供有关存储的报告XML数据的信息,下面是我的尝试,它抛出了一个索引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();
}
}
}
SQL中"Select"answers"'之间缺少空格。(选择FROM TUReport)