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
468 views
in Technique[技术] by (71.8m points)

c# - SQL timeout exception

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.

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

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); 
                }
            } 
        } 
   }

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

...