Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
184 views
in Technique[技术] by (71.8m points)

c# - Why am I getting "timeout" and "Cannot find table 0" and what preventive measures are at my disposal?

In the answer to a previous question here, I was advised to also ask about this related issue.

Once in awhile, the report generation code I have throws two exceptions (they don't display to the user, and they think everything is hunky dory), but I get them emailed to me.

The first is, "Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding."

...and the one that always follows quickly thereafter is, "Cannot find table 0"

The first exeption message opines that the last line of code in the try section ("new SqlDataAdapter(cmd).Fill(ds);") in the method below is throwing the exception:

public static DataTable ExecuteSQLReturnDataTable(string sql, 
    CommandType cmdType, params SqlParameter[] parameters)
{
    using (var ds = new DataSet())
    {
        using (var connStr = new SqlConnection(CPSConnStr))
        {
            using (var cmd = new SqlCommand(sql, connStr))
            {
                cmd.CommandType = cmdType;
                cmd.CommandTimeout = EXTENDED_TIMEOUT;
                foreach (var item in parameters)
                {
                    cmd.Parameters.Add(item);
                }

                try
                {
                    cmd.Connection.Open();
                    new SqlDataAdapter(cmd).Fill(ds);
                }
                catch (Exception ex)
                {
                    RoboReporterConstsAndUtils.HandleException(ex);
                }
                return ds.Tables[0];
            }
        }
    }
}

The second exception message claims that it emanates from the last significant line in the method above ("return ds.Tables[0];") as well as this one:

var dtFillRateResults =
    RoboReporterSQL.ExecuteSQLReturnDataTable
       (FILL_RATE_BY_DISTRIBUTOR_BY_CUSTOMER_STORED_PROC,
        CommandType.StoredProcedure,
            new SqlParameter()
            {
                ParameterName = "@Unit",
                SqlDbType = SqlDbType.VarChar,
                Value = _unit
            },
            new SqlParameter()
            {
                ParameterName = "@Member",
                SqlDbType = SqlDbType.VarChar,
                Value = _memberId
            },
            new SqlParameter()
            {
                ParameterName = "@BegDate",
                SqlDbType = SqlDbType.DateTime,
                Value = Convert.ToDateTime(_dateBegin)
            },
            new SqlParameter()
            {
                ParameterName = "@EndDate",
                SqlDbType = SqlDbType.DateTime,
                Value = Convert.ToDateTime(_dateEnd)
            }
    );

FILL_RATE_BY_DISTRIBUTOR_BY_CUSTOMER_STORED_PROC is a Stored Proc that is used elsewhere and prior to my efforts and activities here, so it's not the SP itself that's causing the problem.

For the super-curious, the bespoke method call from the code above is:

public static DataTable ExecuteSQLReturnDataTable(string connectionStr,     
    string sql, CommandType cmdType, params SqlParameter[] parameters)
{
    using (var ds = new DataSet())
    {
        using (var connStr = new SqlConnection(connectionStr))
        {
            using (var cmd = new SqlCommand(sql, connStr))
            {
                cmd.CommandType = cmdType;
                cmd.CommandTimeout = EXTENDED_TIMEOUT;
                foreach (var item in parameters)
                {
                    cmd.Parameters.Add(item);
                }

                try
                {
                    cmd.Connection.Open();
                    new SqlDataAdapter(cmd).Fill(ds);
                }
                catch (Exception ex)
                {
                    RoboReporterConstsAndUtils.HandleException(ex);
                    return null;
                }
                return ds.Tables[0];
            }
        }
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The errors are because the Fill or the open part here

try
{
    cmd.Connection.Open();
    new SqlDataAdapter(cmd).Fill(ds);
}

is timing out but since you handle the exception the execution ends up hitting the return statement where you are trying to return a table from the still null table collection of the dataset.

The timeout can happen for many reasons. Probably need more info for that but if you use the sql profiler in management studio you should be able to get an idea on if the connection opens, is proc called, how longs its trying to run and what part is causing the timeout. If it works elsewhere capture your input params and try those out directly.

Also setting a timeout of 0 should give you the max possible.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...