Reputation: 17
I'm having a problem with my database being detected. I recently added a database as you can see in the image below and my query looks right (the sql below it is what the linq is going to replace) but I'm not sure why Visual Studio is not detecting my database. When I try to add
using System.Data.Entities
I can't find it and I'm not using it already.
My current usings:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using System.Data.SqlClient;
The following screenshot may help you understand what I'm trying to say (open the image in a new tab to view it at 100% scale):
Thanks for your help in advance!
Upvotes: 1
Views: 65
Reputation: 52311
You have a few issues:
You haven't added the model to your project. I don't see any ORM-related files in Solution Explorer for SqlIntegrationWithVs
.
Once you add the model, given that BloodBank
is not the property of Register
class; thus, it still won't exist in the current context.
In general, ORMs put table-mapped classes in a separate namespace, for example BloodBankDatabase.BloodBanks
, and objects which let you access table data are put in a specific class, so you'll end up with something like:
using BloodBankDatabase;
using (var context = new DatabaseContext(connectionString))
{
var returnedPerson = from loginDetails in BloodBank
where loginDetails.Username == usersInput
select loginDetails.Username;
}
Also, looking at your code, you don't seem to understand how C# work. For example:
You shouldn't call cmd.Dispose()
inside the using (SqlCommand cmd = ...) { }
, because using
already does that.
SqlDataReader
implements IDisposable
, so you've forgotten the using () { }
around it.
You should't catch Exception
.
where Username == usersInput
doesn't make sense in this context; are you sure you understand lambda expressions?
Don't bool usernameTaken = false;
. Instead, declare the variable as close as possible to the location where you use it first.
Validate your inputs before using them. What happens if userInput
is empty? Or extremely long?
How can you improve?
Stack Oveflow is not a code review website. Once you get your code working (and only then), you may be interested in submitting it to https://codereview.stackexchange.com/
You may also be interested in reading a book to learn C#. C# in Depth by Jon Skeet is de facto THE book any C# beginner should read.
If you work in a company, insist in doing code reviews for every line of committed code. Try to get more experienced developers to review your code. If not, find someone (a friend, an old colleague) who is ready to review your code.
Code analysis, available in paid versions of Visual Studio, will point out the issues like the forgotten using () { }
blocks. This is much easier than checking on MSDN every class you use to see whether it's implementing IDisposable
.
StyleCop is another tool which may interest you: it won't find errors per se, but will force you to use an uniform style across the code base.
Upvotes: 3