에 로그인하기위한 간단한 api를 만들려고 시도하지만 데이터가 데이터베이스에없는 경우에도 내 검사 조건은 true
을 반환합니다. 여기 login.cs
C# rest에서 코드화가 거짓 일 때 true를 반환하면
public string username { get; set; }
public string password { get; set; }
public string AddEmployees(Login Emp)
{
try
{
SqlDataReader reader = null;
SqlCommand com;
SqlConnection con = new SqlConnection(@"Data Source=DOTNET;Initial Catalog=edin;Integrated Security=True;Pooling=False");
com = new SqlCommand("Select count(*) from data where name='" + username + "' and phone='" + password + "'", con);
con.Open();
reader = com.ExecuteReader();
if (reader.Read())
{
return "success";//returns this always
}
else
{
return "error";
}
con.Close();
}
catch (Exception aa)
{
return aa.ToString();
}
}
및 내 모델 내 잘못이다 Controller
public string AddEmployees(Login Emp)
{
Login employe = new Login();
var response = employe.AddEmployees(Emp);
return response;
}
..? 또는 일부 코드가 누락되었습니다. 아무도 도와주세요
목록에 넣은 다음 계산하면 아무 것도 발견되지 않더라도 카운트가 0을 반환 할 가능성이 있습니까? –
'SELECT COUNT (*) ...'는 항상 값 (0 또는 1)을 반환하므로'if (reader.Read())'는 항상 'true'가됩니다. –
ExecuteScalar가 이 경우 ! – mybirthname