为什么我不能通过我的方法SQL
返回我的变量SQLSelection()
?
我已经宣布了,然后重新分配它然后返回它。但是这不起作用,再加上多个错误消息,什么都不返回?
private void SQLSelection()
{
String SQL = null;
if (comboBox1.Text == "All" & comboBox2.Text == "All")
{
String SQL = "SELECT stationID, LocationName, plandate, username, status FROM dbo.joblist WHERE status in ('New','Hold')";
}
else if (comboBox1.Text == "All" & comboBox2.Text == "@status")
{
String SQL = "SELECT stationID, LocationName, plandate, username, status FROM dbo.joblist WHERE status = @status";
}
else if (comboBox1.Text == "@username" & comboBox2.Text == "All")
{
String SQL = "SELECT stationID, LocationName, plandate, username, status FROM dbo.joblist WHERE status = @username";
}
else
{
String SQL = "SELECT stationID, LocationName, plandate, username, status FROM dbo.joblist WHERE username = @username and status = @status";
}
return String SQL;
}
答案 0 :(得分:3)
您的返回类型为void,而不是string。将其更改为:
private string SQLSelection()
虚空是一个“不归路”的#34;对某事物进行的行动,而不是"返回"返回return语句的值。如果您尝试返回值,则会遇到编译器错误。
此外,您继续重新声明
String SQL
在每个方法中。只需通过删除每个if语句中的String声明,将值分配给SQL。
最后,您的return语句再次尝试重新声明变量。
您生成的方法应如下所示:
private string SQLSelection()
{
String SQL = null;
if (comboBox1.Text == "All" & comboBox2.Text == "All")
{
SQL = "SELECT stationID, LocationName, plandate, username, status FROM dbo.joblist WHERE status in ('New','Hold')";
}
else if (comboBox1.Text == "All" & comboBox2.Text == "@status")
{
SQL = "SELECT stationID, LocationName, plandate, username, status FROM dbo.joblist WHERE status = @status";
}
else if (comboBox1.Text == "@username" & comboBox2.Text == "All")
{
SQL = "SELECT stationID, LocationName, plandate, username, status FROM dbo.joblist WHERE status = @username";
}
else
{
SQL = "SELECT stationID, LocationName, plandate, username, status FROM dbo.joblist WHERE username = @username and status = @status";
}
return SQL;
}
答案 1 :(得分:2)
您的方法签名表示返回void
,这意味着该方法不返回任何内容。将其更改为:
private string SqlSelection()
你也有SqlSelection
的一些问题,我怀疑它甚至编译。尝试像
private string SqlSelection()
{
string sql = null;
if (comboBox1.Text == "All" & comboBox2.Text == "All")
{
sql = "SELECT stationID, LocationName, plandate, username, status FROM dbo.joblist WHERE status in ('New','Hold')";
}
else if (comboBox1.Text == "All" & comboBox2.Text == "@status") }
...
return sql;
}
答案 2 :(得分:2)
1)您的返回类型应为String而不是void。
private String SQLSelection(){
2)不要在if语句中redaclare你的变量:
SQL =" SELECT ..&#34 ;;
3)你不需要在return语句中使用String:
返回SQL;
答案 3 :(得分:2)
你也可以在返回之前检查你的变量
private string SQLSelection()
{
private String SQL = null;
if (comboBox1.Text == "All" & comboBox2.Text == "All")
{
SQL = "SELECT stationID, LocationName, plandate, username, status FROM dbo.joblist WHERE status in ('New','Hold')";
}
else if (comboBox1.Text == "All" & comboBox2.Text == "@status")
{
SQL = "SELECT stationID, LocationName, plandate, username, status FROM dbo.joblist WHERE status = @status";
}
else if (comboBox1.Text == "@username" & comboBox2.Text == "All")
{
SQL = "SELECT stationID, LocationName, plandate, username, status FROM dbo.joblist WHERE status = @username";
}
else
{
SQL = "SELECT stationID, LocationName, plandate, username, status FROM dbo.joblist WHERE username = @username and status = @status";
}
if(SQL != null)
return SQL;
else
return "any phrase you want ";
}
答案 4 :(得分:1)
您的代码至少包含以下错误:
您的方法的返回类型声明为void
(意味着它没有任何返回值),因此您无法从中返回任何内容。将返回类型更改为string
:
private string SQLSelection()
您正在尝试重新声明要分配给它的SQL
变量,并在return语句中。例如,而不是:
String SQL = "…";
应该只是:
SQL = "…";
而不是:
return String SQL;
应该只是:
return SQL;
(String SQL;
是声明类型SQL
的变量string
的语法。您在方法的开头声明变量;您不需要稍后重新开始。)