C# 当前上下文中不存在名称“数据库”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9547158/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The name 'Database' does not exist in the current context?
提问by Simon Kiely
I am getting this error when I try to launch my website using WebMatrix. I have a .cs file which makes a call var db = Database.Open("dbase");.
当我尝试使用 WebMatrix 启动我的网站时出现此错误。我有一个 .cs 文件可以拨打电话var db = Database.Open("dbase");。
I have a database in my project called "dbase". I have no web.config file and no imports for using WebMatrix packages since I am launching the site using WebMatrix, so I don't believe I should need them. Do I need to wrap the code in Razor tags, like @{var db = Database.Open("dbase"); }? This causes an error for me too.
我的项目中有一个名为“dbase”的数据库。我没有 web.config 文件,也没有使用 WebMatrix 包的导入,因为我使用 WebMatrix 启动站点,所以我认为我不需要它们。我是否需要将代码包装在 Razor 标签中,例如@{var db = Database.Open("dbase"); }?这对我来说也是一个错误。
What could be causing this? Does anyone have any solutions for this?
什么可能导致这种情况?有没有人对此有任何解决方案?
采纳答案by Jon Skeet
You need a reference to the WebMatrix.Data.dllassembly (which you probably have) and you alsoneed a usingdirective for the WebMatrix.Datanamespace:
您需要对WebMatrix.Data.dll程序集(您可能拥有)的引用,并且还需要命名空间的using指令WebMatrix.Data:
using WebMatrix.Data;
That will import the Databaseclass so you can use it without fully-qualifying the name.
这将导入Database该类,因此您无需完全限定名称即可使用它。
It's not clear why you think you wouldn't need any "imports" (by which I assume you mean using directives like the one above) but if this is in a plain C# file, then you certainly doneed them (or you need to fully-qualify type names, which is ugly).
为什么你认为你不需要任何“进口”(由我假设你的意思是使用类似上面的指令),目前尚不清楚,但如果这是一个普通的C#文件,那么你肯定做需要他们(或者你需要完全限定的类型名称,这很丑陋)。
回答by Matthew Lowe
I ran into this issue when I was going through the w3schools stuff on ASP.NET.
我在 ASP.NET 上浏览 w3schools 的东西时遇到了这个问题。
Basically, the above answers are correct: you need the assembly (DLL) WebMatrix.Data, but the commenters don't tell you how to fix the problem. Here's how:
基本上,上面的答案是正确的:您需要程序集(DLL)WebMatrix.Data,但评论者没有告诉您如何解决问题。就是这样:
First, copy the file WebMatrix.Data.dll into your site's /bin folder.
首先,将文件 WebMatrix.Data.dll 复制到您站点的 /bin 文件夹中。
If you're not sure where to get it, you can have WebMatrix create a new project using a database-enabled template -- say, Bakery -- and get it out of that project's bin folder. Or you can search your hard drive for the file. I have a copy in C:\Program Files\Microsoft ASP.NET\ASP.NET Web Pages\v2.0\Assemblies.
如果您不确定从哪里获取它,您可以让 WebMatrix 使用启用数据库的模板创建一个新项目——比如 Bakery——并将其从该项目的 bin 文件夹中取出。或者,您可以在硬盘驱动器中搜索该文件。我在C:\Program Files\Microsoft ASP.NET\ASP.NET Web Pages\v2.0\Assemblies.
Then, in your ASP.NET page, import the assembly.
然后,在您的 ASP.NET 页面中,导入程序集。
This is kind of a bad idea for a site you're going to have to maintain for a long time, but for the purposes of this demo, you just need to add @using WebMatrix.Data;to the top of your products page. It should end up looking something like this:
对于您将不得不长期维护的站点来说,这是一个坏主意,但出于本演示的目的,您只需要添加@using WebMatrix.Data;到产品页面的顶部即可。它最终应该看起来像这样:
@using WebMatrix.Data;
@{
var db = Database.Open("SmallBakery");
var selectQueryString = "SELECT * FROM Product ORDER BY Name";
}
Now it should recognize the symbol "Database", and all will be well.
现在它应该识别符号“数据库”,一切都会好起来的。
回答by Daniel Magana
You just need to get this "Microsoft.AspNet.WebPages.WebData" from the NuGet Gallery.
您只需要从 NuGet 库中获取此“Microsoft.AspNet.WebPages.WebData”。
回答by DHLopez
In my case I had the nugget package installed but it was not finding the WebMatrix.Data. The problem was that I created a new project, instead I just created a website (file/new/WEBSITE), then the Database is found by default (I guess it's because of the type of project I created the first time)
就我而言,我安装了 nugget 包,但没有找到 WebMatrix.Data。问题是我创建了一个新项目,而不是我只是创建了一个网站(file/new/ WEBSITE),然后默认找到了数据库(我猜是因为我第一次创建的项目类型)
Now it is working fine, hopefully this will help someone.
现在它工作正常,希望这会对某人有所帮助。

