开发者

Workaround for ASP throwing MySQL exception

开发者 https://www.devze.com 2023-04-12 21:03 出处:网络
This is going to take some explaining. I\'m new to ASP, having come fro开发者_StackOverflow社区m PHP. Completely different world. Using the MySql Connecter/Net library, I decided to make a database wr

This is going to take some explaining. I'm new to ASP, having come fro开发者_StackOverflow社区m PHP. Completely different world. Using the MySql Connecter/Net library, I decided to make a database wrapper which had a fair amount of fetch methods, one being a "FetchColumn()" method which simply takes a string as its parameter and uses the following implementation:

public object FetchColumn(string query)
        {
            object result = 0;

            MySqlCommand cmd = new MySqlCommand(query, this.connection);

            bool hasRows = cmd.ExecuteReader().HasRows;

            if (!hasRows)
            {
                return false;
            }

            MySqlDataReader reader = cmd.ExecuteReader();

            int count = 0;

            while(reader.HasRows)
            {
                result = reader.GetValue(count);
                count++;
            }

            return result;
        }�          return result;
        }public object FetchColumn(string query)

What I'm looking for is a way to return false IF and only IF the query attempts to fetch a result which doesn't exist. The problem is that, with my implementation, it throws an error/exception. I need this to "fail gracefully" at run time, so to speak. One thing I should mention is that with this implementation, the application throws an error as soon as the boolean "hasRows" is assigned. Why this is the case, I have no idea.

So, any ideas?


It's hard to say for sure, since you didn't post the exact exception that it's throwing, but I suspect the problem is that you're calling ExecuteReader on a command that is already in use. As the documentation says:

While the MySqlDataReader is in use, the associated MySqlConnection is busy serving the MySqlDataReader. While in this state, no other operations can be performed on the MySqlConnection other than closing it. This is the case until the MySqlDataReader.Close method of the MySqlDataReader is called.

You're calling cmd.ExecuteReader() to check to see if there are rows, and then you're calling ExecuteReader() again to get data from the rows. Not only does this not work because it violates the conditions set out above, it would be horribly inefficient if it did work, because it would require two trips to the database.

Following the example shown in the document I linked, I'd say what you want is something like:

public object FetchColumn(string query)
{
    MySqlCommand cmd = new MySqlCommand(query, this.connection);
    MySqlDataReader reader = cmd.ExecuteReader();
    try
    {
        bool gotValue = false;
        while (reader.Read())
        {
            // do whatever you're doing to return a value
            gotValue = true;
        }
        if (gotValue)
        {
            // here, return whatever value you computed
        }
        else
        {
            return false;
        }
    }
    finally
    {
        reader.Close();
    }
}

I'm not sure what you're trying to compute with the HasRows and the count, etc., but this should get you pointed in the right direction.


you need to surround the error throwing code with a try clause

try { 
    //The error throwing Code 
} 
catch (exception e)
{
    //Error was encountered
    return false   
}

If the error throwing code throws and error the catch statement will execute, if no error is thrown then the catch statement is ignored


First of all do a try and catch

try
        {
            //code
        }
        catch (Exception exp)
        {
            //show exp as message
        }

And the possible reason of your error is that your mysql query has errors in it. try executing your query directly in your mysql query browser and you'll get your answer. If its working fine then double check your connection string if its correct.

NOTE:mark as answer if it solves your issue

0

精彩评论

暂无评论...
验证码 换一张
取 消