【发布时间】:2011-04-24 09:48:39
【问题描述】:
我在使用别名和参数的 MySQL Select Where 语句时遇到了一些问题。我的问题在于语句的 Where 部分。就目前而言,当我尝试使用任何参数时,我不会返回任何结果。
有问题的陈述是:
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 子句从 WHERE (postcode = @postcode) AND (suburb = @suburb) AND (categorycode = @categorycode) AND (status = @status) 更改为 WHERE (postcode = 4020)确实有效 - 正如预期的那样。
将 WHERE 子句替换为 (postcode = @postcode) 并传递参数(如下所示)不起作用。
sqlFillRelated.Parameters.AddWithValue("@postcode", int.Parse(PostcodeTxtBox.Text.ToString()))
从服务器资源管理器 SQL 命令运行时,完整的查询(带参数)可以成功运行。
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 的语言添加标签。
标签: c# mysql alias where-clause