如何在从 SQL 检索的 DataSet 中按名称引用 DataRow 元素?
因此,我使用 XmlWriter
和 DataSet
构建一些 XML,但是当需要循环 DataSet< 中的每个
DataRow
时, /code> 我无法弄清楚如何像“userid”这样的引用以及从存储过程返回的引用。 在页面代码中,我看到他们将其作为 Eval("userid")
或我使用相同存储过程的任何内容执行,但我现在在 ASHX 中使用它......请参阅“发生了什么”这里??' 在下面的代码中...
DataSet getData;
getData = SqlHelper.ExecuteDataset(ConfigurationManager.ConnectionStrings["connstr"].ConnectionString, CommandType.StoredProcedure, "Course_NewReportGet_Get_Sav", objPAra)
//COUNT NUMBER OF RESULTS FOR COUNT ATTRIBUTE (must add!)
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.IndentChars = (" ");
using(XmlWriter writer = XmlWriter.Create("data.xml", settings))
{
writer.WriteStartElement("changes");
writer.WriteAttributeString("clientname", foundCompany.CompanyName);
writer.WriteAttributeString("clientid", foundCompany.Abbreviation);
//writer... INSERT COUNT ATTRIBUTE
foreach(DataRow dr in getData.Tables)
{
writer.WriteStartElement("change");
writer.WriteStartElement("user");
writer.WriteAttributeString("userid", dr... WHAT GOES HERE??;
}
writer.WriteEndElement();
}
So I am building some XML using a XmlWriter
and a DataSet
but when it comes time to loop through each DataRow
in the DataSet
I can't figure out how do reference like "userid" and such that come back from the stored procedure. In page code I see them doing it as Eval("userid")
or whatever which I am using the same stored procedure, but I am using it in an ASHX now... see the 'WHAT GOES HERE??' in the code below...
DataSet getData;
getData = SqlHelper.ExecuteDataset(ConfigurationManager.ConnectionStrings["connstr"].ConnectionString, CommandType.StoredProcedure, "Course_NewReportGet_Get_Sav", objPAra)
//COUNT NUMBER OF RESULTS FOR COUNT ATTRIBUTE (must add!)
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.IndentChars = (" ");
using(XmlWriter writer = XmlWriter.Create("data.xml", settings))
{
writer.WriteStartElement("changes");
writer.WriteAttributeString("clientname", foundCompany.CompanyName);
writer.WriteAttributeString("clientid", foundCompany.Abbreviation);
//writer... INSERT COUNT ATTRIBUTE
foreach(DataRow dr in getData.Tables)
{
writer.WriteStartElement("change");
writer.WriteStartElement("user");
writer.WriteAttributeString("userid", dr... WHAT GOES HERE??;
}
writer.WriteEndElement();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
首先,你的 foreach 是错误的。 您需要循环
getData.Tables[0].Rows
。 如果有超过 1 个表,则需要循环 getData.Tables。但是,答案是它是一个索引属性。 因此,
dr["userId"]
将为您提供该值。First off, your foreach is wrong. You need to loop over
getData.Tables[0].Rows
. If there's more than 1 table, you need to loop over getData.Tables.But, the answer is it's an indexed property. So,
dr["userId"]
will get you the value.这非常简单。 数据行 (dr) 中的整数列可通过以下方式访问:
It is pretty straightforward. An integer column in your datarow (dr) would be accessed with:
你内心的 foreach 就是问题所在。
应该
Your inner foreach is the issue.
Should be
您将需要添加一个迭代级别 - 您当前正在
foreach(DataRow dr in getData.Tables)
中迭代表,DataRow 对象低于一级。You're going to want to add an iteration level - you're currently iterating over Tables in your
foreach(DataRow dr in getData.Tables)
, the DataRow objects are one level below.尝试使用 DataRow.Item 属性作为如下所示
Try using the DataRow.Item Property as shown below