Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...