C#catch无效(令牌无效)

时间:2014-12-31 19:06:02

标签: c# windows visual-studio-2013

        public Boolean Delete(Int32 HolidayNo)
    {
        //provides the functionality for the delete class

        //create an instance of the data connection class called MyDatabase
        clsDataConnection MyDatabase = new clsDataConnection();
        //add the HolidayNo parameter passed to this function to the list of parameters to use in the database
        MyDatabase.AddParameter("@HolidayNo", HolidayNo);
        //execute the stored procedure in the database
        MyDatabase.Execute("sproc_tblHolidays_Delete");
        //return value for function
        return true;
    }
    catch
    {
        return false;
    }
}

捕获的是无效令牌。不知道如何解决......

对于那些好奇的人,我正在制作一个删除功能来删除大学数据库中的某些值。

3 个答案:

答案 0 :(得分:3)

更改代码以添加尝试,然后检查任何内部异常的异常详细信息。

public Boolean Delete(Int32 HolidayNo)
{
    try
    {
        //provides the functionality for the delete class

        //create an instance of the data connection class called MyDatabase
        clsDataConnection MyDatabase = new clsDataConnection();
        //add the HolidayNo parameter passed to this function to the list of parameters to use in the database
        MyDatabase.AddParameter("@HolidayNo", HolidayNo);
        //execute the stored procedure in the database
        MyDatabase.Execute("sproc_tblHolidays_Delete");
        //return value for function
        return true;
    }
    catch (Exception ex)
    {
        return false;
    }
}

答案 1 :(得分:1)

您收到Unexpected Token错误的原因是您的catch块完全超出了您的方法。此外,您错过了try的{​​{1}}部分。要更正此问题,请将try / catch块放在方法中,然后将其余代码放在catch之前的try块中:

catch

答案 2 :(得分:0)

我甚至不知道如何汇编。请先清理您的方法。像这样:

public Boolean Delete(Int32 HolidayNo)
{
    var deleted = false;
    try {
        if (HolidayNo > 0) {
            //provides the functionality for the delete class

            //create an instance of the data connection class called MyDatabase
            clsDataConnection MyDatabase = new clsDataConnection();
            //add the HolidayNo parameter passed to this function to the list of parameters to use in the database
            MyDatabase.AddParameter("@HolidayNo", HolidayNo);
            //execute the stored procedure in the database
            MyDatabase.Execute("sproc_tblHolidays_Delete");

            deleted = true;
        }
    }
    catch (Exception ex)
    {
        // TODO: Log exception ex
        return deleted;
    }
}