我有以下代码来读取数据是否存在。我想返回true或false,但C#编译器说:" 并非所有代码路径都返回值"我的方法名称ReadDV
上显示红色错误行。
代码是:
public bool ReadDV(string ReadCommand)
{
try
{
SqlConnection SCO = ConnectionClass.getconnection();
SqlCommand delCmd = new SqlCommand(ReadCommand, SCO);
if (SCO.State != ConnectionState.Open)
SCO.Open();
SqlDataReader r = delCmd.ExecuteReader();
if (r.Read())
{
if (SCO.State != ConnectionState.Closed)
SCO.Close();
r.Close();
return true;
}
}
catch (Exception ex)
{
return false;
}
}
ConnectionClass.getconnection()
代码是返回连接字符串的方法的一部分,它正常工作。我将添加我的代码的第二部分来调用此方法:当read为null时它不起作用
string RUSERSSV = @"select * from USERSSV where Users = ........;
DB db = new DB(); // calling method
if (comboBox1.Text != "")
{
if (db.ReadDV(RUSERSSV)==true) // this is the mention part
{
string IUSERSSV = @"update USERSSV set Users = // insert fired if read is true
db.insert(IUSERSSV);
Save.Form(this);
}
else
{
string IUSERSSV = @"insert into USERSSV // update fired if read false
db.insert(IUSERSSV);
Save.Form(this);
}
答案 0 :(得分:4)
您在if
中有返回声明可能执行或不执行您应该有一个必须执行的return
声明。如果无条件执行,则在catch之后放置返回可能会删除错误。您可以使用bool变量来存储返回的值并返回该变量。
bool success = false;
try
{
SqlConnection SCO = ConnectionClass.getconnection();
SqlCommand delCmd = new SqlCommand(ReadCommand, SCO);
if (SCO.State != ConnectionState.Open) SCO.Open();
SqlDataReader r = delCmd.ExecuteReader();
if (r.Read())
{
if (SCO.State != ConnectionState.Closed) SCO.Close();
r.Close();
success = true;
}
}
catch (Exception ex)
{
return false;
}
return success;
答案 1 :(得分:1)
将return语句放在 if(r.Read())块
之外if (r.Read())
{
if (SCO.State != ConnectionState.Closed) SCO.Close();
r.Close();
}
// other code
return true;
基本上,在函数结束之前应该有一个return语句。
答案 2 :(得分:1)
当if(r.Read())
为false时,您的代码不会返回任何内容。
您可以按照以下方式修复代码:
public bool ReadDV(string ReadCommand)
{
bool returnValue = false;
try
{
SqlConnection SCO = ConnectionClass.getconnection();
SqlCommand delCmd = new SqlCommand(ReadCommand, SCO);
if (SCO.State != ConnectionState.Open) SCO.Open();
SqlDataReader r = delCmd.ExecuteReader();
if (r.Read())
{
if (SCO.State != ConnectionState.Closed) SCO.Close();
r.Close();
returnValue = true;
}
}
catch (Exception ex)
{
returnValue = false;
}
return returnValue;
}
当r.Read为false或捕获到异常时,上面的代码现在总是返回false。
答案 3 :(得分:1)
我提出这个问题,它是一个更清洁的解决方案,它可以防止像你这样的问题:
public bool ReadDV(string ReadCommand)
{
bool result = false;
try
{
SqlConnection SCO = ConnectionClass.getconnection();
SqlCommand delCmd = new SqlCommand(ReadCommand, SCO);
if (SCO.State != ConnectionState.Open) SCO.Open();
SqlDataReader r = delCmd.ExecuteReader();
if (r.Read())
{
if (SCO.State != ConnectionState.Closed) SCO.Close();
r.Close();
result = true;
}
}
catch (Exception ex)
{
result = false;
}
return result;
}
答案 4 :(得分:1)
public bool ReadDV(string ReadCommand)
{
bool bRetVal = false;
try
{
SqlConnection SCO = ConnectionClass.getconnection();
SqlCommand delCmd = new SqlCommand(ReadCommand, SCO);
if (SCO.State != ConnectionState.Open) SCO.Open();
SqlDataReader r = delCmd.ExecuteReader();
if (r.Read())
{
if (SCO.State != ConnectionState.Closed) SCO.Close();
r.Close();
bRetVal = true;
}
}
catch (Exception ex)
{
bRetVal = false;
}
return bRetVal
}
尝试初始化变量中的返回值,然后根据代码路径更改值,最后只有一个return语句,这样就不需要多个return语句。 (错误是因为您错过了该函数的return语句 - 每个可能的代码路径都应返回该函数的值)
答案 5 :(得分:0)
感谢所有
我用以下内容制作:
public bool ReadDV(string ReadCommand) { bool result = false; 尝试 { SqlConnection SCO = ConnectionClass.getconnection(); SqlCommand delCmd = new SqlCommand(ReadCommand,SCO); if(SCO.State!= ConnectionState.Open)SCO.Open(); SqlDataReader r = delCmd.ExecuteReader();
if (r.Read()==true)
{
if (SCO.State != ConnectionState.Closed) SCO.Close();
r.Close();
result = true;
}
else if (r.Read() == false)
{
if (SCO.State != ConnectionState.Closed) SCO.Close();
r.Close();
result = false;
}
}
catch (Exception ex)
{
result = false;
}
return result;
}