为什么我的标签不会改变

时间:2015-01-01 21:05:28

标签: c#

当我运行我的代码时,标签保持不变,但是当我调试它时,我可以看到文本发生变化,然后在运行完毕后再进行更改

public void getData(string a) 
{
    SqlConnection conn = new SqlConnection(@"Data Source=MASSI\FABERSERVER;Initial Catalog=Data.mdf;Integrated Security=True");
    conn.Open();
    SqlCommand command = new SqlCommand("Select UserID,UserName,Email FROM Login Where UserName= '" + a + "'", conn);
    SqlDataReader reader = command.ExecuteReader();

    while (reader.Read())
    {
        label1.Text = reader["UserID"].ToString();
        label2.Text = reader["UserName"].ToString();
        label3.Text = reader["Email"].ToString();
    }

    conn.Close();
}

1 个答案:

答案 0 :(得分:0)

以防万一,试试这个:

public void getData(string a) {
        SqlConnection conn = new SqlConnection(@"Data Source=MASSI\FABERSERVER;Initial Catalog=Data.mdf;Integrated Security=True");
        conn.Open();
        SqlCommand command = new SqlCommand("Select UserID,UserName,Email FROM Login Where UserName= '" + a + "'", conn);
        SqlDataReader reader = command.ExecuteReader();

        string id, name, email;

        while (reader.Read())
        {
            id = reader["UserID"].ToString();
            name = reader["UserName"].ToString();
            email = reader["Email"].ToString();

        }
        conn.Close();

        label1.Text = id;
        label2.Text = name;
        label3.Text = email;
    }
}

希望,这有帮助。