Reputation: 478
What is the equivalent of reader.Read() in Entity Framework which could be used in loops like the code snippet below.
public bool Login_Authentication(ref string Username, ref string Password)
{
try
{
SqlConnection Con = new SqlConnection(SQLDatabase_Connection.GetConnection());
Con.Open();
SqlCommand command = new SqlCommand("SelectLoginData", Con);
command.CommandType = CommandType.StoredProcedure;
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
employeeID = reader.GetInt32(1);
userName = reader.GetString(2);
passWord = reader.GetString(3);
string userType = reader.GetString(4);
if (userName == Username && Password == passWord)
{
if (userType == "admin")
{
adminLogIn();
break;
}
else if (userType == "employee")
{
employeeLogIn();
break;
}
}
}
Con.Close();
Con.Dispose();
}
catch (Exception ex)
{}
}
Now I have a table named TBL_PAYROLL, Entity framework class named HRMSEntities. I'm doing the following but its not working. Error Message: Error 5 Non-invocable member 'HRMSDataAccessLayer.HRMSEntities.TBL_PAYROLL' cannot be used like a method.
HRMSEntities hrmsDB = new HRMSEntities();
private void calculatePayrollManagement()
{
using (HRMSEntities context = new HRMSEntities())
{
foreach (TBL_PAYROLL user in context.TBL_PAYROLL())
{
// for every row there would be some calculation
// want to get data of every row in user variable
}
}
}
Upvotes: 0
Views: 975
Reputation: 6415
EntityFramework is a nice way of mapping your SQL Tables, Views and stored procedures into classes - you don't need to mess around with sql commands, or think in terms of how you'd handle things in sql.
So, for example, let's assume EF generated a context for you called 'myDataContext', and you have a class called 'Users' (created from a table called users).
You could write some linq like this:
var myAdmins = myDataContext.Users.Where(x=>x.UserType=="admin");
to create a list of all the users with a usertype of 'admin'.
Or to use your example (but taking note of the other comments about storing passwords in plain text is bad) ...
var userData = myDataContext.Users
.Where(x=>x.UserName.Equals(userName)
&& x.Password.Equals(passWord).FirstOrDefault();
if (userData==null)
{
// there is no user with this username/password combo ...
}
else
{
if (userData.UserType=="admin")
AdminLogin();
else if (userData.UserType=="employee"
EmployeeLogin();
}
Please note this isn't a great example! FirstOrDefault() is making sure that only one result is returned to userData, otherwise it would be a list. But if you are getting more than one user returned with that username/password then your data is in seriously bad shape! You could 'Single' to demand that only one result be returned, and otherwise throw an exception. But anyway, this is just a taster of some linq.
Upvotes: 1
Reputation: 151720
You don't have to. Entity Framework handles reading SQL data and populating entities.
using (var context = new YourContext())
{
foreach (var user in context.SelectLoginData())
{
// user is an instance of User,
// containing all properties filled from table
}
}
Also your login stored procedure seems to be a bit ... sub-optimal. You simply select all users from the database and compare them in code, not at all leveraging the functionality SQL delivers. You also seem to store passwords as plain text, which is far from advisable.
Upvotes: 1