Does the name "Database" not exist in the current context?

I get this error when I try to launch my website using WebMatrix. I have a .cs file that makes a call to var db = Database.Open("dbase"); .

I have a database in my project called "dbase". I do not have a web.config file and there is no import to use WebMatrix packages, since I run the site using WebMatrix, so I do not think that I need it. Do I need to wrap the code with Razor tags, for example @{var db = Database.Open("dbase"); } @{var db = Database.Open("dbase"); } ? It also causes an error for me.

What could be the reason for this? Does anyone have solutions for this?

+7
source share
4 answers

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

 using WebMatrix.Data; 

This will import the Database class so that you can use it without fully defining the name.

It’s not clear why you think that you don’t need any “import” ones (I suppose you mean using directives like the ones above), but if it is in a regular C # file, then you definitely need them (or you need to fully qualify type names, which is ugly).

+5
source

I ran into this problem when I was browsing w3schools stuff on ASP.NET.

Basically, the answers above are correct: you need a WebMatrix.Data assembly (DLL), but commentators do not tell you how to fix this problem. Here's how:

First, copy the WebMatrix.Data.dll file to your / bin folder in your site.

If you don’t know where to get it, you can create WebMatrix a new project using a template with a database support - say, Bakery - and pull it out of this project bin folder. Or you can search a file on your hard drive. I have a copy in C:\Program Files\Microsoft ASP.NET\ASP.NET Web Pages\v2.0\Assemblies .

Then, on the ASP.NET page, import the assembly.

This is kind of a bad idea for a site that you will need 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 product page. It should look something like this:

 @using WebMatrix.Data; @{ var db = Database.Open("SmallBakery"); var selectQueryString = "SELECT * FROM Product ORDER BY Name"; } 

Now he should recognize the symbol "Database", and everything will be fine.

+7
source

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

+6
source

In my case, I had a set of nuggets installed, but it did not find WebMatrix.Data. The problem was that I created a new project, instead I just created a website (file / new / WEBSITE ), then the database was found by default (I think this is because of the type of project I created in first time)

Now it works fine, hope this helps someone.

0
source

All Articles