The name 'Database' does not exist in the current context?

强颜欢笑 提交于 2019-12-03 14:03:50

You need a reference to the WebMatrix.Data.dll assembly (which you probably have) and you also need a using directive for the WebMatrix.Data namespace:

using WebMatrix.Data;

That will import the Database class so you can use it without fully-qualifying the name.

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 do need them (or you need to fully-qualify type names, which is ugly).

I ran into this issue when I was going through the w3schools stuff on ASP.NET.

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:

First, copy the file WebMatrix.Data.dll into your site's /bin folder.

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.

Then, in your ASP.NET page, import the assembly.

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;
@{
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.

Daniel Magana

You just need to get this "Microsoft.AspNet.WebPages.WebData" from the NuGet Gallery.

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)

Now it is working fine, hopefully this will help someone.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!