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

发布于 2024-08-08 14:55:12 字数 1620 浏览 9 评论 0原文

好吧,首先对这个问题感到抱歉,这对你们来说一定是非常简单的,但我自己正在努力解决这个问题,我需要让它发挥作用:( 好吧,我尝试在我的应用程序上使用 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 子句中使用

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

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

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

非常感谢


更新:

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

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

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

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

谢谢:)


更新(解决方案)

西蒙的回答非常有效,并且在为 System.Data 和 System 添加命名空间后第一次尝试就成功了.Xml,但与此同时,乔什的回答提出了一种非常好的、很酷的使用数据集的方式,在我看来,这种方式效果更好,我想我现在就采用它。

谢谢你的帮助

Well, first of all sorry about this question it must be pretty straight forward for you guys but I'm struggling myself on it, and I need to make it work :(
Well I'm trying t o use DataSet on my application

and when I render it I got:

The type 'System.Data.DataSet' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Data

in my application System.Data is already being referenced from C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Data.dll

and I'm using on my using clauses as well

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

This DataSet is a response from a Webservice
So any Ideas on how to fix this problem?

PS. I don't know if it helps, but I'm using nHaml to render my view

Thanks a lot


UPDATE:

The only solution I found for now was to instead passing a DataSet to the view converter the DataSet to a

<List<List<String>>

and pass a loop through the entire DataSet like this

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

Actually this is completely crazy ins't it?

All this job could be easily done into the view if using DataSet directly
I hope anyone can help me with a more simplistic way to do it

Thank you :)


UPDATE (SOLUTION)

Simon's answer was really effective and it worked on the first try after adding namespaces for System.Data and System.Xml but at the same time, Josh's answer present a very nice and cool way to work with DataSets, which on my opinion works much better and I think I'll go for it now.

Thanks for you help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

肤浅与狂妄 2024-08-15 14:55:12

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

,显然用您的其他引用和配置替换“...”

try adding an explicit reference to System.Data in your nhaml configuration

<?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>

obviously replacing "..." with your other references and config

救星 2024-08-15 14:55:12

我唯一能想到的是,在页面上下文中,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 循环更高效。

希望这有帮助。

The only thing I could think of is that in the context of the page, the System.Data reference is not visible.

Try adding the namespace in your web.config:

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

I know it's not really part of your question, but I would recommend building a class filled with properties representing the fields in your datatable. Using Linq, you can easily convert your rows into the class object and return a list of them. Here's some rough (and uncompiled) code.

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

You want it to be serializable so your web service can return it as xml or json (however you are returning it). The linq would look something like this:

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

return result.ToList();

In my personal experience, DataSets tend to be resource hogs. Also, Linq will be more efficient than your for loops.

Hope this helps.

濫情▎り 2024-08-15 14:55:12

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

Delete the Reffrence(system.Data) ..and Add the same reffrence again .. it might be work..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文