我想在检查数据库表中的id后才重定向网页。如何使用条件或通过调用程序检查dis?
这是我的代码:
public partial class site : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnLink_Click(object sender, EventArgs e)
{
if (txtPropCode.Text != "")
{
Response.Redirect("http://real.com/Properties_Detail?id=" + txtPropCode.Text);
}
}
}
这是存储过程:
CREATE PROCEDURE [dbo].[Go]
(
@Pid bigint
)
AS
SELECT * from Properties where Id=@Pid
答案 0 :(得分:0)
您可以通过在codebehind中运行查询来实现此目的,因为它是一个非复杂的查询,因此不需要为此目的编写SP。如果您只需要验证记录是否存在,请使用ExecuteScaler方法并验证结果是否存在,请改为:
protected void btnLink_Click1(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(YourConnectionString);
SqlCommand cmd = new SqlCommand("Select * from Properties where Id=@Pid", con);
cmd.Parameters.AddWithValue("@Pid",Convert.ToInt32(txtPropCode.Text));
con.Open();
object id=cmd.ExecuteScalar();
If((int)id!=-1);
//it means record exists
else
//The record doesn't exist
}
现在由您决定是否要在记录存在时重定向用户,否则不会重定向或无论您需要做什么。它经过测试的代码可以使用。
希望它有所帮助!