I have an intranet web-based application that stores the information of added users in its database.
我有一个基于Web的Intranet应用程序,它将添加的用户的信息存储在其数据库中。
What I want to do is the following: when the user browses to the webstie, it will check if the user exists in its own database or not. If not, it is going to check his information using Active Directory.
我想要做的是:当用户浏览webstie时,它将检查用户是否存在于自己的数据库中。如果没有,它将使用Active Directory检查他的信息。
I wrote the method but the problem after checking the database and if the user is not there, it will go to the second method which is checking the organization code of the employee:
我在检查数据库之后编写了方法但问题,如果用户不在那里,它将转到检查员工组织代码的第二个方法:
public static bool isMember(string userid)
{
Employee employee = new Employee(userid);
if (!String.IsNullOrEmpty(userid))
{
string username = userid;
string connString = "Data Source=appServer\\sqlexpress;Initial Catalog=dbTest;Integrated Security=True";
string cmdText2 = "SELECT Count(*) FROM employee WHERE Username = @username";
using (SqlConnection conn = new SqlConnection(connString))
{
conn.Open();
// Open DB connection.
using (SqlCommand cmd = new SqlCommand(cmdText2, conn))
{
cmd.Parameters.Add("@Username", SqlDbType.VarChar);
cmd.Parameters["@username"].Value = username;
var count = (int)cmd.ExecuteScalar();
return count == 1; // return true if there's only one employee with given name
}
}
}
else if (employee.department.Code == "30003143") //The SapCode of department is "30003143"
return true;
else
return false;
}
So how to fix that? How to make the application goes through (else if) clause if the user does not exist in the database?
那么如何解决这个问题呢?如果数据库中不存在用户,如何使应用程序通过(else if)子句?
1 个解决方案
#1
1
Pretty easy:
挺容易:
public static bool isMember(string userid)
{
// guard clause - if "userid" is invalid, return "false" right away
if (String.IsNullOrEmpty(userid))
{
return false
}
//Object from Employee class
Employee employee = new Employee(userid);
string username = userid;
string connString = "Data Source=appServer\\sqlexpress;Initial Catalog=dbTest;Integrated Security=True";
string cmdText2 = "SELECT Count(*) FROM employee WHERE Username = @username";
using (SqlConnection conn = new SqlConnection(connString))
using (SqlCommand cmd = new SqlCommand(cmdText2, conn))
{
cmd.Parameters.Add("@Username", SqlDbType.VarChar);
cmd.Parameters["@username"].Value = username;
conn.Open();
var count = (int)cmd.ExecuteScalar();
if (count == 1)
{
return true; // return true if there's only one employee with given name
}
}
// if the user already existed in the database - then the above RETURN has
// returned "true" to the caller. So these lines are **only** executed if the
// user was NOT found in the database.
if (employee.department.Code == "30003143") //The SapCode of department is "30003143"
return true;
// now check in Active Directory here.....
return UserExistsInActiveDirectory();
}
#1
1
Pretty easy:
挺容易:
public static bool isMember(string userid)
{
// guard clause - if "userid" is invalid, return "false" right away
if (String.IsNullOrEmpty(userid))
{
return false
}
//Object from Employee class
Employee employee = new Employee(userid);
string username = userid;
string connString = "Data Source=appServer\\sqlexpress;Initial Catalog=dbTest;Integrated Security=True";
string cmdText2 = "SELECT Count(*) FROM employee WHERE Username = @username";
using (SqlConnection conn = new SqlConnection(connString))
using (SqlCommand cmd = new SqlCommand(cmdText2, conn))
{
cmd.Parameters.Add("@Username", SqlDbType.VarChar);
cmd.Parameters["@username"].Value = username;
conn.Open();
var count = (int)cmd.ExecuteScalar();
if (count == 1)
{
return true; // return true if there's only one employee with given name
}
}
// if the user already existed in the database - then the above RETURN has
// returned "true" to the caller. So these lines are **only** executed if the
// user was NOT found in the database.
if (employee.department.Code == "30003143") //The SapCode of department is "30003143"
return true;
// now check in Active Directory here.....
return UserExistsInActiveDirectory();
}