별칭 및 매개 변수를 사용하는 MySQL Select Where 문에 문제가 있습니다. 내 문제는 진술의 Where 부분에 있습니다. 그대로, 매개 변수를 사용하려고하면 결과가 반환되지 않습니다. 문제MySql 쿼리 - 별칭 및 매개 변수를 선택하십시오.
문은 다음과 같습니다 올바른 매개 변수와이 쿼리를 실행
이SELECT postcode, suburb, streetname, categorycode, DATE_FORMAT(dateRecorded, '%d/%m/%Y') AS Expr1, DATE_FORMAT(dateLastModified, '%d/%m/%Y') AS Expr2, status FROM incidentdetails WHERE (postcode = @postcode) AND (suburb = @suburb) AND (categorycode = @categorycode) AND (status = @status)
우측 열,하지만 데이터를 반환합니다.
전체적으로 where 절을 제거하면 전체 테이블을 예상대로 가져옵니다.
WHERE (postcode = @postcode) AND (suburb = @suburb) AND (categorycode = @categorycode) AND (status = @status)
에서 WHERE (postcode = 4020)
까지 where 절을 변경하면 예상대로 작동합니다.
WHERE 절을 (postcode = @postcode)로 바꾸고 매개 변수를 전달하면 (아래 참조) 작동하지 않습니다. 서버 탐색기 SQL 명령에서 실행할 때
sqlFillRelated.Parameters.AddWithValue("@postcode", int.Parse(PostcodeTxtBox.Text.ToString()))
(매개 변수) 전체 쿼리가 성공적으로 작동합니다.
string sqlFILL = "SELECT postcode, suburb, streetname, categorycode, DATE_FORMAT(dateRecorded, '%d/%m/%Y') AS Expr1, DATE_FORMAT(dateLastModified, '%d/%m/%Y') AS Expr2, status FROM incidentdetails WHERE (postcode = @postcode) AND (suburb = @suburb) AND (categorycode = @categorycode) AND (status = @status)";
string sql = "SELECT COUNT(*) FROM incidentdetails WHERE (postcode = @postcode) AND (suburb = @suburb) AND (categorycode = @categorycode) AND (status = @status)";
MySqlConnection mycon = new MySqlConnection(sqlconnection);
mycon.Open();
MySqlCommand selectRelatedCmd = new MySqlCommand(sql, mycon);
MySqlCommand sqlFillRelated = new MySqlCommand(sqlFILL, mycon);
int matches = 0;
selectRelatedCmd.Parameters.AddWithValue("@postcode", int.Parse(PostcodeTxtBox.Text.ToString()));
selectRelatedCmd.Parameters.AddWithValue("@suburb", SuburbTxtBox.Text.ToString());
selectRelatedCmd.Parameters.AddWithValue("@categorycode",IncidentTypeDropList.Text.ToString());
selectRelatedCmd.Parameters.AddWithValue("@status", "Open");
sqlFillRelated.Parameters.AddWithValue("@postcode", int.Parse(PostcodeTxtBox.Text.ToString()));
sqlFillRelated.Parameters.AddWithValue("@suburb", SuburbTxtBox.Text.ToString());
sqlFillRelated.Parameters.AddWithValue("@categorycode", IncidentTypeDropList.Text.ToString());
sqlFillRelated.Parameters.AddWithValue("@status", "Open");
matches = int.Parse(selectRelatedCmd.ExecuteScalar().ToString());
if (matches == 0)
{
matchingIncidentPanel.Visible = false;
}
else if (matches >= 1)
{
matchingIncidentPanel.Visible = true;
}
MySqlDataAdapter da = new MySqlDataAdapter(sqlFILL, mycon);
DataTable table = new DataTable();
table.Locale = System.Globalization.CultureInfo.InvariantCulture;
da.Fill(table);
g.DataSource = table;
g.DataBind();
mycon.Close();
MySQL에 액세스하는 언어의 태그도 추가해야한다고 생각합니다. – stalker