我想检查某些字段是否具有某些唯一值 - 例如,我想检查表中的 field1、field2 是否具有值“YES”。如果它们都为"is",那么我想调用某个函数,但如果其中一个为“否”值,那么我想调用其他函数。请注意,在我的 select 语句中,我从查询字符串中传递了一个 ID。我该怎么做?
protected void Check_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e) {
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString);
SqlDataAdapter da = new SqlDataAdapter("SELECT ID, Field1, Field2 from MyTabel WHERE ID = '" + Request.QueryString["ID"] + "'", con);
DataTable dt = new DataTable();
da.Fill(dt);
}
请您参考如下方法:
如果您只需要确定给定记录中是否特定一组标准存在,那么我建议您在数据库端进行此检查,并且然后使用 ExecuteScalar 得到结果:
using(SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString))
{
using(SqlCommand cmd = new SqlCommand("SELECT count(*) from MyTabel WHERE ID =@ID And (Field1='Yes' And Field2='Yes')" , con))
{
cmd.parameters.Add("@ID",Request.QueryString["ID"]);
con.open();
int result = cmd.ExecuteScalar();
con.close();
if(result == 1)
// condition exists
// so call success function
else
// call failure function
}
}
更新:
这可能与问题没有直接关系,但要从 detailsView 获取记录 Id,您需要执行 2 个步骤:
将
detailsView的datakeynames属性设置为表的主键名称 - 在本例中为ID。<asp:detailsview datakeynames="ID"现在您可以通过以下方式访问选定的 Id
int id = (int)detailsView.SelectedValue;
点击这里了解更多information .
