string abc = WebConfigurationManager.ConnectionStrings["connectionName"].ConnectionString;
SqlConnection con = new SqlConnection(abc);
SqlCommand cmd = new SqlCommand("SELECT UserName ,Password from [Login] WHERE UserName =@usern AND Password = @pass", con);
con.Open();
cmd.Parameters.AddWithValue("@usern", username1.Text);
cmd.Parameters.AddWithValue("@pass", TextBox9.Text);
try
{
//if user iputs = to the values in my DB
Response.Redirect("Userinfo.aspx?id=" + username1.Text);
}
//if user iputs != to the values in my DB
catch(SqlException ex)
{
//error
Label1.Visible = true;
}
cmd.ExecuteNonQuery();
con.Close();
即使用户输入!=我的数据库中的值
,也会使用try语句