嗯,首先抱歉这个问题对你们来说一定非常直接,但是我在努力奋斗,我需要让它发挥作用:( 好吧,我正在尝试在我的应用程序上使用DataSet

当我渲染它时,我得到了:

The type 'System.Data.DataSet' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Data
我的应用程序中的

System.Data已经从C:\ Windows \ Microsoft.NET \ Framework \ v2.0.50727 \ System.Data.dll中引用

我正在使用我的使用条款

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;

此DataSet是来自Webservice的响应 那么关于如何解决这个问题的任何想法?

PS。我不知道它是否有帮助,但我正在使用nHaml来渲染我的视图

非常感谢


更新:

我现在找到的唯一解决方案是将DataSet传递给视图转换器,将DataSet传递给

<List<List<String>>

并像这样传递整个DataSet循环

List<List<String>> myList = new List<List<String>>();

foreach (DataRow row in dsTrades.Tables[0].Rows)
{
    List<String> myListColumns = new List<String>();

    for (var index = 0; index < dsTrades.Tables[0].Columns.Count; index ++)
    {
        myListColumns.Add(row[index].ToString());
    }

    myList.Add(myListColumns);
}

// THIS SHOULD BE THE DATASET AND NOW 
// IT'S CONVERTED TO A LIST WITH STRINGS LIST INSIDE
viewModel.Trades = myList; 

return View(viewModel);

其实这完全是疯了吗?

如果直接使用DataSet,可以轻松完成所有这些工作 我希望任何人都能以更简单的方式帮助我做到这一点

谢谢:)


更新(解决方案)

Simon的回答非常有效,它在为System.Data和System.Xml添加名称空间后第一次尝试起作用,但与此同时,Josh的回答提供了一种非常好的和酷的方式来使用DataSet,我认为工作得更好,我想我现在就会去做。

谢谢你的帮助

有帮助吗?

解决方案

尝试在nhaml配置中添加对System.Data的显式引用

<?xml version="1.0"?>

<configuration>
...
    <configSections>
            <section name="nhaml" type="NHaml.Configuration.NHamlConfigurationSection, NHaml"/>
    </configSections>
...
<nhaml autoRecompile="true">
            <assemblies>
                    <clear/>
                    ...
                    <add assembly="System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
            </assemblies>
            <namespaces>
                    <clear/>
                    ...
                    <add namespace="System.Data"/>
            </namespaces>
    </nhaml>

明显取代“......”与您的其他参考和配置

其他提示

我唯一能想到的是,在页面的上下文中,System.Data引用不可见。

尝试在web.config中添加命名空间:

<pages>
   <controls />
   <namespaces>
      <add namespace="System.Data"/>
   </namespaces>
</pages>

我知道这不是你问题的一部分,但我建议建立一个充满代表数据表中字段的属性的类。使用Linq,您可以轻松地将行转换为类对象并返回它们的列表。这是一些粗略(和未编译)的代码。

[Serializable]
public class MyClass
{
   public string Property1 { get; set; }
   public string Property1 { get; set; }
}

您希望它是可序列化的,因此您的Web服务可以将其作为xml或json返回(但是您将返回它)。 linq看起来像这样:

var result = from r in dataSet.Table[0].Rows.AsEnumerable()
             select new MyClass() {
                Property1 = r["Field1"].ToString()
                Property2 = r["Field2"].ToString()
             };

return result.ToList();

根据我的个人经验,DataSet往往是资源匮乏。此外,Linq将比你的for循环更有效。

希望这有帮助。

删除引用(system.Data)..并再次添加相同的引用..它可能正常工作..

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top