ASP.NET做WEB服务时方法报错

来源:百度知道 编辑:UC知道 时间:2024/05/31 02:54:18
[WebMethod]
public bool selectStudentinfo(string ID,string No)
{
string connstr="server=localhost;uid=sa;pwd=;database=student;Trusted_Connection=no";
SqlConnection conn=new SqlConnection(connstr);
conn.Open();
SqlCommand cmd=new SqlCommand("select*from Student-info",conn);
SqlDataReader myReader;
myReader=cmd.ExecuteReader();
while(myReader.Read())
{
if(((Convert.ToString(ID)==myReader[0].ToString().Trim()))&&((Convert.ToString(No)==myReader[2].ToString().Trim())))
{
return true;
}

else
{
return false;
}
}
myReader.Close();
conn.Close();
}
}

老是报出“并非所有的代码路径都返回值” 的错误
说明:参数ID和No是客户端调用时要输入的字符串

你这个应该在 conn.Close();
后面再加一行return true或者false;

因为这个函数返回类型是bool,所以必须有返回值的,
而你这个函数编写,如果第一次执行到:while(myReader.Read()) 这条语句时,假如,没有找到相关记录,就不会进入这个循环,程序会转到最后直接执行
myReader.Close();
conn.Close();
而这里是没有返回值供返回的,你可以根据你的逻辑返回,当没有记录(你这个函数是,当表中没有任何数据时)返回true或false看你自己的处理逻辑了。

conn.Close();
改成
conn.Close();
return true;
就行了