public bool loginpro(string loginas, string dept, string usnm, string pass)
{
try
{
string qrstr;
qrstr = "select * from login where loginas=='" + loginas + "',dept=='" + dept + "',usnm=='" + usnm + "',pass=='" + pass + "'";
Gencon.Open();
SqlCommand cmd = new SqlCommand(qrstr, Gencon);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
Gencon.Close();
if (dt.Rows.Count > 0)
{
return true;
}
}
catch (Exception e)
{
return false;
}
}
답변
사용하여이 작업을 단순화 할 수있다. 물론 컴파일러가 컴파일시에 멈추지 만 런타임에 다른 오류가 발생합니다.
그래서 컴파일 시간 문제를 쉽게 해결할 수 있습니다. 당신이 어떤 행이 쿼리에 의해 반환하지 않는 경우 그냥 retrun 값을 쓰기 : 이제
// This returns true if you have rows, false if not
return (dt.Rows.Count > 0);
런타임에 직면하게 될 데 문제가
이- SQL의 동일한 연산자는 다음과 같다 = 하지
- 조건 논리 연산자 (AND, OR)에 의해 결합되어야하는 배수 ==
- SQL 텍스트 매개 변수가 있어야
.
public bool loginpro(string loginas, string dept, string usnm, string pass)
{
try
{
string qrstr;
qrstr = @"select * from login where [email protected] and dept = @dept
and usnm = @user and pass= @pass";
Gencon.Open();
SqlCommand cmd = new SqlCommand(qrstr, Gencon);
cmd.Parameters.Add("@login", SqlDbType.NVarChar).Value = loginas;
cmd.Parameters.Add("@dept", SqlDbType.NVarChar).Value = dept;
cmd.Parameters.Add("@user", SqlDbType.NVarChar).Value = usnm;
cmd.Parameters.Add("@pass", SqlDbType.NVarChar).Value = pass;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
Gencon.Close();
return (dt.Rows.Count > 0);
}
catch (Exception e)
{
Gencon.Close();
return false;
}
}
는 using statement를 사용하여 메모리 누수 및 보안 문제를 일으킬 수있는 명확한 text password to your database 엔진을 통과하려고하지 같은 다른 문제가 있습니다.
...
if (dt.Rows.Count > 0)
{
return true;
}
...
// what should be returned? true or false?
나는 이런 식으로 뭔가에 방법을 쓰기를 다시 제안 :
문제는 try 블록에 있습니다. 블록을 반환하면 DataTable
에 행이있을 때만 값을 반환합니다.
if (dt.Rows.Count > 0)
{
return true;
}
else
{
// has to return something.
return false;
}
또는 당신은 당신의 코드에서 많은 문제가 있습니다
return dt.Rows.Count > 0 ; // assuming in else you want to return false.
else 블록에서 예외를 throw하여 블록을 catch하려는 경우? – Swaroop
이 작동하면 그냥 예외가 발생합니다. –
오류의 직접적인 원인은 닷넷은 당신이 제로 행이있는 경우을 반환 할 값을 을 알 수 없다는 것입니다 :
public bool loginpro(string loginas, string dept, string usnm, string pass) {
//DONE: Make SQL readable; debug it ("=" instead of "==")
//DONE: Do not fetch redundant data (select * ...)
//DONE: Make SQL parametrized
String sql =
@"select 1
from login
where loginas = @prm_loginas and
dept = @prm_ dept and
usnm = @prm_user and
pass = @pass"; //TODO: do not store password, but its hash value
try {
//DONE: wrap IDisposable into using
//DONE: do not use global SQL connections Gencon.Open()...Gencon.Close()
using (SqlConnection con = new SqlConnection(connectionStringHere)) {
con.Open();
//DONE: wrap IDisposable into using
using (SqlCommand cmd = new SqlCommand(sql, con)) {
cmd.Parameters.Add("@prm_loginas", SqlDbType.NVarChar).Value = loginas;
cmd.Parameters.Add("@prm_ dept", SqlDbType.NVarChar).Value = dept;
cmd.Parameters.Add("@prm_user", SqlDbType.NVarChar).Value = usnm;
//TODO: do not pass password! Pass hash value instead
cmd.Parameters.Add("@pass", SqlDbType.NVarChar).Value = pass;
//DONE: wrap IDisposable into using
//DONE: do not fetch redundant data (you want at most one record only)
using (var reader = cmd.ExecuteReader()) {
return reader.Read(); // <- cursor has at least one record
}
}
}
}
catch (DbException ee) { //DONE: do not catch all the exceptions
return false;
}
}
Rows.Count가 0 일 경우 어떻게됩니까? 가치를 되찾았습니까? 컴파일러 (와 C# 규칙)는 이것에 만족하지 않습니다. – Steve
행이 없으면 false를 반환해야합니다. – Alex
그런데이 문제를 해결 한 후에는 쿼리가 여러 가지 이유로 완전히 잘못되었습니다. ==는 SQL이 아닌 C# 연산자입니다. 여러 조건을 논리 연산자로 결합해야하는 곳은 sql 텍스트가 Sql Injection 파티입니다. – Steve