那里的新问题。在从数据库中读取数据并且记录用完时,它只是说“当没有数据存在时尝试读取无效”,所以问题是如何防止这种情况发生?
这是我的代码:
{
Label3.Text = Request.QueryString["Username"];
}
SqlConnection con = new SqlConnection("Data Source=JEVGENIJ-PC;Initial Catalog=ViaFitness;Integrated Security=True");
static SqlDataReader dr;
SqlCommand cmd;
protected void Button1_Click(object sender, EventArgs e)
{
con.Open();
SqlCommand cmd = new SqlCommand("select * from Summary where UserName='"+Label3.Text+ "'", con);
dr = cmd.ExecuteReader();
dr.Read();
Label3.Text = dr[2].ToString();
Label1.Text = dr[1].ToString();
Label2.Text = dr[0].ToString();
}
protected void Button2_Click(object sender, EventArgs e)
{
dr.Read();
Label3.Text = dr[2].ToString();
Label1.Text = dr[1].ToString();
Label2.Text = dr[0].ToString();
con.Close();
}
}