代码之家  ›  专栏  ›  技术社区  ›  zanona oldmanwiggins

ASP.NET MVC-System.Data.DataSet未引用问题

  •  1
  • zanona oldmanwiggins  · 技术社区  · 15 年前

    首先,对于这个问题我很抱歉,你们肯定是直截了当的,但我在努力,我需要让它发挥作用:( 我正在尝试在我的应用程序上使用数据集

    当我呈现它时,我得到:

    The type 'System.Data.DataSet' is defined in an assembly that is not referenced. You must add a reference to assembly '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;
    

    此数据集是来自WebService的响应 关于如何解决这个问题有什么想法吗?

    我不知道是否有帮助,但我正在使用nhaml呈现我的视图

    谢谢


    更新:

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

    <List<List<String>>
    

    像这样在整个数据集中传递一个循环

    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);
    

    事实上这是完全疯狂的,不是吗?

    如果直接使用数据集,所有这些工作都可以轻松地在视图中完成。 我希望有人能用一种更简单的方法来帮助我

    谢谢:


    更新(解决方案)

    Simon的答案非常有效,在为System.Data和System.XML添加名称空间后,它第一次成功了,但同时,Josh的答案提供了一种非常好的、很酷的数据集处理方法,在我看来,这种方法更有效,我想我现在就开始使用它。

    谢谢你的帮助

    3 回复  |  直到 15 年前
        1
  •  3
  •   Simon Randy Burden    15 年前

    尝试在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>
    

    显然,将“…”替换为其他引用和配置

        2
  •  1
  •   Josh    15 年前

    我唯一能想到的是,在页面的上下文中,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();
    

    根据我的个人经验,数据集往往是资源的霸主。而且,Linq比for循环更有效。

    希望这有帮助。

        3
  •  0
  •   San    15 年前

    删除引用(system.data)..并再次添加相同的引用。可能是工作……