SQL timeout exception

a 夏天 提交于 2019-12-25 19:15:14

问题


Why do I get this exception in my code? I restarted the server, changed ports, etc, but nothing is working.

What's wrong?

DataTable dt = new DataTable();

SqlConnection con = new SqlConnection("server=localhost;user=armin;password=root;");
con.Open();

SqlCommand result = new SqlCommand(
    "SELECT userid FROM KDDData.dbo.userprofile order by userid", con);

SqlDataReader reader = result.ExecuteReader();
dt.Load(reader);

List<string> userids = new List<string>(dt.Rows.Count);

foreach (DataRow item in dt.Rows)
{
    userids.Add(item.ItemArray[0].ToString().Trim());
}

con.Close();

con = new SqlConnection("server=localhost;user=armin;password=root;");
con.Open();

foreach (string user in userids)
{
    DataTable temp = new DataTable();
    SqlCommand result1 = new SqlCommand(
    "select itemid from KDDTrain.dbo.train where userid=" + user, con);

    SqlDataReader reader1 = result1.ExecuteReader();

    if (!reader1.HasRows)
    {
        continue;
    }

    temp.Load(reader1);
}

The first query works fine, but the second doesn't. As you can see I even use some other SqlConnection but it still doesn't work.

Note:The database i'm working with has atleast 100 milion records,thought may be this would be a problem.


回答1:


Something doesn't look right in your connection string
I always seen "server=localhost; user=armin;password=root" in connections strings for MySql not for SqlServer where instead I will use "Data Source=(LOCAL);Integrated Security=SSPI" or the INSTANCE name of SqlServer. Are you sure that the first query works?.

However I think you should use the appropriate using statement

DataTable dt = new DataTable(); 
using(SqlConnection con = new SqlConnection("server=localhost;user=armin;password=root;"))
{
    using(SqlCommand result = new SqlCommand(
            "SELECT userid FROM KDDData.dbo.userprofile order by userid", con))
    {
        con.Open(); 
        using(SqlDataReader reader = result.ExecuteReader())
        {
           dt.Load(reader); 
           List<string> userids = new List<string>(dt.Rows.Count); 
           foreach (DataRow item in dt.Rows) 
           { 
              userids.Add(item.ItemArray[0].ToString().Trim()); 
           }
        } 
        DataTable temp = new DataTable(); 
        foreach (string user in userids) 
        { 
            using(SqlCommand result1 = new SqlCommand( 
            "select itemid from KDDTrain.dbo.train where userid=" + user, con))
            {
                using(SqlDataReader reader1 = result1.ExecuteReader())
                {
                    if (!reader1.HasRows)   continue; 
                    temp.Load(reader1); 
                }
            } 
        } 
   }



回答2:


Please insert this line

result1.CommandTimeout = 0;

befor this line in the second query

SqlDataReader reader1 = result1.ExecuteReader();  



回答3:


Dispose your reader after:

foreach (DataRow item in dt.Rows)
{
    userids.Add(item.ItemArray[0].ToString().Trim()); 
} 

...and also close the connection after temp.Load(reader1). Also close the reader1.

Instead of all this... the clean way is to use USING for initializng the readers and connection. :)



来源:https://stackoverflow.com/questions/10241771/sql-timeout-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!