Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用方法“获取编译错误”;并非所有代码路径都返回一个值";_C#_Asp.net_Methods_Compiler Errors_Boolean - Fatal编程技术网

C# 使用方法“获取编译错误”;并非所有代码路径都返回一个值";

C# 使用方法“获取编译错误”;并非所有代码路径都返回一个值";,c#,asp.net,methods,compiler-errors,boolean,C#,Asp.net,Methods,Compiler Errors,Boolean,我不明白为什么我总是得到编译错误:“不是所有的代码路径都返回一个值”。我正在编写一个简单的类方法,如果帐户可用,它应该返回true;如果帐户不可用或为null/空,它应该返回false。该方法的代码如下所示: public static bool AccountAvailable(int AccountId) { try { bool accountavailable; string queryTransaction =

我不明白为什么我总是得到编译错误:“不是所有的代码路径都返回一个值”。我正在编写一个简单的类方法,如果帐户可用,它应该返回true;如果帐户不可用或为null/空,它应该返回false。该方法的代码如下所示:

public static bool AccountAvailable(int AccountId)
{
    try
    {            
        bool accountavailable;

        string queryTransaction = "Select Count(AccountID) FROM Accounts WHERE AccountID = " + AccountId.ToString() + " AND AccountUsed = 0";

        //grab a connection to the database
        Database database = DatabaseFactory.CreateDatabase();

        //create an instance of the command
        DbCommand command = database.GetSqlStringCommand(queryTransaction);

        object dataobject = command.ExecuteScalar();

        if (dataobject == null || string.IsNullOrEmpty(Convert.ToString(dataobject)))
        {
            accountavailable = false;
        }

        else if (Convert.ToInt32(dataobject) == 0)
        {
            accountavailable = false;
        }

        else if (Convert.ToInt32(dataobject) > 0)
        {
            accountavailable = true;
        }

        else
        {
            accountavailable = true;
        }

        return accountavailable;
    }

    catch
    {

    }
}

在此方面的任何帮助或建议将不胜感激。谢谢

如果在返回值之前在代码中抛出异常,则控件将移动到
catch
块。然后它到达方法的末尾,而不返回任何内容


返回catch块内或之后的内容。

建议尝试此代码

public static bool AccountAvailable(int AccountId)
{
    bool accountavailable = false;
    try
    {            

        string queryTransaction = "Select Count(AccountID) FROM Accounts WHERE AccountID = " + AccountId.ToString() + " AND AccountUsed = 0";

        //grab a connection to the database
        Database database = DatabaseFactory.CreateDatabase();

        //create an instance of the command
        DbCommand command = database.GetSqlStringCommand(queryTransaction);

        object dataobject = command.ExecuteScalar();

        if (dataobject == null || string.IsNullOrEmpty(Convert.ToString(dataobject)))
        {
            accountavailable = false;
        }

        else if (Convert.ToInt32(dataobject) == 0)
        {
            accountavailable = false;
        }

        else if (Convert.ToInt32(dataobject) > 0)
        {
            accountavailable = true;
        }

        else
        {
            accountavailable = true;
        }

    }

    catch
    {

    }
    return accountavailable;
}

在catch块中,添加一个return:

catch (Exception ex)
{
    // your code
    return null;
}

解释当从
try
块中抛出异常时,您认为会发生什么。另外,首先还要解释一下为什么要使用try块。像您在这里所做的那样构建一个try-catch是一种“最糟糕的做法”,因此,如果您解释了为什么这样做,我们可以消除您的错误想法,这些错误想法导致您编写了这段糟糕的代码。您不应该捕获所有异常,然后对它们不做任何处理。如果你有问题,你永远也找不到哪里出了问题。如果您遇到了无法在代码中处理的问题,那么您需要了解它,这意味着至少您应该记录该问题,并且更可能的是,您应该让异常冒泡,因为您无法有意义地处理该问题。因此,如果您遇到意外异常,您会被要求做什么。哦,您不应该通过压缩字符串来生成查询,这会让您接受SQL注入。@Servy:很好的建议,但要学究一点,在这种情况下,很难用int注入任何有用的内容。@spender True,我注意到在我发表评论之后,但我觉得还是值得离开。我纠正了编译错误并回答了这个问题。我不判断编码的质量,实际上,再看一眼,这甚至不会编译
accountavailable
在最后使用时没有明确指定,这是一个编译器错误。我知道,我是法国人,英语也不完美,我在帖子中将“correct”替换为“suggest”