计算一段程序运行的时间

protected void Button1_Click(object sender, EventArgs e)
        {
             Stopwatch stw = new Stopwatch();
             stw.Start();
             using (SqlConnection sqlcnn=new SqlConnection(sqlstr))
             {
                 using (SqlCommand sqlcmm=sqlcnn.CreateCommand())
                 {   
                     sqlcnn.Open();
                     sqlcmm.CommandText = "select * from users where id=1002222";
                     SqlDataReader reader = sqlcmm.ExecuteReader();
                     while (reader.Read())
                     {
                         Response.Write("id为:" + reader[0].ToString());
                         stw.Stop();
                         Response.Write("  用时为:" +stw.Elapsed.Milliseconds.ToString()+"毫秒");
                     }
                     sqlcnn.Close();
                 }
             }
        }

你可能感兴趣的:(计算一段程序运行的时间)